Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/openapi3"
---

Fix duplicate type name error when a model with a `@visibility(Lifecycle.Create, Lifecycle.Update)` property extends another model.
8 changes: 7 additions & 1 deletion packages/openapi3/src/schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,13 @@ export class OpenAPI3SchemaEmitterBase<
const derivedModels = model.derivedModels.filter(includeDerivedModel);
// getSchemaOrRef on all children to push them into components.schemas
for (const child of derivedModels) {
this.emitter.emitTypeReference(child);
if (this._visibilityUsage.isUnreachable(child)) {
// Unreachable derived models will be emitted by processUnreferencedSchemas with
// Visibility.All context. Force the same context here to avoid a duplicate declaration.
this.emitter.emitTypeReference(child, { referenceContext: { visibility: Visibility.All } });
} else {
this.emitter.emitTypeReference(child);
}
}

this.applyDiscriminator(model, schema as any);
Expand Down
27 changes: 27 additions & 0 deletions packages/openapi3/test/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,33 @@ worksFor(supportedVersions, ({ openApiFor }) => {
required: ["name"],
});
});

it("inheritance tree unreachable with @visibility(Create, Update) property doesn't get conflicts", async () => {
const res = await openApiFor(`
model Base {
}

model Child extends Base {
@visibility(Lifecycle.Create, Lifecycle.Update) name: string;
}
`);

deepStrictEqual(Object.keys(res.components.schemas), ["Base", "Child"]);
deepStrictEqual(res.components.schemas.Child, {
type: "object",
allOf: [
{
$ref: "#/components/schemas/Base",
},
],
properties: {
name: {
type: "string",
},
},
required: ["name"],
});
});
});

worksFor(["3.0.0"], ({ openApiFor }) => {
Expand Down
Loading