Skip to content

OpenAPI 3.1.0 support: add support for validating parameters against JSON Schema 2020-12 #9134

Open
@char0n

Description

@char0n

This issue was originally reported by @jashanbhullar, in swagger-api/swagger-editor#2638 (comment). The cause of the issue is following code:

function validateValueBySchema(value, schema, requiredByParam, bypassRequiredCheck, parameterContentMediaType) {
.

This function is JSON Schema Draft 4/5 specific and validates parameter values (in primitive way). This was identified during OpenAPI 3.1.0 implementation, but the implementation was postponed after the SwaggerUI@5 release with related issues.

This code needs to be isolated into separate plugin and the code needs to be overridden by JSON Schema 2020-12 specific code when definition is of OpenAPI 3.1.0 type.


For type number you can select the bounds by using minimum and maximum or by using exclusiveMinimum and exclusiveMaxmimum. Docs here

By using minimum and maximum the editor shows the error as:

image

While with exclusiveMaximum and exclusiveMinimum, it doesn't throw a validation error and sends the request to the backend

image

Same behaviour is observed in swagger 3.0 and in the editor https://editor.swagger.io/ so I guess this has always been there.

The syntax of exclusiveMaximum and exclusiveMinimum has also changed in 3.1. Source

Check the spec below

openapi: 3.1.0
info:
  title: FastAPI
  version: 0.1.0
paths:
  /:
    get:
      summary: Read Root
      operationId: read_root__get
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema: {}
  /number-with-no-equal:
    get:
      summary: Read Item
      operationId: read_item_number_with_no_equal_get
      parameters:
        - name: num
          in: query
          required: true
          schema:
            type: integer
            exclusiveMaximum: 100
            exclusiveMinimum: 0
            description: Enter a number between 0 and 100
            title: Num
          description: Enter a number between 0 and 100
          example: 1
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema: {}
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                properties:
                  detail:
                    items:
                      properties:
                        loc:
                          items:
                            anyOf:
                              - type: string
                              - type: integer
                          type: array
                          title: Location
                        msg:
                          type: string
                          title: Message
                        type:
                          type: string
                          title: Error Type
                      type: object
                      required:
                        - loc
                        - msg
                        - type
                      title: ValidationError
                    type: array
                    title: Detail
                type: object
                title: HTTPValidationError
  /number-with-equal:
    get:
      summary: Read Item
      operationId: read_item_number_with_equal_get
      parameters:
        - name: num
          in: query
          required: true
          schema:
            type: integer
            maximum: 100
            minimum: 0
            description: Enter a number between 0 and 100
            title: Num
          description: Enter a number between 0 and 100
          example: 1
      responses:
        '200':
          description: Successful Response
          content:
            application/json:
              schema: {}
        '422':
          description: Validation Error
          content:
            application/json:
              schema:
                properties:
                  detail:
                    items:
                      properties:
                        loc:
                          items:
                            anyOf:
                              - type: string
                              - type: integer
                          type: array
                          title: Location
                        msg:
                          type: string
                          title: Message
                        type:
                          type: string
                          title: Error Type
                      type: object
                      required:
                        - loc
                        - msg
                        - type
                      title: ValidationError
                    type: array
                    title: Detail
                type: object
                title: HTTPValidationError
components:
  schemas:
    HTTPValidationError:
      properties:
        detail:
          items:
            properties:
              loc:
                items:
                  anyOf:
                    - type: string
                    - type: integer
                type: array
                title: Location
              msg:
                type: string
                title: Message
              type:
                type: string
                title: Error Type
            type: object
            required:
              - loc
              - msg
              - type
            title: ValidationError
          type: array
          title: Detail
      type: object
      title: HTTPValidationError
    ValidationError:
      properties:
        loc:
          items:
            anyOf:
              - type: string
              - type: integer
          type: array
          title: Location
        msg:
          type: string
          title: Message
        type:
          type: string
          title: Error Type
      type: object
      required:
        - loc
        - msg
        - type
      title: ValidationError

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions