From 0e388f3f587feaac8a9876cbc07d6762457eda42 Mon Sep 17 00:00:00 2001 From: Travis Prescott Date: Mon, 16 Oct 2023 15:30:36 -0700 Subject: [PATCH] Fix #2575. --- packages/openapi3/src/openapi.ts | 13 ++++++++--- packages/openapi3/test/shared-routes.test.ts | 23 ++++++++++++++++++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/packages/openapi3/src/openapi.ts b/packages/openapi3/src/openapi.ts index 46a965378bc..bfb8ba44975 100644 --- a/packages/openapi3/src/openapi.ts +++ b/packages/openapi3/src/openapi.ts @@ -416,11 +416,18 @@ function createOAPIEmitter(program: Program, options: ResolvedOpenAPI3EmitterOpt /** * Validates that common responses are consistent and returns the minimal set that describes the differences. */ - function validateCommonResponses(ops: HttpOperation[]): HttpOperationResponse[] { + function validateCommonResponses( + statusCode: string, + ops: HttpOperation[] + ): HttpOperationResponse[] { const statusCodeResponses: HttpOperationResponse[] = []; for (const op of ops) { for (const response of op.responses) { - statusCodeResponses.push(response); + if (getOpenAPI3StatusCodes(response.statusCodes, response.type).includes(statusCode)) { + statusCodeResponses.push(response); + } else { + const test = "best"; + } } } const ref = statusCodeResponses[0]; @@ -609,7 +616,7 @@ function createOAPIEmitter(program: Program, options: ResolvedOpenAPI3EmitterOpt } shared.bodies = validateCommonBodies(operations); for (const [statusCode, ops] of responseMap) { - shared.responses.set(statusCode, validateCommonResponses(ops)); + shared.responses.set(statusCode, validateCommonResponses(statusCode, ops)); } results.push(shared); return results; diff --git a/packages/openapi3/test/shared-routes.test.ts b/packages/openapi3/test/shared-routes.test.ts index 9b70e7f1c68..4d14f1a1d47 100644 --- a/packages/openapi3/test/shared-routes.test.ts +++ b/packages/openapi3/test/shared-routes.test.ts @@ -353,13 +353,21 @@ describe("openapi3: shared routes", () => { ` @service({title: "My Service"}) namespace Foo { + + @error + model ErrorModel { + @header "x-ms-error-code": string; + + description: string; + } + @sharedRoute @route("/process") - op processInt(@body body: int32, @query options: string): int32; + op processInt(@body body: int32, @query options: string): int32 | ErrorModel; @sharedRoute @route("/process") - op processString(@body body: string, @query options: string): string; + op processString(@body body: string, @query options: string): string | ErrorModel; } ` ); @@ -402,6 +410,17 @@ describe("openapi3: shared routes", () => { }, description: "The request has succeeded.", }, + default: { + description: "The request has succeeded.", + headers: { + "x-ms-error-code": { + required: true, + schema: { + type: "string", + }, + }, + }, + }, }); });