-
-
Notifications
You must be signed in to change notification settings - Fork 210
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
fix: nested examples in OpenAPI schemas for request body & response #772
Conversation
}) | ||
|
||
test('uses examples if has property required in body', async (t) => { | ||
test('marks request body as required', async (t) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test had a wrong description. I fixed it since it mentions examples, but doesn't test anything related to them.
Originally introduced in #541
test('transforms examples in example if single string example', async (t) => { | ||
t.plan(2) | ||
const fastify = Fastify() | ||
test('move examples from "x-examples" to examples field', async (t) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
x-examples
behavior & test are unchanged, this is just how the diff shows.
Apologies for changes after I already requested a review, but I realized there was a better implementation. My initial solution, that you can find in the earlier commits, made changes to
New solution, even though more LoC, has several advantages:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
@mcollina When would be a good time to merge this? |
Now ;) |
@mcollina Sorry for bothering you, but 8.14 didn't get published after merging. Not sure if it's a CI issue or needs to be done manually. |
Summary
Fixes #771
This PR addresses the following issue — example values in nested schemas of request body and response are not getting transformed into OpenAPI compliant format. For example, fastify schema below is not being processed correctly. Specifically
.examples
field is currently preserved as is on any nested properties, even though array is not a valid format for OpenAPI examples.OpenAPI spec for example values
I will spell out my current understanding of the standard in case it will help someone in the future.
example: <value>
examples: {<value_name>: { value: <value>, ... }}
- official documentation.examples
is not supported at the top level of.schema
objects, in.properties
of objects and.items
of arrays. This means those are only allowed to have a single example..examples
in cases I just outlined.examples
field outside ofschema
— this behavior is already implemented and covered by the test suite.Change description
Code
schemaTypeToNestedSchemas
strategy that allows to extract all sub-schemas for different schema types.resolveSchemaExamples
andresolveSchemaExamplesRecursive
functions that only take a single example (orx-examples
unchanged).schemaToMediaRecursive
function that works the same asschemaToMedia
, but also recursively resolves examples inside the schema.resolveBodyParams
andresolveResponse
to recursive implementation.Tests
Overall, I found current test coverage for example values very lacking and introduced much more extensive set of tests. Because these tests are very repetitive I had to decide whether to generalize them or not. I opted for explicit repetitive testing of each combination: single/multiple example values, parameters/body/response, shallow/deeply nested schemas, strings/numbers/objects/arrays. I added some grouping to the tests and believe explicitly testing each use case is the most maintainable approach within the current testing setup, but imho using
fast-check
would definitely help with repetitiveness and test quality.The combined test file diff on this PR is a bit of a spaghetti monster, so I broke it down into two commits that are much easier to review separately.
Commit 1 - removed some tests:
Commit 2 - added 30 explicit tests for each significant combination of configuration
Documentation
Checklist
npm run test
andnpm run benchmark
and the Code of conduct