Skip to content

Commit

Permalink
[#3607] Fix OpenAPI schema
Browse files Browse the repository at this point in the history
Add the new validator to the list. This list is now sorted to ensure
consistency.
  • Loading branch information
Viicos committed Dec 19, 2023
1 parent 507f855 commit 70e2b6a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5269,6 +5269,7 @@ paths:
schema:
type: string
enum:
- brk-Zaakgerechtigde
- kvk-branchNumber
- kvk-kvkNumber
- kvk-rsin
Expand Down Expand Up @@ -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
Expand Down
21 changes: 20 additions & 1 deletion src/openforms/api/drf_spectacular/plumbing.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down Expand Up @@ -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
)
14 changes: 11 additions & 3 deletions src/openforms/validations/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 70e2b6a

Please sign in to comment.