Extra JSON Reference properties ignored - reduces reusability of references
Hi everyone.
I feel that the ignoring of extra properties when using JSON References goes against the goal of reusability.
Take this example from a fictional Swagger contract:
{
addresses: [
"homeAddress": {
"description": "The premises at which electricity is provided.",
"$ref": "#/definitions/Address"
},
"invoiceAddress": {
"description": "The billing address - must be where the customer's credit card is registered.",
"$ref": "#/definitions/Address"
}
]
}
The "description" properties help to distinguish between the two uses of the "Address" Reference. Of course you could argue that better naming of the "homeAddress" and "invoiceAddress" properties could achieve this, but we are not always in control of property names in our data models and sometimes you need a more verbose description.
The Swagger editor (http://editor.swagger.io/#/) is an example of an application that could be improved by removing this limitation.
Is this something that has been raised previously? Please see here for a discussion on the Swagger Google Group: https://groups.google.com/forum/#!topic/swagger-swaggersocket/ewgimdO2cOI
@nickreevesuk If you take the time to read the history of this issue, you will see that we all would like this feature to exist. $ref works the way it does in OpenAPI because that's the way it is defined in JSON Schema. Despite this shortcoming of the $ref feature, community consensus is that people want OpenAPI to use JSON Schema more consistently with its spec, not less.
There have been efforts to address this issue in JSON-Schema in the past, but those efforts stalled. Now that the folks working on that spec have got some momentum again, it is possible that we may see this issue resolved in the near future.
As I noticed you are new here on GitHub, perhaps you are not aware this is a volunteer run project and the best way to make things happen is to contribute. Those who are involved tend not to react positively to threats suggesting that if you don't implement this feature, all your users are going to leave.
Now, I am going to go back to trying to analyze the differences between the draft-4 JSON Schema spec and the latest draft so that we can adopt the new JSON-Schema spec in OpenAPI V3 without introducing breaking changes for our users.
@darrelmiller no threat intended, that's just simply the way mindshare of projects works. I was just trying to move the discussion back to the requirements.
@darrelmiller Thank you for your patience and for the intention to (re)check whether the last draft of the JSON spec might add more flexibility in this regard! However, regarding your remark that:
the best way to make things happen is to contribute
I'd say that actually there is at least such an initiative from the community --- see this comment, for example.
@darrelmiller do note that Draft 06 is almost ready, with most of the remaining PRs as minor bugfix sorts of things: https://github.com/json-schema-org/json-schema-spec/milestone/2
I strongly recommend looking at this rather than Draft 05 as there are significant improvements.
We're not going to include changes from draft 05 or 06 at this stage. The reason we're considering moving to 05 is that it doesn't change the functionality of the things that are already supported by the OAS. It looks like 06 adds functionality, and it's too late for us to evaluate whether we can support it or not. To add to that, since tooling takes time to catch up, I don't think it's wise for us to play the catch-up game in this case.
There is one major issue with draft 05 that has to be resolved before we can switch to it, and that's the drop of the integer type. If that's added back to draft 05, we're likely to move to it. If not, for now, we'll stick with draft 04 (even if expired).
@webron it was apparently an unintentional deletion on the draft editor's part and has been added back in Draft 06. You can't actually change a draft once published so the actual draft 05 document won't reflect this, but it's safe to treat draft 05 as if integer was still supported. There is actually one part in draft 05 that mentions 7 types as if integer was still present, so it wasn't even entirely removed.
The only other thing I'd note about going from draft 05 to draft 06 is that draft 05 added a "uriref" format which became "uri-ref" in draft 06 (because "date-time" and making all compound formats use hyphens).
Otherwise, draft 06 validation is compatible with draft 05 so you'll still be on a smooth path. The big changes (in both) were in hyper-schema which does not impact you.
Yeah, I think it was @Relequestual who mentioned that it was an unintentional drop. However, it don't know if it's safe to treat it that way. If you can't change it, then I would expect you to release draft 06 with the fix and push the rest of the changes as draft 07. I realize this may not work with your plan.
To avoid confusion, it might be safer for us to stay with draft 04 for now (assuming you can't make the suggested change) and look back on updating the support in the next version of our spec.
Having looked at the root cause of this I can suggest one other possible approach in addition to those I mentioned above.
The underlying reason why JSON schema specification rejects siblings of $ref seems to be because of the concern about these potentially overriding validation criteria in different references to the same underlying definition in conflicting ways. It is safe for purely JSON schema validation purposes to ignore siblings of $ref which don't affect validation as others have already observed.
This opens the possibility of defining the OpenAPI as a superset of the JSON schema where all additional tags must begin with a whitelisted set of prefixes which get stripped out to reduce the contents to a standard JSON schema document. This allows the OpenAPI file to be used both as is by tools which understand the additions or will ignore them. For tools with a strict interpretation of JSON schema we require an additional tool in the toolchain which will strip out this well defined set of extensions to to produce a new set of JSON schema specification compliant input files.
Although this does require an extra tool in the chain it does break the log jam by decoupling the two specifications, leaving OpenAPI much more free to evolve as it needs. This might help with a much wider set of issues.
Following the principle that things should be closed for modification but open for extension it would have been more helpful if the JSON schema specification forbad only tags it understood as siblings of $ref, but that is beyond the scope of this group.
@nickreevesuk json-schema-org/json-schema-spec#98 , the "$use" proposal mentioned earlier which is under consideration for Draft 07 in large part because of support from folks from OAI. Options for implementing it would include either a whitelist (all annotation keywords are on the whitelist and can appear in the "with" clause) or blacklist approach (all validation keywords are on the blacklist and cannot appear in the "with" clause). Either approach could be extensible,a nd both could be used.
It is easy enough for OAI to implement "$use" as an extension which could then fold into JSON Schema proper, just keep us posted on any implementation choices you make.
I ran into this issue recently and add the following as a practical note:
Scanning the descriptions in the apis.guru OpenAPI directory, I found two descriptions that put "description" fields alongside "$ref".
I expect that this will be a fairly common mistake.
We might guard against this by enforcing schema validation early. For example, public archives like apis.guru might only publish API models that pass validation.
But hand-written source files are what they are.
For better or worse, I was able to accept these descriptions in my schema-driven reader by generating my reader from a modified schema that adds a "description" property to the "jsonReference" definition.
"jsonReference": {
"type": "object",
"required": [
"$ref"
],
"additionalProperties": false,
"properties": {
"$ref": {
"type": "string"
},
"description": {
"type": "string"
}
}
}
Pointing to specific lines in those would help, because I can't find a problem. That said, there's nothing to catch in the validation. It's not wrong to have additional properties, they're just ignored. There's nothing invalid about that.
For example, public archives like apis.guru might only publish API models that pass validation.
We do. :) What @webron said.
I think there is a real use case for having description near references – a description for a type is not suitable as description for all properties of this type. That is why people put a description there – even knowing than any tools will ignore it, it is useful for human readers.
We had a similar discussion over at JSON Schema as @ePaul explains. The description is ignored when with a $ref, but actually it's still useful for human readers of the raw documents.
Wouldn't it be nice having this valuable information as part of visual OpenAPI clients, like Swagger UI, and not just for a human incidentally reading the raw JSON schema?
As stated earlier, we are looking into adding a new key word to JSON Schema core which will allow this sort of thing. JSON reference was kind of rolled into JSON Schema.
Regardless I think this is probably more of an upstream issue from Swagger / OAI, as they are not defining the behaviour.
We had a similar discussion over at JSON Schema as @ePaul explains. The description is ignored when with a $ref, but actually it's still useful for human readers of the raw documents.
Absolutely - see the initial discussion of this subject on the JSON Schema group The proposal to adapt to this in a generic way is JSON Schema #98, but I would tend to agree that the description seems to be by-far the most common use case.
If apis.guru provides strong support to indicate this issue applies almost exclusively to the description, and it the solution could be much simpler if only support for the description were required, I'd encourage you to go for it!
If apis.guru provides strong support to indicate this issue applies almost exclusively to the description...
I'm not sure if you meant APIs.guru the collection or APIs.guru the maintainer (hi!), but anyway here is some analysis.
Of 5,182 instances of $ref having other properties:
4,356 are description only
283 are x-ms-client-flatten
138 are title only
131 are description and readOnly
107 are description and title
102 are description and x-ms-client-flatten
20 are properties and required
16 are example
7 are type
6 are description and type
6 are readOnly
5 are description and x-ms-client-name
3 are description and externalDocs
2 are required
The results may be skewed somewhat by the Google specs being particularly bad offenders of $ref + description.
If anyone's interested I could run the same analysis over our entire test corpus of nearly 50,000 definitions.
If anyone's interested I could run the same analysis over our entire test corpus of nearly 50,000 definitions.
@MikeRalphson I would love to see this information, and if you could update json-schema-org/json-schema-spec#98 with it that would be great. The main reason that proposal hasn't gone in is that a few people are refusing to support it on the grounds that they want to be able to use it with all validation keywords. My argument has been that annotation keywords (description, title, default, and examples from the validation spec, readOnly from the hyper-schema spec) are their own distinct use case that has substantially broader support for overrides than validation. Your data so far would seem to support that.
I'm curious about the type usage, though- any patterns there?
For refernece, myself and @handrews are maintainers of JSON Schema.