Conversation
…objects Fixes fastify#878 Signed-off-by: Liang Xu <lx3133584@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Extends OpenAPI example conversion to preserve Example Object metadata and custom keys.
Changes:
- Detects Example Objects using
valueorexternalValue. - Uses
name,id, or generated keys. - Adds request-body coverage for metadata preservation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
lib/spec/openapi/utils.js |
Adds Example Object conversion logic. |
test/spec/openapi/option.test.js |
Tests metadata and custom example keys. |
Suppressed comments (3)
lib/spec/openapi/utils.js:125
- This shape check breaks backward compatibility for ordinary object payloads that happen to contain
valueorexternalValue. For example, an object schema withexamples: [{ value: 42 }, { value: 43 }]previously emitted those objects as the example values; it now emits42and43, changing the payload and potentially making it violate its schema. A schema example can legally use any property names, so Example Objects need an explicit, non-colliding opt-in (or a separate input field) rather than being inferred solely from payload keys.
if ('value' in example || 'externalValue' in example) {
lib/spec/openapi/utils.js:125
- The new Example Object handling only runs when there are multiple examples.
schemaToMediasends a one-element array directly tomedia.example, so[{ name: 'first', summary: '...', value: payload }]is still emitted as the raw payload{ name, summary, value }instead of anexamplesmap and loses the intended Example Object semantics. Route a single detected Example Object through this converter as well and cover that case.
if ('value' in example || 'externalValue' in example) {
lib/spec/openapi/utils.js:128
- A custom name of
__proto__invokes the accumulator object's prototype setter instead of creating an examples-map entry, so that example disappears from serialized output and its object becomes the accumulator prototype. Define an own property explicitly (or use a prototype-free map) for user-controlled keys.
examplesObject[name] = exampleObj
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if (typeof example === 'object' && example !== null) { | ||
| if ('value' in example || 'externalValue' in example) { | ||
| const name = example.name || example.id || ('example' + (index + 1)) | ||
| const { name: _name, id: _id, ...exampleObj } = example | ||
| examplesObject[name] = exampleObj | ||
| } else { | ||
| examplesObject['example' + (index + 1)] = { value: example } | ||
| } | ||
| } else { | ||
| examplesObject[example] = { value: example } | ||
| } |
|
Thanks for the PR. Closing because detecting Example Objects by the presence of Note that named examples with |
Problem
When
examplesare specified on OpenAPI 3.0 schema definitions,convertExamplesArrayToObjectunconditionally generated auto-incrementing keys (example1,example2) and wrapped the entire object under{ value: ... }. This made it impossible for users to specify custom example names/IDs or attachsummary,description, orexternalValueattributes as supported by the OpenAPI 3.0 Example Object specification.Solution
convertExamplesArrayToObjectto inspect if an example element is an OpenAPI 3.0 Example Object (containingvalueorexternalValue).summary,description, and other Example Object properties, usingexample.nameorexample.idas the key (falling back toexampleN).Testing
test/spec/openapi/option.test.jsverifying preservation of summary, description, and custom example names/IDs.