swagger-api / swagger-api/swagger-ui
OpenAPI 3.1.0 support: add support for validating parameters against JSON Schema 2020-12
@char0n is already working on this.
Since Aug 16, 2023.
- Dominant language
- JavaScript
- Stars
- 29k
- Forks
- 9.3k
- Avg merge
- 2d 23h
- Merged PRs (30d)
- 25
Description
This issue was originally reported by @jashanbhullar, in https://github.com/swagger-api/swagger-editor/issues/2638#issuecomment-1679885972. The cause of the issue is following code: https://github.com/swagger-api/swagger-ui/blob/1ce9ce0cda2a55bf3896c0d89706400a83ba1d7e/src/core/utils.js#L412.
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:
While with exclusiveMaximum and exclusiveMinimum, it doesn't throw a validation error and sends the request to the backend
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.