fix(response-object-factory): preserve example/examples for built-in scalar response types#3883
Merged
kamilmysliwiec merged 2 commits intoApr 30, 2026
Conversation
…scalar response types
When a response decorator is configured with a built-in scalar/array
type and an example payload, e.g.
@ApiOkResponse({ type: Number, example: 42 })
ResponseObjectFactory#create stripped `example` and `examples` from
the response via `omit(response, exampleKeys)` but never re-attached
them to the generated media-type entry. As a result the example
silently disappeared from the OpenAPI document.
For non-built-in (DTO) types and array-of-DTO responses, the
ResponseObjectMapper already pipes `example`/`examples` into the
media-type wrapper via `pick(response, exampleKeys)`. Apply the same
treatment to the built-in-type branch so that scalar (and array of
scalar) responses keep their examples.
Member
|
Could you update e2e tests as well? |
…calar responses Adds three new cats-controller endpoints exercising the bug fixed in this PR: a scalar @apiresponse with a single example, a scalar @apiresponse with named examples, and an isArray scalar response with example. The new validate-schema e2e check confirms each example/examples entry lands inside content[application/json] and is not left behind at the response top level.
Contributor
Author
|
@kamilmysliwiec done — pushed 1f305df which adds e2e coverage for this fix. Three new endpoints in `e2e/src/cats/cats.controller.ts`:
A new test in `e2e/validate-schema.e2e-spec.ts` (`should preserve example/examples for built-in scalar response types`) asserts:
`npm run test:e2e -- validate-schema -t "preserve example"` → 2/2 passing on the new test plus the existing #3335 sibling. |
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 kind of change does this PR introduce?
Bug fix.
What is the current behavior?
When a response decorator is configured with a built-in scalar (or array of scalar) type and an example payload, the example is silently dropped from the generated OpenAPI document.
The generated 200 response has no example:
{ "description": "", "content": { "application/json": { "schema": { "type": "number" } } } }ResponseObjectFactory#createstripsexampleandexamplesfrom the response viaomit(response, exampleKeys)for the built-in-type branch, but it never re-attaches them to the generated media-type entry. The non-built-in (DTO) branch — handled byResponseObjectMapper— already does the right thing viapick(response, exampleKeys)when it constructs the media type. The built-in-type branch is the outlier.What is the new behavior?
example/examplesare now forwarded toMimetypeContentWrapper.wrapfor both the scalar and array-of-scalar built-in-type code paths, mirroring the DTO behaviour. The example ends up next toschemainside eachcontent[<mimetype>]entry, as required by the OpenAPI spec, and is no longer left behind at the response top level.{ "description": "", "content": { "application/json": { "schema": { "type": "number" }, "example": 42 } } }Additional context (optional)
Added a focused unit test for
ResponseObjectFactorycovering scalarexample, scalarexamples, theisArray: truecodepath, and a guard thatexampledoes not leak back to the top-level response object. The fullswagger-explorerandresponse-object-mappersuites continue to pass (63 tests).