Reference objects don't combine well with “nullable”
See original GitHub issuePerhaps more of a question than an issue report, but I am not sure how to combine “nullable” with a reference object; I have the impression they don’t combine well?
In Swagger-UI issue 3325 I posted an example with the following schema. The intent is to express that both “dataFork” and “resourceFork” contain a Fork, except that “resourceFork” can also be null
, but “dataFork” cannot be null
. As was pointed out though, the schema is not in accordance with the OpenAPI v3.0.0 specification, due to the use of JSON Schema’s {"type":"null"}
which has not been adopted in OpenAPI.
...
"File": {
"type": "object",
"properties": {
"name": { "type": "string" },
"dataFork": { "$ref": "#/components/schemas/Fork" },
"resourceFork": {
"anyOf": [
{ "type": "null" },
{"$ref": "#/components/schemas/Fork" } ] },
...
I considered I can instead express the example as follows, which as far as I can tell is at least in accordance with the OpenAPI specification? But the use of “anyOf” with only one schema inside seems clumsy, is there a better way to handle this? Essentially, a better way to combine “nullable” with the reference object?
...
"File": {
"type": "object",
"properties": {
"name": { "type": "string" },
"dataFork": { "$ref": "#/components/schemas/Fork" },
"resourceFork": {
"nullable": true,
"anyOf": [
{ "$ref": "#/components/schemas/Fork" } ] },
...
Issue Analytics
- State:
- Created 6 years ago
- Reactions:31
- Comments:39 (26 by maintainers)
Top GitHub Comments
A is not valid OpenAPI v3.0, but would be valid OpenAPI v3.1 as it has aligned with JSON Schema 2019-09.
I’ve never seen B, feels funny. Don’t have all the answers for this. Most folks do the allOf thing but that is apparently incorrect and always has been, despite me doing it for bloomin ages in various tools which seemed to think it was just fine.
Yes, I have read it. One reason I am advocating for examples, is that without them, it’s pretty challenging to understand what is valid in each of these specs. You have to read OAS 3.1.0, 3.0.2 and the JSON schema, carefully parse some of the crucial sentences and compare them. I think all of this confusion can go away with a few good examples.