-
Notifications
You must be signed in to change notification settings - Fork 78
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
Add "example" to openapi schemas output (via schemars) #325
Add "example" to openapi schemas output (via schemars) #325
Conversation
Neat! @ahl can you take a look at this one? @bahamas10 What do you think about folding this example into |
I renamed the example file and also added some comments to better help someone following along with 1. what the file does and 2. what they should expect. I chose to rename the example as opposed to folding it into the basic example to leave in the nested struct example without possibly over-complicating |
This is great; thank you for contributing it. I wondered if you gave any consideration to validating the example. I could imagine trying to deserialize from the example into the rust type... but that seems like kinds of a pain in the neck, and our only recourse would be a runtime failure. Can you please add an entry to the changelog along the lines of "the example field (if present) for |
@ahl I'll push the changelog change up, but one quick question for validation. Can you explain what you mean by validating the example? As it is currently, the Is this the validation you are talking about, or do you mean something else? |
That's what I was thinking: take the |
This PR makes it so schemas defined with an example (via schemars) are passed-through to the generated OpenAPI JSON.
Currently, specifying an example using schemars will be lost when the OpenAPI JSON is generated by dropshot. This PR ensures any examples specified are passed along to the resultant JSON.
In the OpenAPI spec (
v3.0.3
) it is possible to specify anexample
key inside most objects (see spec).schemars
has supported examples by using an example function since versionv0.7.6
(see changelog).This PR is completely "opt-in" - if a user does not specify an example then their generated JSON should look the same as it has before this PR.
To specify an example you need to tell schemars how to find the function that generates an example struct:
The schema created for
Student
will look something like:(truncated for this example)
An example of this is included in this PR in the
example.rs
:This code passes:
And both of these work (and pass) as expected: