-
Notifications
You must be signed in to change notification settings - Fork 9.1k
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
Description is not allowed for elements containing $ref #1514
Comments
There are few points regarding this:
|
Related (or duplicate): #556 |
In OpenAPI, components schemas can be declared independently, and then referred to to avoid duplication. However, when referencing an existing schema, no other information can be provided. This means that a field of a struct whose type has its own schema must use a '$ref', but then cannot specify anything else, especially no default value, no description, no constraints... This is a pretty bad overlook from the OpenAPI spec: �ihttps://github.com/OAI/OpenAPI-Specification/issues/1514 The proposed workaround is to use an untyped schema with all the details, and push the $ref inside an allOf clause, on its own. This is what is done in this commit. As you can see, this is pretty bad semantically, and I can't really imagine expecting a client to properly understand what it means, but that's the solution... Change-Id: Ifdba5fe6a7a2494aa136ec3ea3f0952edd45d6d0 rip-it: 0c86011
I was looking for same use case where I want to add description for the $ref field in swagger. Above answer helped, when we add description with allOf, swagger-ui picks up this description. Now my problem is how to generate allOf programmatically? Is there any annotation that can be used to generate allOf. I am using swagger-maven-plugin with @apimodel and @ApiModelProperty to generate swagger from APIs. |
See OAI/OpenAPI-Specification#1514 The openapi parser raises an error when this happens
This looks like a serious design flaw in both OpenAPI and JSON Schema. |
This part of the comment appears to be out-of-date, it seems? The version of the JSON Schema Core used by 3.1.0-RC1 does not seem to explicitly disallow additional properties being beside Additionally, within the changelog for that version of JSON Schema Core, there is this note:
which would also support that keywords can be used next to the If I might suggest something -- perhaps specification extensions could be allowed next to If this is off-topic for this issue, I'd be happy to open a new issue regarding this. |
Nice research @LikeLakers2, I think it worth opening a new issue as I believe OpenAPI must eventually move towards a graceful resolution of this problem. Also you write
This page explicitly says that any such properties will be just ignored. In what way does it disallow additional properties? |
Bad wording on my part. Woops. Anyways, I'll open an issue soon. And I'll make sure to use the right wording this time, haha. |
In OpenAPI 3.1 you can use any JSON Schema property alongside a $ref in a schema object. However, $ref used in other OAS objects like parameter, requestBody, can only have summary and description as peers. JSON Schema has done the hard work to clearly define how peer properties interact with $ref. In OpenAPI we are going to have to define the semantics of peer properties on a case by case basis. We have allowed summary and description as they don't impact behavior. We are open to considering other properties but we will need to do that carefully. Regarding the swagger.io doc, I believe that wording is incorrect. in 3.0 of OAS you are not allowed to include properties alongside $ref, they would cause a validation error. |
@darrelmiller That... feels confusing. Why do we have two different definitions for Also,
If the swagger.io doc is incorrect, then the specification itself should be updated. In the Reference Object documentation for both v3.0.3 and v3.1.0-RC1, and even on v3.1.0 on the development branch (as of the time of this comment), the following text is found regarding additional properties:
The use of the word "ignore" here would imply that additional properties (including specification extensions) can be in the object, but will never be parsed. If that isn't what is meant, then the wording in the specification should be fixed. |
My mistake. I thought the spec explicitly disallowed those properties.
We have the OAS Reference object and JSON schema has a $ref object. We use the same keyword because they have approximately the same behavior. This is the impact of enabling full JSON Schema support. JSON Schema is able to identify every property as either an annotation or a constraint. It has defined how annotations and constraints can be combined. Every property in JSON schema can be evaluated independently. In OAS the relationships between properties is more complex. Allowing arbitrary inclusion of peer properties to a $ref could create some very confusing situations. e.g. Here's just one example
|
The wording is admittedly a bit vague -- I can see why it might trip you up, even if you knew it said that. It says "cannot be extended" only to say that additional properties will "be ignored". "Cannot be extended" would imply that you are disallowed from even including them, whereas "be ignored" would imply that you can include them if you don't mind the parser ignoring them. However, both of them being in reference to how an object can be structured, means that the actual meaning might be vague -- and parsers will probably take the least restrictive route, just-in-case. So I think it may be worth clarifying this wording anyways... maybe replace the line with something like "Any additional properties (including specification extensions) SHALL be ignored."
Ah, I think I understand the difference now. Thanks. I can see why that might be hard to deal with. Is there any active issue regarding trying to figure that out? As an aside, it may be worth clarifying that |
I have a question about wording here:
Does it mean that if the schema object by the reference does not have a description but the Reference object has description, then the "resulting" object will not anyway have description? If so, why? Why can't it just ADD-OR-OVERRIDE the field? |
@greatvovan OAS Reference Objects are no longer used by Schema Objects in OAS. The comment quoted above is only used for other components. e.g If you $ref a We don't want to be in a position where allowing a property in a |
Got it. Maybe rephrase it then as "does not allow" instead of "does not define"? |
Might be an improvement. PR welcome! |
@darrelmiller I saw you gave a thumbs up to my comment -- do you want me to open a PR to make those suggested changes? |
@webron thanks for mentioning this. It is not clear from this issue what the solution and outcome was. it is not also clear for me by just looking at current spec of 3.1 (https://spec.openapis.org/oas/v3.1.0) how this is solved. I can only see there is a "Reference Object" that can contain for example given following sample: components:
schemas:
SomeSchemaDef:
type: object
properties:
someProp:
$ref: '#/components/schemas/SomeOtherSchemaDef'
# here I would like to add a `description` for `someProp` what is the solution that OpenAPI 3.1 provides? |
@webron Hi - what is the solution in 3.1? |
@blakew you can use |
Within schemas you can only use $ref and not reference objects. The docs note this distinction:
I'm particularly interested in a solution for including descriptions within schemas. |
Correct. Technically you can create and use your own JSON Schema vocabulary that will also allow it in schemas as well. |
@blakew The JSON Schema dialect used in OpenAPI 3.1 is based on JSON Schema 2020-12 which does allow you to put a |
I am not sure I understand the difference between object reference and object schema. Can I include custom properties - extensions - next to the $ref? For example: components: schemas: SomeSchemaDef: type: object properties: someProp: $ref: '#/components/schemas/SomeOtherSchemaDef' # here I would like to add 'x-my-extension' for 'someProp' |
A "Schema Object" is what the documentation calls any part of an OpenAPI document that represents a JSON Schema such as So, in your example, yes, you can add any additional keywords you like next to |
I didn't follow this tread, it got fragmented quickly. What's the bottom line for:
Is there a workaround? |
@michaelcilibrasi your example works in OpenAPI 3.1. In earlier versions, you need to wrap the location:
description: User home address location <- error
allOf:
- $ref: "#/components/schemas/Location" |
hkosova, I forgot to thank you for your response. |
It would be very handy so if we can prepare some defined structure, lets say a responseBody, and then in each API request, |
Fixes smithy-lang#2400. When a member targets a structure, it becomes a schema reference when converted to JSON Schema. Previously, we didn't add member docs to the converted object, possibly because earlier versions of open api or JSON Schema did not support it. Reading through [this issue](OAI/OpenAPI-Specification#1514) the [OAI spec](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#reference-object), and the [JSON Schema Spec](https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.3), it seems that OpenAPI 3.1 and JSON Schema 2020-12 support the `description` property alongside `$ref`. I wasn't able to find anything about whether it is supported in [JSON Schema 07](https://json-schema.org/draft-07/json-schema-release-notes). This commit adds a new config option, `addReferenceDescriptions` that will add the `description` property alongside `$ref` when the member has documentation. I made it opt-in through the config option so we don't cause any existing documentation to be changed unexpectedly.
Fixes smithy-lang#2400. When a member targets a structure, it becomes a schema reference when converted to JSON Schema. Previously, we didn't add member docs to the converted object, possibly because earlier versions of open api or JSON Schema did not support it. Reading through [this issue](OAI/OpenAPI-Specification#1514) the [OAI spec](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#reference-object), and the [JSON Schema Spec](https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.3), it seems that OpenAPI 3.1 and JSON Schema 2020-12 support the `description` property alongside `$ref`. I wasn't able to find anything about whether it is supported in [JSON Schema 07](https://json-schema.org/draft-07/json-schema-release-notes). This commit adds a new config option, `addReferenceDescriptions` that will add the `description` property alongside `$ref` when the member has documentation. I made it opt-in through the config option so we don't cause any existing documentation to be changed unexpectedly.
Fixes #2400. When a member targets a structure, it becomes a schema reference when converted to JSON Schema. Previously, we didn't add member docs to the converted object, possibly because earlier versions of open api or JSON Schema did not support it. Reading through [this issue](OAI/OpenAPI-Specification#1514) the [OAI spec](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#reference-object), and the [JSON Schema Spec](https://json-schema.org/draft/2020-12/json-schema-core#section-8.2.3), it seems that OpenAPI 3.1 and JSON Schema 2020-12 support the `description` property alongside `$ref`. I wasn't able to find anything about whether it is supported in [JSON Schema 07](https://json-schema.org/draft-07/json-schema-release-notes). This commit adds a new config option, `addReferenceDescriptions` that will add the `description` property alongside `$ref` when the member has documentation. I made it opt-in through the config option so we don't cause any existing documentation to be changed unexpectedly.
I know that specification says that when using $ref any other elements are ignored.
So here a real life scenario.
So if i have same structure of location but different semantics, only way to communicate this would be through field names, but that is very limiting.
Also two locations could be created with identical structure, just to distinguish between semantics, but this is code duplication.
I understand that this is not a trivial issue, but this is obvious shortcoming in spec which i noticed after just couple of days of basic real life usage... And i'm not the only one.
There are people with the same problem here
swagger-api/swagger-editor#1184 (comment)
The text was updated successfully, but these errors were encountered: