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
Currently, we use JSON References as-is. While there's a huge advantage to that tooling-wise, it does hinder reusability in some cases, and the issue has been raised (unofficially) a few times. Overriding the description is one case, but there are also cases of wanting to override whether fields are required and so on. Not sure if we should change it, given the cost, but we should consider it.
I think this is a very good Proposal. In my example I want to set:
parameters:
- $ref: '#/parameters/fields'
default: 'local_default'
I would be willing to update json-refs to have a flag that allowed you to handle this situation. Based on OpenAPI feature requests, I can see a few different things we'd need:
- Merging keys, like the example above
- Overriding/Replacing keys, possibly like the example below
The JSON Reference spec clearly says these should be ignored. But...there's nothing stopping json-refs from allowing extra functionality on top in an opt-in manner.
Realize, this has nothing specific to do with OpenAPI other than a number of the Node.js tools around OpenAPI use json-refs. A decision on either side does not have to reflect or impact the ability/decision of the other. I hope that is clear.
I would love this feature, but I strongly advise we don't do it. If we did, tooling support would be inconsistent because the spec says it's a no-no.
There is a construct in JSON schema draft 5 but I don't think we're considering updating to that (@webron?)
Good point. I was just pointing out that if the decision was to do this, I have no objection for my tooling. As for JSON Schema Draft 5, unless it alters the JSON References specification I don't see us being able to support this if we do things the JSON References way. I wonder if we should reach out to the JSON References authors?
I'd also love to be able to override items from a $ref. I'd actually been using them before but now that the swagger editor is flagging them as warnings I'd been reconsidering it.
The warnings are accurate so don't attempt to merge/override properties in a referenced object as it won't work. I would love to see this addressed as well.
Parent meta issue: #579
I haven't checked draft 5, so I don't know if it has effects on JSON References as well (possibly with a new draft to that). I don't think we'll be moving to draft 5 as it's not official (yeah, an official draft).
Breaking away from specs is a challenge. Unlike some expansions to JSON Schema which are actually allowed by JSON Schema itself, we're suggesting here something that goes against the spec of JSON References.
That said, within the spec, the inability to add/override JSON References makes them unusable in some cases and forces people to not DRY or make incomplete documentation.
The real challenge here is not to say we allow it, but try to think of the edge cases that could help users abuse this feature in a way, and causing more problems for the tooling. I'd like to see this change happening, but we need to be careful here.
There is a merge construct which addresses this issue. But I think moving to draft 5 would be a big mistake right now.
I have a team trying to use the swagger editor hosted on swaggers site to edit our API's but with the recent enhancement to the editor to use this update, our descriptions were showing as warnings. We have a very similar issue as @JimSangwine which I can post our actual example but for now we've just cloned an older tag of the swagger editor and are running it locally.
So the reason that I posted is to support this change but to ask for clarification, is @fehguy suggesting that we do not support this because the draft version of the JSON Schema does not support it and the swagger editor was upgraded to use the new draft version?
If i'm following correctly, it's a draft and a reason why this argument should be made to change the specification.
Actually it supported both in Swagger and Draft-4, you just need to use allOf keyword.
So instead of:
description: 'The premises at which electricity is provided.'
$ref": '#/definitions/Address'
You should write:
description: 'The premises at which electricity is provided.'
allOf:
- $ref: '#/definitions/Address'
It standard usage of allOf nothing fancy here, and I think it fully solve this problem.
Thanks @JimSangwine - I also face this issue, indeed I opened this as a question on the JSON Schema Group https://groups.google.com/forum/#!topic/json-schema/YS1dbWXk_4s
@IvanGoncharov - I really like this suggestion. It seems syntactically valid, but functionally unclear. For example - if both elements happen to have a description, which one wins? Furthermore, fields which include this type of reference are dropped from the output of the code generator, so for me, it's not an acceptable 'interim fix' :(
Found out accidentally today that Swagger UI supports and visualizes description within $ref object as in the initial example, whereas Swagger Editor issues a warning
Tackling PR: #741
@IvanGoncharov Regarding the suggestion to use allOf for the purpose, I am not sure if this is the intended use of allOf as has been suggested. allOf intended use does not seem to suggest that it can be used for overriding property values or providing additional "description" property.
Maybe this can be best resolved by the proposed new $use extension of JSON Schema - see https://github.com/json-schema-org/json-schema-spec/issues/98
allOf is pretty much unsupported in most tools.
e.g.
https://github.com/swagger-api/swagger-editor/issues/476
The overarching goal of OpenAPI is to meet the needs around meta data definition of APIs. If a technical choice (e.g. to model references as JSON References) conflicts with those needs it is not the original need that it is at fault but the technical choice, because it is not fit for purpose.
There are many genuine uses for wanting to add meta data at the point of reference. If JSON References cannot meet that need, then Open API should select a different syntactic construct as an alternative to meet that need. This might be the new $use extension of JSON schema as above, or an alternative specific to Open API. For backwards compatibility this would probably be an alternative option rather than a replacement.
Failure to address reasonable community needs will simply cause the community to move elsewhere to projects that can meet their needs.