Skip to content

Commit 435ccd7

Browse files
authored
fix: set responses without content schemas to never or unknown, depending on status code (#334)
* test: set responses without content schemas to `never` or `unknown`, depending on status code * fix: set responses without content schemas to `never` or `unknown`, depending on status code * fixup! test: set responses without content schemas to `never` or `unknown`, depending on status code * style: make eslint happy
1 parent 624b66c commit 435ccd7

File tree

7 files changed

+1227
-1149
lines changed

7 files changed

+1227
-1149
lines changed

src/types/OpenAPI3.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface OpenAPI3Parameter {
3232

3333
export interface OpenAPI3ResponseObject {
3434
description?: string;
35-
content: {
35+
content?: {
3636
[contentType: string]: { schema: OpenAPI3SchemaObject | OpenAPI3Reference };
3737
};
3838
}

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function comment(text: string): string {
88
}
99

1010
/** shim for Object.fromEntries() for Node < 13 */
11-
export function fromEntries(entries: [string, any][]): object {
11+
export function fromEntries(entries: [string, any][]): Record<string, unknown> {
1212
return entries.reduce((obj, [key, val]) => ({ ...obj, [key]: val }), {});
1313
}
1414

src/v3.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,11 @@ export default function generateTypesV3(
196196
([statusCode, response]) => {
197197
if (response.description) output += comment(response.description);
198198
if (!response.content || !Object.keys(response.content).length) {
199-
output += `"${statusCode}": any;\n`;
199+
const type =
200+
statusCode === "204" || Math.floor(+statusCode / 100) === 3
201+
? "never"
202+
: "unknown";
203+
output += `"${statusCode}": ${type};\n`;
200204
return;
201205
}
202206
output += `"${statusCode}": {\n`;

tests/bin/expected/petstore.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ export interface paths {
1010
/**
1111
* Invalid ID supplied
1212
*/
13-
'400': any
13+
'400': unknown
1414
/**
1515
* Pet not found
1616
*/
17-
'404': any
17+
'404': unknown
1818
/**
1919
* Validation exception
2020
*/
21-
'405': any
21+
'405': unknown
2222
}
2323
}
2424
post: {
2525
responses: {
2626
/**
2727
* Invalid input
2828
*/
29-
'405': any
29+
'405': unknown
3030
}
3131
}
3232
}
@@ -54,7 +54,7 @@ export interface paths {
5454
/**
5555
* Invalid status value
5656
*/
57-
'400': any
57+
'400': unknown
5858
}
5959
}
6060
}
@@ -82,7 +82,7 @@ export interface paths {
8282
/**
8383
* Invalid tag value
8484
*/
85-
'400': any
85+
'400': unknown
8686
}
8787
}
8888
}
@@ -110,11 +110,11 @@ export interface paths {
110110
/**
111111
* Invalid ID supplied
112112
*/
113-
'400': any
113+
'400': unknown
114114
/**
115115
* Pet not found
116116
*/
117-
'404': any
117+
'404': unknown
118118
}
119119
}
120120
post: {
@@ -130,7 +130,7 @@ export interface paths {
130130
/**
131131
* Invalid input
132132
*/
133-
'405': any
133+
'405': unknown
134134
}
135135
}
136136
delete: {
@@ -149,11 +149,11 @@ export interface paths {
149149
/**
150150
* Invalid ID supplied
151151
*/
152-
'400': any
152+
'400': unknown
153153
/**
154154
* Pet not found
155155
*/
156-
'404': any
156+
'404': unknown
157157
}
158158
}
159159
}
@@ -205,7 +205,7 @@ export interface paths {
205205
/**
206206
* Invalid Order
207207
*/
208-
'400': any
208+
'400': unknown
209209
}
210210
}
211211
}
@@ -233,11 +233,11 @@ export interface paths {
233233
/**
234234
* Invalid ID supplied
235235
*/
236-
'400': any
236+
'400': unknown
237237
/**
238238
* Order not found
239239
*/
240-
'404': any
240+
'404': unknown
241241
}
242242
}
243243
/**
@@ -256,11 +256,11 @@ export interface paths {
256256
/**
257257
* Invalid ID supplied
258258
*/
259-
'400': any
259+
'400': unknown
260260
/**
261261
* Order not found
262262
*/
263-
'404': any
263+
'404': unknown
264264
}
265265
}
266266
}
@@ -273,7 +273,7 @@ export interface paths {
273273
/**
274274
* successful operation
275275
*/
276-
default: any
276+
default: unknown
277277
}
278278
}
279279
}
@@ -283,7 +283,7 @@ export interface paths {
283283
/**
284284
* successful operation
285285
*/
286-
default: any
286+
default: unknown
287287
}
288288
}
289289
}
@@ -293,7 +293,7 @@ export interface paths {
293293
/**
294294
* successful operation
295295
*/
296-
default: any
296+
default: unknown
297297
}
298298
}
299299
}
@@ -322,7 +322,7 @@ export interface paths {
322322
/**
323323
* Invalid username/password supplied
324324
*/
325-
'400': any
325+
'400': unknown
326326
}
327327
}
328328
}
@@ -332,7 +332,7 @@ export interface paths {
332332
/**
333333
* successful operation
334334
*/
335-
default: any
335+
default: unknown
336336
}
337337
}
338338
}
@@ -357,11 +357,11 @@ export interface paths {
357357
/**
358358
* Invalid username supplied
359359
*/
360-
'400': any
360+
'400': unknown
361361
/**
362362
* User not found
363363
*/
364-
'404': any
364+
'404': unknown
365365
}
366366
}
367367
/**
@@ -380,11 +380,11 @@ export interface paths {
380380
/**
381381
* Invalid user supplied
382382
*/
383-
'400': any
383+
'400': unknown
384384
/**
385385
* User not found
386386
*/
387-
'404': any
387+
'404': unknown
388388
}
389389
}
390390
/**
@@ -403,11 +403,11 @@ export interface paths {
403403
/**
404404
* Invalid username supplied
405405
*/
406-
'400': any
406+
'400': unknown
407407
/**
408408
* User not found
409409
*/
410-
'404': any
410+
'404': unknown
411411
}
412412
}
413413
}

0 commit comments

Comments
 (0)