Skip to content

Commit 949b8ac

Browse files
authored
Merge pull request #349 from OpenAPI-Qraft/feat/default-error-resolving
feat(plugin): add support for responses with `$ref` specification
2 parents 6ea6a04 + 8f90621 commit 949b8ac

8 files changed

Lines changed: 76 additions & 62 deletions

File tree

.changeset/true-weeks-love.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@openapi-qraft/test-fixtures': patch
3+
'@openapi-qraft/plugin': patch
4+
---
5+
6+
Add support for responses with `$ref` specification.

packages/openapi-typescript-plugin/src/__snapshots__/no-extra-options.ts.snapshot.ts

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,17 @@ export interface components {
174174
url: string;
175175
};
176176
};
177-
responses: never;
177+
responses: {
178+
/** @description Unexpected error */
179+
DefaultErrorSchemaResponse: {
180+
headers: {
181+
[name: string]: unknown;
182+
};
183+
content: {
184+
"application/json": components["schemas"]["ErrorSchemaResponse"];
185+
};
186+
};
187+
};
178188
parameters: {
179189
IdIn: string[];
180190
/** @example 2023-06-04 */
@@ -262,15 +272,7 @@ export interface operations {
262272
"application/json": components["schemas"]["HTTPValidationError"];
263273
};
264274
};
265-
/** @description Unexpected error */
266-
default: {
267-
headers: {
268-
[name: string]: unknown;
269-
};
270-
content: {
271-
"application/json": components["schemas"]["ErrorSchemaResponse"];
272-
};
273-
};
275+
default: components["responses"]["DefaultErrorSchemaResponse"];
274276
};
275277
};
276278
get_approval_policies_id: {
@@ -514,15 +516,7 @@ export interface operations {
514516
"application/json": components["schemas"]["HTTPValidationError"];
515517
};
516518
};
517-
/** @description Internal Server Error */
518-
default: {
519-
headers: {
520-
[name: string]: unknown;
521-
};
522-
content: {
523-
"application/json": components["schemas"]["ErrorSchemaResponse"];
524-
};
525-
};
519+
default: components["responses"]["DefaultErrorSchemaResponse"];
526520
};
527521
};
528522
post_files: {

packages/openapi-typescript-plugin/src/__snapshots__/with-explicit-component-enum-exports.ts.snapshot.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,17 @@ export interface components {
8989
url: string;
9090
};
9191
};
92-
responses: never;
92+
responses: {
93+
/** @description Unexpected error */
94+
DefaultErrorSchemaResponse: {
95+
headers: {
96+
[name: string]: unknown;
97+
};
98+
content: {
99+
"application/json": components["schemas"]["ErrorSchemaResponse"];
100+
};
101+
};
102+
};
93103
parameters: {
94104
IdIn: string[];
95105
/** @example 2023-06-04 */
@@ -160,15 +170,7 @@ export interface operations {
160170
"application/json": components["schemas"]["HTTPValidationError"];
161171
};
162172
};
163-
/** @description Internal Server Error */
164-
default: {
165-
headers: {
166-
[name: string]: unknown;
167-
};
168-
content: {
169-
"application/json": components["schemas"]["ErrorSchemaResponse"];
170-
};
171-
};
173+
default: components["responses"]["DefaultErrorSchemaResponse"];
172174
};
173175
};
174176
post_files: {

packages/openapi-typescript-plugin/src/__snapshots__/with-explicit-component-exports.ts.snapshot.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,17 @@ export interface components {
8989
url: string;
9090
};
9191
};
92-
responses: never;
92+
responses: {
93+
/** @description Unexpected error */
94+
DefaultErrorSchemaResponse: {
95+
headers: {
96+
[name: string]: unknown;
97+
};
98+
content: {
99+
"application/json": components["schemas"]["ErrorSchemaResponse"];
100+
};
101+
};
102+
};
93103
parameters: {
94104
IdIn: string[];
95105
/** @example 2023-06-04 */
@@ -160,15 +170,7 @@ export interface operations {
160170
"application/json": components["schemas"]["HTTPValidationError"];
161171
};
162172
};
163-
/** @description Internal Server Error */
164-
default: {
165-
headers: {
166-
[name: string]: unknown;
167-
};
168-
content: {
169-
"application/json": components["schemas"]["ErrorSchemaResponse"];
170-
};
171-
};
173+
default: components["responses"]["DefaultErrorSchemaResponse"];
172174
};
173175
};
174176
post_files: {

packages/openapi-typescript-plugin/src/__snapshots__/with-extra-options.ts.snapshot.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,17 @@ export interface components {
126126
url: string;
127127
};
128128
};
129-
responses: never;
129+
responses: {
130+
/** @description Unexpected error */
131+
DefaultErrorSchemaResponse: {
132+
headers: {
133+
[name: string]: unknown;
134+
};
135+
content: {
136+
"application/json": components["schemas"]["ErrorSchemaResponse"];
137+
};
138+
};
139+
};
130140
parameters: {
131141
IdIn: string[];
132142
/** @example 2023-06-04 */
@@ -197,15 +207,7 @@ export interface operations {
197207
"application/json": components["schemas"]["HTTPValidationError"];
198208
};
199209
};
200-
/** @description Internal Server Error */
201-
default: {
202-
headers: {
203-
[name: string]: unknown;
204-
};
205-
content: {
206-
"application/json": components["schemas"]["ErrorSchemaResponse"];
207-
};
208-
};
210+
default: components["responses"]["DefaultErrorSchemaResponse"];
209211
};
210212
};
211213
post_files: {

packages/plugin/src/lib/open-api/OpenAPISchemaType.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ export type OpenAPISchemaType = {
2424
};
2525
responses: {
2626
[statusCode in number | 'default']: {
27-
description: string;
27+
$ref?: string;
28+
description?: string;
2829
content?: {
2930
[contentType: string]:
3031
| {

packages/plugin/src/lib/open-api/getServices.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,13 @@ export const getServices = (
6666
>
6767
>(
6868
(acc, [statusCode, response]) => {
69+
if (response.$ref) {
70+
response = resolveDocumentLocalRef(
71+
response.$ref,
72+
openApiJson
73+
) as typeof response;
74+
}
75+
6976
const statusType =
7077
statusCode !== 'default' && // See "default" response https://swagger.io/docs/specification/describing-responses/#default
7178
Number(statusCode) < 400

packages/test-fixtures/openapi.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,7 @@
120120
}
121121
},
122122
"default": {
123-
"description": "Unexpected error",
124-
"content": {
125-
"application/json": {
126-
"schema": { "$ref": "#/components/schemas/ErrorSchemaResponse" }
127-
}
128-
}
123+
"$ref": "#/components/responses/DefaultErrorSchemaResponse"
129124
}
130125
},
131126
"security": [{ "userToken": [] }, { "partnerToken": [] }]
@@ -493,12 +488,7 @@
493488
}
494489
},
495490
"default": {
496-
"description": "Internal Server Error",
497-
"content": {
498-
"application/json": {
499-
"schema": { "$ref": "#/components/schemas/ErrorSchemaResponse" }
500-
}
501-
}
491+
"$ref": "#/components/responses/DefaultErrorSchemaResponse"
502492
}
503493
},
504494
"security": [{ "HTTPBearer": [] }]
@@ -881,6 +871,16 @@
881871
}
882872
}
883873
}
874+
},
875+
"responses": {
876+
"DefaultErrorSchemaResponse": {
877+
"description": "Unexpected error",
878+
"content": {
879+
"application/json": {
880+
"schema": { "$ref": "#/components/schemas/ErrorSchemaResponse" }
881+
}
882+
}
883+
}
884884
}
885885
}
886886
}

0 commit comments

Comments
 (0)