fix/examples: Fix OpenAPI "examples" - #935
Open
gevalo1 wants to merge 3 commits into
Open
Conversation
Author
|
Just for reference, this is what our |
gevalo1
marked this pull request as ready for review
July 29, 2026 13:25
Parameter schemas were converted with schemaToMedia, which only resolves examples at the top level of a schema. An examples array nested inside a parameter schema, for example the items of an array query parameter, was therefore left as it is and ended up in the document, where examples is not a valid Schema Object keyword, making the specification invalid. Parameters now use schemaToMediaRecursive, as request bodies and responses already do.
The Schema Object in OpenAPI 3.1 is a JSON Schema, where examples is an array of values and example is deprecated. The examples array was still rewritten into the OpenAPI 3.0 shape though, so a single example was moved into example and multiple examples were converted into a named examples map on the media or parameter object, which loses values and emits a deprecated keyword. The version is now taken into account, as requested in fastify#625 (comment), so that a document targeting 3.1.x keeps the arrays untouched at every depth, while the 3.0 line keeps its current behaviour. The x-examples field is unchanged and remains the way to document named examples in both versions.
Author
|
Hi @mcollina, @Fdawgs, @climba03003, @Tony133 Would appreciate a review when someone has time, happy to discuss and/or make any requested changes! |
There was a problem hiding this comment.
Pull request overview
Updates OpenAPI example handling to preserve JSON Schema examples arrays in OpenAPI 3.1.
Changes:
- Makes example conversion version-aware.
- Recursively handles nested parameter examples.
- Adds tests and documentation for OpenAPI 3.1 behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
lib/spec/openapi/utils.js |
Implements version-aware example handling. |
test/spec/openapi/option.test.js |
Tests nested and OpenAPI 3.1 examples. |
README.md |
Documents version-specific behavior. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| ```json | ||
| "/": { | ||
| "post": { | ||
| "requestBody": { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this fixes
examplesarrays are always rewritten into the OpenAPI 3.0 shape, even when thedocument declares
openapi: 3.1.0.In 3.0 that rewrite is correct and necessary: the Schema Object there is a subset
of JSON Schema draft 4 and only has a singular
example, whileexamplesis amap of Example Objects that lives on the Media Type or Parameter Object. So a
single example is moved into
example, and multiple examples become a named mapone level up.
In 3.1 the Schema Object is JSON Schema 2020-12.
examplesis a plain array,valid at any depth, and
exampleis deprecated. Running the 3.0 rewrite against a3.1 document therefore is not correct:
examples[0]survivesexample anything under
anyOf/oneOf) keep their arrays, so one document endsup mixing both styles
This caused issues for us: we generate a 3.1 document for our REST docs and
were using a
pnpm patchfor this plugin to get a valid OpenAPI schema.What I changed
The example handling now takes the target version into account. For
3.1.xthearrays are left exactly as written, at every depth, and nothing is hoisted. Every
other version keeps the current behaviour, unchanged and untouched.
There is no new plugin option. The version already selects this, which matches what was asked for in #625:
x-examplesis deliberately untouched and keeps producing the named Example Objectmap in both versions, so the use case from #616 and #770 still works. That remains
the way to attach names, summaries and descriptions to examples.
The first commit is separate
While testing we found that parameter schemas were converted with
schemaToMediainstead of
schemaToMediaRecursive, so nestedexamplesarrays inside a parameterwere never resolved. An array query parameter like this:
Checklist
npm run test && npm run benchmark --if-presentand the Code of conduct