Skip to content

fix(plugin): generate object examples from @example doc tags - #4005

Merged
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
y-hsgw:fix/plugin-object-example
Aug 17, 2026
Merged

fix(plugin): generate object examples from @example doc tags#4005
kamilmysliwiec merged 1 commit into
nestjs:masterfrom
y-hsgw:fix/plugin-object-example

Conversation

@y-hsgw

@y-hsgw y-hsgw commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

With introspectComments enabled, an @example doc tag holding a JSON object is parsed successfully, but createLiteralFromAnyValue only handles arrays and primitives and returns undefined for objects. The resulting exception is swallowed by the property visitor, so the entire property (including required and type) is dropped from _OPENAPI_METADATA_FACTORY:

export class PresignedPostDto {
  /**
   * @example { "Content-Type": "video/mp4", "key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" }
   */
  fields!: Record<string, string>;
}
// generated
static _OPENAPI_METADATA_FACTORY() {
    return {};
}

Issue Number: N/A

What is the new behavior?

createLiteralFromAnyValue now handles plain objects (and null) recursively, emitting object literal expressions with string literal keys so that non-identifier keys such as "Content-Type" work:

// generated
static _OPENAPI_METADATA_FACTORY() {
    return { fields: { required: true, type: "object", additionalProperties: { type: "string" }, example: { "Content-Type": "video/mp4", "key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" } } };
}

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

  • Added coverage in test/plugin/fixtures/create-cat-alt2.dto.ts for an object @example on a Record<string, string> property.

Object values in @example JSDoc tags were parsed successfully but
createLiteralFromAnyValue only handled arrays and primitives, returning
undefined for objects. The resulting exception was swallowed by the
property visitor, dropping the entire property from
_OPENAPI_METADATA_FACTORY. Handle plain objects (and null) by emitting
object literal expressions with string literal keys.
@kamilmysliwiec
kamilmysliwiec merged commit 4a5a4b0 into nestjs:master Aug 17, 2026
1 check passed
@y-hsgw
y-hsgw deleted the fix/plugin-object-example branch August 17, 2026 09:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants