Skip to content

fix(openapi3): reduce visibility context of array declarations - #12300

Open
Hardik K (hardikkaurani) wants to merge 1 commit into
microsoft:mainfrom
hardikkaurani:fix/openapi3-array-declaration-visibility-context
Open

Hardik K (hardikkaurani) wants to merge 1 commit into
microsoft:mainfrom
hardikkaurani:fix/openapi3-array-declaration-visibility-context

Conversation

@hardikkaurani

Copy link
Copy Markdown

Fixes #12198

Problem

When a named array model (e.g. model Range is int32[][]) is referenced in both a collection operation (Foo[]) and direct operation (Foo), it is emitted under a suffixed name (such as RangeItem) instead of its canonical declaration name.

In @typespec/openapi3, SchemaEmitter overrides modelDeclarationReferenceContext, modelInstantiationReferenceContext, scalarDeclarationReferenceContext, enumDeclarationReferenceContext, and unionDeclarationReferenceContext, all of which call reduceContext so that a reference to a type not transformed by visibility collapses back to the declaration's context.

However, arrayDeclarationReferenceContext was not overridden in SchemaEmitter. When the array model is reached through an array item context (Foo[] with Visibility.Item), the declaration retained Visibility.Item. Because getUsage() reports multiple visibility states, #createDeclaration appends an Item suffix, resulting in RangeItem.

Fix

Override arrayDeclarationReferenceContext in OpenAPI3SchemaEmitterBase to call reduceContext(array). When isTransformed is false, reduceContext collapses the visibility back, avoiding unnecessary suffixed declarations for named array models.

The schema output for the reproduction in #12198 now correctly emits Foo and Range without an artificial RangeItem suffix.

Tests

Added named array model referenced by collection and direct operations is emitted once to packages/openapi3/test/metadata.test.ts.

  • Verified failure on main before fix: schemas produced Foo, RangeItem.
  • Verified passing with fix: schemas produced Foo, Range with property $ref: "#/components/schemas/Range".
  • Verified test suite passes (79 tests in metadata.test.ts), and prettier --check + oxlint are clean.
  • Added Chronus change file .chronus/changes/fix-openapi3-array-declaration-visibility-context.md.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@hardikkaurani

Copy link
Copy Markdown
Author

Hello Timothee Guerin (@timotheeguerin),

I have submitted this pull request to resolve issue #12198 regarding named array models receiving unexpected visibility suffixes (such as RangeItem) when referenced in collection operations.

Following the pattern established for other declaration kinds and template instantiations, this PR overrides arrayDeclarationReferenceContext to call reduceContext(array), collapsing the visibility context back to canonical representation when the type is not transformed.

All regression tests, prettier formatting, and oxlint checks pass cleanly, and a Chronus changelog entry has been included. Please review when convenient. Thank you for your time.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The focused fix follows existing context-reduction behavior, includes regression coverage, and has no identified blocking issues.

No open issues 🎉

What changed in this PR

Fixes #12198 by preserving canonical names for named array schemas referenced through both collection and direct operations.

Changes:

  • Applies existing visibility-context reduction to array declarations.
  • Adds regression coverage for schema names and references.
  • Adds an OpenAPI3 fix changelog entry.
File Description
packages/​openapi3/​test/​metadata.test.ts Verifies canonical array schema naming and references.
packages/​openapi3/​src/​schema-emitter.ts Reduces array declaration reference context.
.chronus/​changes/​fix-openapi3-array-declaration-visibility-context.md Documents the naming fix.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:openapi3 Issues for @typespec/openapi3 emitter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Quirk while defining openapi schemas

2 participants