Skip to content

Commit c3e40b4

Browse files
authored
make status codes numeric (#381)
1 parent 0c0060d commit c3e40b4

File tree

12 files changed

+5208
-5204
lines changed

12 files changed

+5208
-5204
lines changed

examples/stripe-openapi3.ts

Lines changed: 350 additions & 350 deletions
Large diffs are not rendered by default.

src/types/OpenAPI3.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export interface OpenAPI3Operation {
3434
parameters?: Parameter[];
3535
requestBody?: OpenAPI3RequestBody;
3636
responses: {
37-
[statusCode: string]: OpenAPI3ResponseObject;
37+
[statusCode: number]: OpenAPI3ResponseObject;
38+
default?: OpenAPI3ResponseObject;
3839
};
3940
}
4041

src/v3.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,17 @@ export default function generateTypesV3(input: OpenAPI3 | OpenAPI3Schemas, optio
202202

203203
// handle responses
204204
output += `responses: {\n`;
205-
Object.entries(operation.responses).forEach(([statusCode, response]) => {
205+
Object.entries(operation.responses).forEach(([statusCodeString, response]) => {
206+
// NOTE: Numeric status codes and the "default" response.
207+
const statusCode = Number(statusCodeString) || statusCodeString;
208+
if (!response) return;
206209
if (response.description) output += comment(response.description);
207210
if (!response.content || !Object.keys(response.content).length) {
208-
const type = statusCode === "204" || Math.floor(+statusCode / 100) === 3 ? "never" : "unknown";
209-
output += `"${statusCode}": ${type};\n`;
211+
const type = statusCode === 204 || Math.floor(+statusCode / 100) === 3 ? "never" : "unknown";
212+
output += `${statusCode}: ${type};\n`;
210213
return;
211214
}
212-
output += `"${statusCode}": {\n`;
215+
output += `${statusCode}: {\n`;
213216
Object.entries(response.content).forEach(([contentType, encodedResponse]) => {
214217
output += `"${contentType}": ${transform(encodedResponse.schema)};\n`;
215218
});

tests/bin/expected/petstore.ts

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,15 @@ export interface operations {
6464
/**
6565
* Invalid ID supplied
6666
*/
67-
'400': unknown
67+
400: unknown
6868
/**
6969
* Pet not found
7070
*/
71-
'404': unknown
71+
404: unknown
7272
/**
7373
* Validation exception
7474
*/
75-
'405': unknown
75+
405: unknown
7676
}
7777
}
7878
addPet: {
@@ -84,7 +84,7 @@ export interface operations {
8484
/**
8585
* Invalid input
8686
*/
87-
'405': unknown
87+
405: unknown
8888
}
8989
}
9090
/**
@@ -103,14 +103,14 @@ export interface operations {
103103
/**
104104
* successful operation
105105
*/
106-
'200': {
106+
200: {
107107
'application/xml': components['schemas']['Pet'][]
108108
'application/json': components['schemas']['Pet'][]
109109
}
110110
/**
111111
* Invalid status value
112112
*/
113-
'400': unknown
113+
400: unknown
114114
}
115115
}
116116
/**
@@ -129,14 +129,14 @@ export interface operations {
129129
/**
130130
* successful operation
131131
*/
132-
'200': {
132+
200: {
133133
'application/xml': components['schemas']['Pet'][]
134134
'application/json': components['schemas']['Pet'][]
135135
}
136136
/**
137137
* Invalid tag value
138138
*/
139-
'400': unknown
139+
400: unknown
140140
}
141141
}
142142
/**
@@ -155,18 +155,18 @@ export interface operations {
155155
/**
156156
* successful operation
157157
*/
158-
'200': {
158+
200: {
159159
'application/xml': components['schemas']['Pet']
160160
'application/json': components['schemas']['Pet']
161161
}
162162
/**
163163
* Invalid ID supplied
164164
*/
165-
'400': unknown
165+
400: unknown
166166
/**
167167
* Pet not found
168168
*/
169-
'404': unknown
169+
404: unknown
170170
}
171171
}
172172
updatePetWithForm: {
@@ -194,7 +194,7 @@ export interface operations {
194194
/**
195195
* Invalid input
196196
*/
197-
'405': unknown
197+
405: unknown
198198
}
199199
}
200200
deletePet: {
@@ -213,11 +213,11 @@ export interface operations {
213213
/**
214214
* Invalid ID supplied
215215
*/
216-
'400': unknown
216+
400: unknown
217217
/**
218218
* Pet not found
219219
*/
220-
'404': unknown
220+
404: unknown
221221
}
222222
}
223223
uploadFile: {
@@ -245,7 +245,7 @@ export interface operations {
245245
/**
246246
* successful operation
247247
*/
248-
'200': {
248+
200: {
249249
'application/json': components['schemas']['ApiResponse']
250250
}
251251
}
@@ -258,7 +258,7 @@ export interface operations {
258258
/**
259259
* successful operation
260260
*/
261-
'200': {
261+
200: {
262262
'application/json': { [key: string]: number }
263263
}
264264
}
@@ -271,14 +271,14 @@ export interface operations {
271271
/**
272272
* successful operation
273273
*/
274-
'200': {
274+
200: {
275275
'application/xml': components['schemas']['Order']
276276
'application/json': components['schemas']['Order']
277277
}
278278
/**
279279
* Invalid Order
280280
*/
281-
'400': unknown
281+
400: unknown
282282
}
283283
}
284284
/**
@@ -297,18 +297,18 @@ export interface operations {
297297
/**
298298
* successful operation
299299
*/
300-
'200': {
300+
200: {
301301
'application/xml': components['schemas']['Order']
302302
'application/json': components['schemas']['Order']
303303
}
304304
/**
305305
* Invalid ID supplied
306306
*/
307-
'400': unknown
307+
400: unknown
308308
/**
309309
* Order not found
310310
*/
311-
'404': unknown
311+
404: unknown
312312
}
313313
}
314314
/**
@@ -327,11 +327,11 @@ export interface operations {
327327
/**
328328
* Invalid ID supplied
329329
*/
330-
'400': unknown
330+
400: unknown
331331
/**
332332
* Order not found
333333
*/
334-
'404': unknown
334+
404: unknown
335335
}
336336
}
337337
/**
@@ -387,14 +387,14 @@ export interface operations {
387387
/**
388388
* successful operation
389389
*/
390-
'200': {
390+
200: {
391391
'application/xml': string
392392
'application/json': string
393393
}
394394
/**
395395
* Invalid username/password supplied
396396
*/
397-
'400': unknown
397+
400: unknown
398398
}
399399
}
400400
logoutUser: {
@@ -418,18 +418,18 @@ export interface operations {
418418
/**
419419
* successful operation
420420
*/
421-
'200': {
421+
200: {
422422
'application/xml': components['schemas']['User']
423423
'application/json': components['schemas']['User']
424424
}
425425
/**
426426
* Invalid username supplied
427427
*/
428-
'400': unknown
428+
400: unknown
429429
/**
430430
* User not found
431431
*/
432-
'404': unknown
432+
404: unknown
433433
}
434434
}
435435
/**
@@ -451,11 +451,11 @@ export interface operations {
451451
/**
452452
* Invalid user supplied
453453
*/
454-
'400': unknown
454+
400: unknown
455455
/**
456456
* User not found
457457
*/
458-
'404': unknown
458+
404: unknown
459459
}
460460
}
461461
/**
@@ -474,11 +474,11 @@ export interface operations {
474474
/**
475475
* Invalid username supplied
476476
*/
477-
'400': unknown
477+
400: unknown
478478
/**
479479
* User not found
480480
*/
481-
'404': unknown
481+
404: unknown
482482
}
483483
}
484484
}

0 commit comments

Comments
 (0)