Skip to content

fix(openapi3): Fix duplicate type name error for unreachable derived models with visibility properties - #11427

Merged
iscai-msft merged 3 commits into
mainfrom
copilot/fix-duplicate-type-name-error
Jul 30, 2026
Merged

fix(openapi3): Fix duplicate type name error for unreachable derived models with visibility properties#11427
iscai-msft merged 3 commits into
mainfrom
copilot/fix-duplicate-type-name-error

Conversation

Copilot AI commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

When a model with @visibility(Lifecycle.Create, Lifecycle.Update) properties extends another model, and both are unreachable from HTTP operations, the OpenAPI3 emitter throws Duplicate type name: 'Account'.

Root Cause

Two code paths independently emit the derived model with different visibility contexts, bypassing the TypeEmitter deduplication cache:

  1. processUnreferencedSchemas emits Account with Visibility.All context (correct for unreachable types).
  2. modelDeclaration emits Account as a derived model inheriting the parent's reduced context (Visibility.Read — because Object has no transformed properties, so reduceContext downgrades it).

isTransformed(Account, All) = true (since item_id is present in All but absent in Read), so path 1 keeps All while path 2 produces Read. Two distinct cache keys → two #createDeclaration calls → same name → error.

Fix

In modelDeclaration, emit unreachable derived models with an explicit { visibility: Visibility.All } referenceContext, matching what processUnreferencedSchemas uses. This makes both paths produce the same context key — the TypeEmitter cache deduplicates on the second call.

model Object {}

model Account extends Object {
  @visibility(Lifecycle.Create, Lifecycle.Update)
  item_id: string;
}
// Previously: Duplicate type name: 'Account'
// Now: emits correctly as a single schema

Files changed:

  • packages/openapi3/src/schema-emitter.ts — check isUnreachable per derived model in modelDeclaration loop; if unreachable, force Visibility.All referenceContext
  • packages/openapi3/test/metadata.test.ts — add regression test for this case

@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.

Copilot AI linked an issue Jul 28, 2026 that may be closed by this pull request
4 tasks
…with visibility properties

When a model with @visibility(Lifecycle.Create, Lifecycle.Update) properties
extends another model and both are unreachable from HTTP operations,
processUnreferencedSchemas would emit the derived model with Visibility.All context,
while modelDeclaration would also emit it by inheriting the parent's reduced Read context.
These two different contexts resulted in two schema declarations with the same name,
causing a 'Duplicate type name' error.

Fix: Emit unreachable derived models with explicit Visibility.All context in
modelDeclaration to match the context used by processUnreferencedSchemas, ensuring
the TypeEmitter cache deduplication works correctly.

Fixes #9727

Co-authored-by: timotheeguerin <1031227+timotheeguerin@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix duplicate type name issue in model definitions fix(openapi3): Fix duplicate type name error for unreachable derived models with visibility properties Jul 28, 2026
@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:openapi3 Issues for @typespec/openapi3 emitter label Jul 28, 2026
Copilot AI requested a review from timotheeguerin July 28, 2026 19:06
@pkg-pr-new

pkg-pr-new Bot commented Jul 29, 2026

Copy link
Copy Markdown

Open in StackBlitz

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

commit: 840ac0a

@timotheeguerin
timotheeguerin marked this pull request as ready for review July 29, 2026 15:39
@github-actions

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • @typespec/openapi3
Show changes

@typespec/openapi3 - fix ✏️

Fix duplicate type name error when a model with a @visibility(Lifecycle.Create, Lifecycle.Update) property extends another model.

@azure-sdk-automation

Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

@iscai-msft
iscai-msft added this pull request to the merge queue Jul 30, 2026
Merged via the queue into main with commit ee686ab Jul 30, 2026
33 checks passed
@iscai-msft
iscai-msft deleted the copilot/fix-duplicate-type-name-error branch July 30, 2026 16:17
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.

Can not derive model properties

3 participants