From 4915d5bbe64b2df53c998f20a59d8fbdf258dd08 Mon Sep 17 00:00:00 2001 From: Timothee Guerin Date: Mon, 12 Feb 2024 11:14:40 -0800 Subject: [PATCH] Fix: Required array not respecting encodedName (#2906) The required array didn't make use the encoded name creating invalid spec where there would be a mismatch of names --- .changeset/wicked-brooms-march.md | 5 +++++ packages/openapi3/src/schema-emitter.ts | 8 +++++++- packages/openapi3/test/models.test.ts | 3 +++ .../output/encoded-names/@typespec/openapi3/openapi.yaml | 4 ++-- 4 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 .changeset/wicked-brooms-march.md diff --git a/.changeset/wicked-brooms-march.md b/.changeset/wicked-brooms-march.md new file mode 100644 index 0000000000..9977a6f57e --- /dev/null +++ b/.changeset/wicked-brooms-march.md @@ -0,0 +1,5 @@ +--- +"@typespec/openapi3": patch +--- + +Fix: `required` array on schema wasn't using the value provided by `@encodedName("application/json"` diff --git a/packages/openapi3/src/schema-emitter.ts b/packages/openapi3/src/schema-emitter.ts index 32a7d54994..6456d8b589 100644 --- a/packages/openapi3/src/schema-emitter.ts +++ b/packages/openapi3/src/schema-emitter.ts @@ -268,7 +268,13 @@ export class OpenAPI3SchemaEmitter extends TypeEmitter< } if (!this.#metadataInfo.isOptional(prop, visibility)) { - requiredProps.push(prop.name); + const encodedName = resolveEncodedName( + this.emitter.getProgram(), + prop, + this.#getContentType() + ); + + requiredProps.push(encodedName); } } diff --git a/packages/openapi3/test/models.test.ts b/packages/openapi3/test/models.test.ts index a2052829db..27d920de27 100644 --- a/packages/openapi3/test/models.test.ts +++ b/packages/openapi3/test/models.test.ts @@ -32,6 +32,7 @@ describe("openapi3: models", () => { ); expect(res.schemas.Foo).toMatchObject({ + required: ["xJson"], properties: { xJson: { type: "integer", format: "int32" }, }, @@ -48,6 +49,7 @@ describe("openapi3: models", () => { ); expect(res.schemas.Foo).toMatchObject({ + required: ["xJson"], properties: { xJson: { type: "integer", format: "int32" }, }, @@ -65,6 +67,7 @@ describe("openapi3: models", () => { ); expect(res.schemas.Foo).toMatchObject({ + required: ["xJson"], properties: { xJson: { type: "integer", format: "int32" }, }, diff --git a/packages/samples/test/output/encoded-names/@typespec/openapi3/openapi.yaml b/packages/samples/test/output/encoded-names/@typespec/openapi3/openapi.yaml index ad906f39fe..6b2d123108 100644 --- a/packages/samples/test/output/encoded-names/@typespec/openapi3/openapi.yaml +++ b/packages/samples/test/output/encoded-names/@typespec/openapi3/openapi.yaml @@ -23,8 +23,8 @@ components: WithEncodedNames: type: object required: - - notBefore - - expires + - nbf + - exp - created - updated properties: