-
Notifications
You must be signed in to change notification settings - Fork 9k
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
Schema example generation should be straightened. #6470
Comments
One issue is that schema examples may not actually have a parent/child relationship, particularly across contentTypes. So it would be better to replace and not merge. Using the provided definition, it's possible that the server response to |
In case it should be replace logic, shouldn't xml generator also use this logic? |
I think if this is a use case the content-types should be using different schemas to clarify this. |
If all the content-type sample generators would use the json logic this would be the case. Since it will return the example if found on prio 1. |
How about examples that are schema related and not content-type. Should they use merge approach? |
I agree. Unfortunately, we've seen issues where this is not the case. |
I think if content-type matches the schema, the examples should be merged. This would be my general expectation of a consistent api definition. @webron any thoughts on this? |
So from technical side I think it might make sense that if there is an |
Started adding unit tests |
@mathis-m I took another look at the OpenAPI specification regarding the Media Type Object. I was incorrect to state that I should expect a https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.1.0.md
Regarding |
* ref: #6470 * fixes: #6540 * fixes: #4943 * add example override option to json * add example override option to xml * added basic oneOf and anyOf support * fix anyof|oneof * only lift xml to items Co-authored-by: Tim Lai <[email protected]>
Are there any plans to add support for yaml media type examples? Currently examples given as schema objects will render as either json or xml, but to get them to display as yaml I must copy and paste a separate example as a multi-line string. Here is an example spec demonstrating the different behaviours:
Thanks |
@tim-lai I think this would be pretty easy to integrate!
import YAML from "js-yaml"
let yamlString
try {
yamlString = YAML.safeDump(YAML.safeLoad(originalStr), {
lineWidth: -1 // don't generate line folds
})
} catch (e) {
return
} |
re: media type examples... moved to new issue: #6857 |
Background
I have been working on a couple of issues and improvements related to examples of schemas.
The biggest problem is that there are many components that are displaying examples in different ways.
Some of them are using the util
getSampleSchema()
. This is a function that generates sample for a provided schema and content-type. Others just usestringify()
on the examples provided for requestBody or responses.Issues
Inconsistency in components
In my opinion all components that display samples of a schema should be using the utility
getSampleSchema
in order to respect the content-type....
Inconsistency in generation
While creating PR #6456 I noticed that the generation itself is inconsistent. The logic for integrating the
example
key of a schema into the generated sample is not the same forxml
andjson
related content-type.Swagger/OpenAPI definition:
The xml generator function
sampleXmlFromSchema
does:The json generator function
sampleFromSchema
instead:xml generator function clearly does a better job from my side.
In addition I would implement example fall back to closest parent schema's example that contains example value for current schema.
Inconsistency in handling examples
I think it would be a nice enhancement if there would be a utility that allows to parse the example value to json.
With this the
getSampleSchema
could also integrate literal examples within the generation process.For #6442 when using
getSampleSchema
there is the need to override the root schema example with content-type or media-type example. In case a property's schema itself specifies an example, this will not lead to an override, because in case of an example defined in nested schema too it will use the nested for the sample. This is why I would like to introduce an optional parameter togetSampleSchema(..., overrideExample = undefined)
this could be used to override all examples at any given nesting level. Other advantage would be not missing some example values that could be used to complete the sample.Would like to hear some thoughts on this before I start developing.
Open Questions
getSampleSchema
fit the Response examples do not respect media-type #6442 use case with merge and override logic? Or would it be a better approach to just parse the example value to JSON and then stringify it respectingly to the content-type or media type without merging schema sample?The text was updated successfully, but these errors were encountered: