diff --git a/.changeset/true-weeks-love.md b/.changeset/true-weeks-love.md new file mode 100644 index 000000000..68e7b9953 --- /dev/null +++ b/.changeset/true-weeks-love.md @@ -0,0 +1,6 @@ +--- +'@openapi-qraft/test-fixtures': patch +'@openapi-qraft/plugin': patch +--- + +Add support for responses with `$ref` specification. diff --git a/packages/openapi-typescript-plugin/src/__snapshots__/no-extra-options.ts.snapshot.ts b/packages/openapi-typescript-plugin/src/__snapshots__/no-extra-options.ts.snapshot.ts index 0efdd5c99..c62552cb2 100644 --- a/packages/openapi-typescript-plugin/src/__snapshots__/no-extra-options.ts.snapshot.ts +++ b/packages/openapi-typescript-plugin/src/__snapshots__/no-extra-options.ts.snapshot.ts @@ -174,7 +174,17 @@ export interface components { url: string; }; }; - responses: never; + responses: { + /** @description Unexpected error */ + DefaultErrorSchemaResponse: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["ErrorSchemaResponse"]; + }; + }; + }; parameters: { IdIn: string[]; /** @example 2023-06-04 */ @@ -262,15 +272,7 @@ export interface operations { "application/json": components["schemas"]["HTTPValidationError"]; }; }; - /** @description Unexpected error */ - default: { - headers: { - [name: string]: unknown; - }; - content: { - "application/json": components["schemas"]["ErrorSchemaResponse"]; - }; - }; + default: components["responses"]["DefaultErrorSchemaResponse"]; }; }; get_approval_policies_id: { @@ -514,15 +516,7 @@ export interface operations { "application/json": components["schemas"]["HTTPValidationError"]; }; }; - /** @description Internal Server Error */ - default: { - headers: { - [name: string]: unknown; - }; - content: { - "application/json": components["schemas"]["ErrorSchemaResponse"]; - }; - }; + default: components["responses"]["DefaultErrorSchemaResponse"]; }; }; post_files: { diff --git a/packages/openapi-typescript-plugin/src/__snapshots__/with-explicit-component-enum-exports.ts.snapshot.ts b/packages/openapi-typescript-plugin/src/__snapshots__/with-explicit-component-enum-exports.ts.snapshot.ts index 81b5bb3bb..f1931db31 100644 --- a/packages/openapi-typescript-plugin/src/__snapshots__/with-explicit-component-enum-exports.ts.snapshot.ts +++ b/packages/openapi-typescript-plugin/src/__snapshots__/with-explicit-component-enum-exports.ts.snapshot.ts @@ -89,7 +89,17 @@ export interface components { url: string; }; }; - responses: never; + responses: { + /** @description Unexpected error */ + DefaultErrorSchemaResponse: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["ErrorSchemaResponse"]; + }; + }; + }; parameters: { IdIn: string[]; /** @example 2023-06-04 */ @@ -160,15 +170,7 @@ export interface operations { "application/json": components["schemas"]["HTTPValidationError"]; }; }; - /** @description Internal Server Error */ - default: { - headers: { - [name: string]: unknown; - }; - content: { - "application/json": components["schemas"]["ErrorSchemaResponse"]; - }; - }; + default: components["responses"]["DefaultErrorSchemaResponse"]; }; }; post_files: { diff --git a/packages/openapi-typescript-plugin/src/__snapshots__/with-explicit-component-exports.ts.snapshot.ts b/packages/openapi-typescript-plugin/src/__snapshots__/with-explicit-component-exports.ts.snapshot.ts index 73c5519a5..f434166ea 100644 --- a/packages/openapi-typescript-plugin/src/__snapshots__/with-explicit-component-exports.ts.snapshot.ts +++ b/packages/openapi-typescript-plugin/src/__snapshots__/with-explicit-component-exports.ts.snapshot.ts @@ -89,7 +89,17 @@ export interface components { url: string; }; }; - responses: never; + responses: { + /** @description Unexpected error */ + DefaultErrorSchemaResponse: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["ErrorSchemaResponse"]; + }; + }; + }; parameters: { IdIn: string[]; /** @example 2023-06-04 */ @@ -160,15 +170,7 @@ export interface operations { "application/json": components["schemas"]["HTTPValidationError"]; }; }; - /** @description Internal Server Error */ - default: { - headers: { - [name: string]: unknown; - }; - content: { - "application/json": components["schemas"]["ErrorSchemaResponse"]; - }; - }; + default: components["responses"]["DefaultErrorSchemaResponse"]; }; }; post_files: { diff --git a/packages/openapi-typescript-plugin/src/__snapshots__/with-extra-options.ts.snapshot.ts b/packages/openapi-typescript-plugin/src/__snapshots__/with-extra-options.ts.snapshot.ts index e421b9a36..f399fe8c0 100644 --- a/packages/openapi-typescript-plugin/src/__snapshots__/with-extra-options.ts.snapshot.ts +++ b/packages/openapi-typescript-plugin/src/__snapshots__/with-extra-options.ts.snapshot.ts @@ -126,7 +126,17 @@ export interface components { url: string; }; }; - responses: never; + responses: { + /** @description Unexpected error */ + DefaultErrorSchemaResponse: { + headers: { + [name: string]: unknown; + }; + content: { + "application/json": components["schemas"]["ErrorSchemaResponse"]; + }; + }; + }; parameters: { IdIn: string[]; /** @example 2023-06-04 */ @@ -197,15 +207,7 @@ export interface operations { "application/json": components["schemas"]["HTTPValidationError"]; }; }; - /** @description Internal Server Error */ - default: { - headers: { - [name: string]: unknown; - }; - content: { - "application/json": components["schemas"]["ErrorSchemaResponse"]; - }; - }; + default: components["responses"]["DefaultErrorSchemaResponse"]; }; }; post_files: { diff --git a/packages/plugin/src/lib/open-api/OpenAPISchemaType.ts b/packages/plugin/src/lib/open-api/OpenAPISchemaType.ts index d5e1b91bc..58c97c1ea 100644 --- a/packages/plugin/src/lib/open-api/OpenAPISchemaType.ts +++ b/packages/plugin/src/lib/open-api/OpenAPISchemaType.ts @@ -24,7 +24,8 @@ export type OpenAPISchemaType = { }; responses: { [statusCode in number | 'default']: { - description: string; + $ref?: string; + description?: string; content?: { [contentType: string]: | { diff --git a/packages/plugin/src/lib/open-api/getServices.ts b/packages/plugin/src/lib/open-api/getServices.ts index 2ad5cf75c..af60c4b37 100644 --- a/packages/plugin/src/lib/open-api/getServices.ts +++ b/packages/plugin/src/lib/open-api/getServices.ts @@ -66,6 +66,13 @@ export const getServices = ( > >( (acc, [statusCode, response]) => { + if (response.$ref) { + response = resolveDocumentLocalRef( + response.$ref, + openApiJson + ) as typeof response; + } + const statusType = statusCode !== 'default' && // See "default" response https://swagger.io/docs/specification/describing-responses/#default Number(statusCode) < 400 diff --git a/packages/test-fixtures/openapi.json b/packages/test-fixtures/openapi.json index 042bd62b5..8fdfb097f 100644 --- a/packages/test-fixtures/openapi.json +++ b/packages/test-fixtures/openapi.json @@ -120,12 +120,7 @@ } }, "default": { - "description": "Unexpected error", - "content": { - "application/json": { - "schema": { "$ref": "#/components/schemas/ErrorSchemaResponse" } - } - } + "$ref": "#/components/responses/DefaultErrorSchemaResponse" } }, "security": [{ "userToken": [] }, { "partnerToken": [] }] @@ -493,12 +488,7 @@ } }, "default": { - "description": "Internal Server Error", - "content": { - "application/json": { - "schema": { "$ref": "#/components/schemas/ErrorSchemaResponse" } - } - } + "$ref": "#/components/responses/DefaultErrorSchemaResponse" } }, "security": [{ "HTTPBearer": [] }] @@ -881,6 +871,16 @@ } } } + }, + "responses": { + "DefaultErrorSchemaResponse": { + "description": "Unexpected error", + "content": { + "application/json": { + "schema": { "$ref": "#/components/schemas/ErrorSchemaResponse" } + } + } + } } } }