PropertyDeserializer fails to add vendor extensions to RefProperty objects
There are several issues reported across multiple swagger repositories regarding this aspect. Since I cannot reference issues from other repositories here I will just leave some links.
- https://github.com/swagger-api/swagger-codegen/issues/2219
- https://github.com/swagger-api/swagger-codegen/issues/2172
- https://github.com/OAI/OpenAPI-Specification/issues/241
Example:
definitions:
MyObject:
type: object
properties:
otherObject:
$ref: '#/definitions/OtherObject'
x-foo-extension: "bar"
OtherObject:
type: object
properies:
...
For this example, the x-foo-extension placed on the otherObject property is ignored.
That's because you can't add anything at the same level as a $ref...
@webron Strictly from technical point of view, I think you can.
If you add vendor extensions to the RefProperty object in PropertyDeserializer.propertyFromNode(...), line 214, they are propagated to the swagger-codegen module and from there to the templates.
Although this seems to work from a technical point of view, I'm not sure if it's in line with the philosophy behind the JSON Reference standard.
I will contribute a pull request tomorrow morning when I get back to my office.
You're right--it is technically possible but outside the standard, and we're discouraging going outside the standard by not supporting it.
If you break the standard then other tools won't work and everyone will be sad.
Thanks @fehguy! I was afraid that was the reason behind it, since it was already mentioned in a couple of other issues. Under these conditions, the pull request will never be accepted, right? The problem is that our main use case revolves around us being able to add custom vendor extensions to all the fields of an object, including those that reference other objects. Since this is a limitation imposed by the JSON Reference standard, I guess there's no way for us to achieve our goal. Probably our last resort would be to have all object declarations inline, but this comes with it's own set of issues (the Spring MVC generator doesn't work properly in that case and the resulting specification file is hard to maintain). Can you see any other workarounds for this?
There are ugly workarounds such as:
otherObject:
allOf:
- $ref: '#/definitions/OtherObject'
- x-foo-extension: "bar"
or
otherObject:
allOf:
- $ref: '#/definitions/OtherObject'
x-foo-extension: "bar"
I would avoid inlining objects.
As all the issues above mention there are genuine use cases when you need to attach vendor extensions to the original reference, rather than the referenced one. My particular one relates to versioning. I want to express meta data regarding at which version a parent object definition gained a property whose value is a child object definition. This genuinely a concern of the parent only and should not pollute the child definition. Also with multiple parent objects referring to the same child it is not appropriate to push all of these down into the child definition.
The fundamental problem is the modelling decision of Swagger to model these as JSON References because JSON References do not have any identity beyond the thing they refer to. They are simply a shorthand for textual inclusion.
As this shows the root of the problem is the Swagger modelling decision to model these as JSON references. My proposal would be to also allow a different JSON syntactic construct to represent references in cases that have this need. That would allow those with these needs to stay within the Swagger community rather than being forced to move to other tools or fork.
Only the parsing phase of the tool chain would change, downstream data representations could use a common object to represent both kinds of references.
The JSON Reference standard is also in draft status and has never been approved.
@nickreevesuk not true. JSON schema decided that siblings to $ref are not allowed. Sure, the Swagger format could have deviated and said "that's not the case for us" but then pretty much no tooling would support it, period. That seemed like a bad idea.
It is relatively easy to say that a feature of JSON schema is not supported by the format. But to break a primary construct of it would not be wise, regardless of technical ease of implementation. Hence, it's not supported.
Please feel free to share your views on this in the OAI Specification which is the swagger specification under the Linux foundation. You are not raising a tooling concern but a pure spec concern.
I'm closing this out--not because it isn't a valid discussion, it's just not valid in this repository. Ignoring the additional extension as the OP mentioned is per design of the swagger-core library, which does it's very best to follow the spec.