From 70e2b6a9ee9affbff6a15851d8366b273d6f235e Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Tue, 19 Dec 2023 12:30:58 +0100 Subject: [PATCH] [#3607] Fix OpenAPI schema Add the new validator to the list. This list is now sorted to ensure consistency. --- src/openapi.yaml | 8 ++++++- src/openforms/api/drf_spectacular/plumbing.py | 21 ++++++++++++++++++- src/openforms/validations/api/views.py | 14 ++++++++++--- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/openapi.yaml b/src/openapi.yaml index 72cefee23b..319e551e3d 100644 --- a/src/openapi.yaml +++ b/src/openapi.yaml @@ -5269,6 +5269,7 @@ paths: schema: type: string enum: + - brk-Zaakgerechtigde - kvk-branchNumber - kvk-kvkNumber - kvk-rsin @@ -9312,8 +9313,13 @@ components: properties: value: type: string - description: Value to be validated + description: Value to be validated. + submissionUuid: + type: string + format: uuid + description: UUID of the submission. required: + - submissionUuid - value ValidationPlugin: type: object diff --git a/src/openforms/api/drf_spectacular/plumbing.py b/src/openforms/api/drf_spectacular/plumbing.py index 0583e17213..823450bb56 100644 --- a/src/openforms/api/drf_spectacular/plumbing.py +++ b/src/openforms/api/drf_spectacular/plumbing.py @@ -1,9 +1,13 @@ +from typing import Any + from drf_spectacular.plumbing import ( ResolvedComponent, build_basic_type, build_parameter_type, ) -from drf_spectacular.utils import OpenApiParameter +from drf_spectacular.utils import OpenApiParameter, inline_serializer +from rest_framework.fields import Field +from rest_framework.serializers import Serializer def build_response_header_parameter( @@ -53,3 +57,18 @@ def build_response_header_component( object=name, ) return component + + +def extend_inline_serializer( + serializer: type[Serializer], + fields: dict[str, Field], + name: str | None = None, + **kwargs: Any, +): + """Return an inline serializer, with fields extended from the provided serializer. + + If one of the provided extra fields already exists on the base serializer, it will be overridden. + """ + return inline_serializer( + name or serializer.__name__, serializer().get_fields() | fields, **kwargs + ) diff --git a/src/openforms/validations/api/views.py b/src/openforms/validations/api/views.py index 540782f5b2..5797ccbdf2 100644 --- a/src/openforms/validations/api/views.py +++ b/src/openforms/validations/api/views.py @@ -4,12 +4,13 @@ from drf_spectacular.types import OpenApiTypes from drf_spectacular.utils import OpenApiParameter, extend_schema, extend_schema_view -from rest_framework import authentication, permissions +from rest_framework import authentication, permissions, serializers from rest_framework.exceptions import PermissionDenied from rest_framework.response import Response from rest_framework.views import APIView from openforms.api.authentication import AnonCSRFSessionAuthentication +from openforms.api.drf_spectacular.plumbing import extend_inline_serializer from openforms.api.views import ListMixin from openforms.submissions.constants import SUBMISSIONS_SESSION_KEY from openforms.submissions.models import Submission @@ -61,14 +62,21 @@ class ValidationView(APIView): @extend_schema( operation_id="validation_run", summary=_("Validate value using validation plugin"), - request=ValidationInputSerializer, + request=extend_inline_serializer( + ValidationInputSerializer, + { + "value": serializers.CharField( + label=_("value"), help_text=_("Value to be validated.") + ) + }, + ), responses=ValidationResultSerializer, parameters=[ OpenApiParameter( "validator", OpenApiTypes.STR, OpenApiParameter.PATH, - enum=[validator.identifier for validator in register], + enum=sorted([validator.identifier for validator in register]), description=_( "ID of the validation plugin, see the [`validation_plugin_list`]" "(./#operation/validation_plugin_list) operation"