Skip to content

Commit

Permalink
Mark discriminator properties consistently as requiered (#4663)
Browse files Browse the repository at this point in the history
Co-authored-by: Timothee Guerin <[email protected]>
  • Loading branch information
AlitzelMendez and timotheeguerin authored Oct 14, 2024
1 parent 3dfdd79 commit de763ce
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: fix
packages:
- "@typespec/openapi3"
---

Discriminator properties are marked as required regardless if they are in TypeSpec to match OpenAPI3 spec.
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
22 changes: 18 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,18 @@ 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 {
id: string;
kind?: string;
}`);

deepStrictEqual(openApi.components.schemas.Animal.required, ["id", "kind"]);
deepStrictEqual(openApi.components.schemas.Animal.discriminator, {
propertyName: "kind",
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ components:
type: object
required:
- name
- kind
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ components:
required:
- name
- favoriteToys
- type
properties:
name:
type: string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ components:
required:
- name
- favoriteToys
- type
properties:
name:
type: string
Expand Down

0 comments on commit de763ce

Please sign in to comment.