-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
describing nullable field by anyOf leads to bad swagger-ui #2094
Comments
If I understand the spec correctly, directly using $ref without anyOf/allOf may violate the OpenAPI spec: swagger-api/swagger-parser#2036 (comment), so while using $ref may look clean, it might not work :( |
I don't think the proposed solution is a valid schema. Combining the It may be possible only if My reading of section 7.5 of the JSON Schema spec [1] is that they are treated separately, so the [1] https://json-schema.org/draft/2020-12/draft-bhutton-json-schema-01#name-applicators |
Thank you @Felk and @MikeEdgar for your quick responses.
All i know is that in OpenApi 3.0.3 the description in swagger-ui was different for the reader (I got a ticket in our project, that the swagger-ui description changed a lot since we updated to Quarkus 3.17 so that it's much more difficult for the user to understand the APIs).
---
openapi: 3.0.3
components:
schemas:
DTO:
type: object
properties:
name:
type: string
example: Hello World
localTime:
type: string
allOf:
- $ref: "#/components/schemas/LocalTime"
example: 13:45:30.123456789
localTimeNullable:
type: string
example: 13:45:30.123456789
anyOf:
- $ref: "#/components/schemas/LocalTime"
- enum:
- null
nullable: true
LocalTime:
format: local-time
type: string
externalDocs:
description: As defined by 'partial-time' in RFC3339
url: https://www.rfc-editor.org/rfc/rfc3339.html#section-5.6
example: 13:45:30.123456789
paths:
/hello:
get:
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/DTO"
summary: Hello
tags:
- Greeting Resource
info:
title: code-with-quarkus API
version: 1.0.0-SNAPSHOT I know that seems to be a low prio issue. Using/displaying anyOf is technically correct. So i have no way to give the user what he wants. ;( Maybe i should open a related ticket in swagger-ui. p.s. there seems a problem with OpenAPi-Validator APIDevTools/swagger-parser#262 so that i can not validate if the OpenApi.yaml i suggested is valid...At least https://editor-next.swagger.io/ digested it without any complains |
In my search for an existing issue in swagger-ui i found the following: swagger-api/swagger-ui#9056
according to this localTimeNullable:
$ref: "#/components/schemas/LocalTime"
type:
- string
- "null" might be correct and the better solution... |
I don't think that representation would give the expected results with JSON Schema. That root Basically, the schema would validated against a non-null instance since both the [1] https://json-schema.org/draft/2020-12/json-schema-core#section-7.5 |
According to OAI/OpenAPI-Specification#3148 both ways are valid (even if the example there uses no $ref)....but i'm no JSON-Schema expert. |
I think the presence of the You can play around with variations of JSON schema with a validator [1]. Consider the following JSON schema that uses the {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$defs": {
"localtime": {
"type": "string",
"format": "local-time",
"pattern": "\\d{2}:\\d{2}:\\d{2}"
},
},
"type": "object",
"properties": {
"value": {
"type": [ "null", "string" ],
"anyOf": [
{ "$ref": "#/$defs/localtime" },
{ "type": "null" }
]
}
}
} Both of these json inputs are valid: {
"value": "12:45:50"
}
{
"value": null
} However, if we modify the schema to remove the {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$defs": {
"localtime": {
"type": "string",
"format": "local-time",
"pattern": "\\d{2}:\\d{2}:\\d{2}"
},
},
"type": "object",
"properties": {
"value": {
"type": [ "null", "string" ],
"$ref": "#/$defs/localtime"
}
}
} |
Thank you @MikeEdgar for your clarification. |
Using OpenApi 3.1.0 nullable fields are described using anyOf:
In swagger-ui this yaml will be rendered as follows:
As you can see (especially if collapsed) the 'real' type of the nullable field is not easy to see.
IMHO instead of using anyOf to describe that the field is nullable, using $ref and a type-array would be better:
This would be rendered as follows
AFAIK the later kind is valid OpenApi 3.1.0, https://editor-next.swagger.io/ shows no warnings/errors
see https://github.com/ChMThiel/quarkus_demo/tree/OpenApi_generator_3.1.0_nullable for reproducer
The text was updated successfully, but these errors were encountered: