Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark discriminator properties consistently as requiered #4663

Merged
merged 6 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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"
---

Introducing a fix so that all discriminator properties are marked as required.
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 7 additions & 0 deletions packages/openapi3/src/schema-emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter<
}
}

const discriminator = getDiscriminator(this.emitter.getProgram(), model);
if (discriminator) {
if (!requiredProps.includes(discriminator.propertyName)) {
requiredProps.push(discriminator.propertyName);
}
}

return requiredProps.length > 0 ? requiredProps : undefined;
}

Expand Down
20 changes: 16 additions & 4 deletions packages/openapi3/test/discriminator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe("openapi3: polymorphic model inheritance with discriminator", () => {
name: { type: "string" },
weight: { type: "number", format: "float" },
},
required: ["name"],
required: ["name", "kind"],
discriminator: {
propertyName: "kind",
mapping: {
Expand Down Expand Up @@ -154,7 +154,7 @@ describe("openapi3: polymorphic model inheritance with discriminator", () => {
},
weight: { type: "number", format: "float" },
},
required: ["name"],
required: ["name", "kind"],
discriminator: {
propertyName: "kind",
mapping: {
Expand Down Expand Up @@ -215,7 +215,7 @@ describe("openapi3: polymorphic model inheritance with discriminator", () => {
name: { type: "string" },
weight: { type: "number", format: "float" },
},
required: ["name"],
required: ["name", "kind"],
discriminator: {
propertyName: "kind",
mapping: {
Expand All @@ -234,7 +234,7 @@ describe("openapi3: polymorphic model inheritance with discriminator", () => {
},
bark: { type: "string" },
},
required: ["kind", "bark"],
required: ["kind", "bark", "breed"],
allOf: [{ $ref: "#/components/schemas/Pet" }],
discriminator: {
propertyName: "breed",
Expand Down Expand Up @@ -344,4 +344,16 @@ describe("openapi3: polymorphic model inheritance with discriminator", () => {
},
]);
});

it("discriminator always needs to be marked as required", async () => {
const openApi = await openApiFor(`
@discriminator("kind")
model Animal {
AlitzelMendez marked this conversation as resolved.
Show resolved Hide resolved
id: string;}`);

deepStrictEqual(openApi.components.schemas.Animal.required, ["id", "kind"]);
deepStrictEqual(openApi.components.schemas.Animal.discriminator, {
propertyName: "kind",
});
});
});
Loading