Skip to content

fix(openapi3): preserve @tagMetadata tag declaration order in emitted OpenAPI#10770

Open
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-operation-order-preservation
Open

fix(openapi3): preserve @tagMetadata tag declaration order in emitted OpenAPI#10770
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-operation-order-preservation

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 22, 2026

When @tagMetadata is applied, tags were emitted in reverse declaration order because TypeSpec decorators execute bottom-up, causing the metadata map to accumulate entries in reverse.

Changes

  • packages/openapi3/src/openapi.ts: In resolveDocumentTags, reverse Object.entries(metadata) before iteration so emitted tags match TypeSpec source declaration order.
  • packages/openapi3/test/tagmetadata.test.ts: Add explicit ordering tests; update existing parent-field tests whose expected values reflected the old reversed order. Added tests covering:
    • Ordering preservation across multiple @tagMetadata decorators
    • Operation-level tag not defined in @tagMetadata — inserted before tagMetadata-defined tags
    • Operation-level tag also defined in @tagMetadata — emitted once in its tagMetadata-declared position (not duplicated)

Example

@service
@tagMetadata("First", #{description: "First tag"})
@tagMetadata("Second", #{description: "Second tag"})
@tagMetadata("Third", #{description: "Third tag"})
namespace PetStore {}

Before: tags emitted as Third → Second → First
After: tags emitted as First → Second → Third (matching declaration order)

Tag insertion behavior

  • Tags used only at the operation level (via @tag) and not declared with @tagMetadata appear first in the output, in the order they are encountered across operations.
  • Tags declared with @tagMetadata follow, in TypeSpec source declaration order.
  • A tag used at both the operation level and in @tagMetadata is emitted exactly once, at its @tagMetadata-declared position with its metadata.

@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:openapi3 Issues for @typespec/openapi3 emitter label May 22, 2026
Copilot AI changed the title [WIP] Fix operation order preservation when using @tagMetadata decorator fix(openapi3): preserve @tagMetadata tag declaration order in emitted OpenAPI May 22, 2026
Copilot AI requested a review from baywet May 22, 2026 14:05
Copilot finished work on behalf of baywet May 22, 2026 14:05
@baywet
Copy link
Copy Markdown
Member

baywet commented May 22, 2026

@copilot add multiple test cases if we don't have them already: one where a tag is used at the operation level, but not defined at the tagMetadata/service level to see where it "inserts" itself. one where a tag is used at the operation level, and defined at the tagMetadata/service level to see where it "inserts" itself.

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 22, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/openapi3@10770

commit: 76ffa16

Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 22, 2026

@copilot add multiple test cases if we don't have them already: one where a tag is used at the operation level, but not defined at the tagMetadata/service level to see where it "inserts" itself. one w...

Added two new tests in commit 76ffa16:

  1. "operation-level tag not defined in @tagMetadata is inserted before tagMetadata tags" — a tag used only via @tag on an operation (not in @tagMetadata) appears first, followed by the @tagMetadata-defined tags.

  2. "operation-level tag also defined in @tagMetadata is emitted once in tagMetadata position" — a tag used via @tag on an operation that is also declared with @tagMetadata is not duplicated; it appears exactly once in its @tagMetadata-declared position (with its metadata).

Copilot finished work on behalf of baywet May 22, 2026 14:23
@baywet baywet marked this pull request as ready for review May 22, 2026 14:33
@baywet baywet requested a review from johanste May 22, 2026 14:34
@baywet baywet enabled auto-merge May 22, 2026 14:34
@azure-sdk-automation
Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

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]: Operation order is not preserved when @tagMetadata decorator is used

2 participants