Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make status codes numeric #381

Merged
merged 1 commit into from
Nov 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
700 changes: 350 additions & 350 deletions examples/stripe-openapi3.ts

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion src/types/OpenAPI3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export interface OpenAPI3Operation {
parameters?: Parameter[];
requestBody?: OpenAPI3RequestBody;
responses: {
[statusCode: string]: OpenAPI3ResponseObject;
[statusCode: number]: OpenAPI3ResponseObject;
default?: OpenAPI3ResponseObject;
};
}

Expand Down
11 changes: 7 additions & 4 deletions src/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,17 @@ export default function generateTypesV3(input: OpenAPI3 | OpenAPI3Schemas, optio

// handle responses
output += `responses: {\n`;
Object.entries(operation.responses).forEach(([statusCode, response]) => {
Object.entries(operation.responses).forEach(([statusCodeString, response]) => {
// NOTE: Numeric status codes and the "default" response.
const statusCode = Number(statusCodeString) || statusCodeString;
if (!response) return;
if (response.description) output += comment(response.description);
if (!response.content || !Object.keys(response.content).length) {
const type = statusCode === "204" || Math.floor(+statusCode / 100) === 3 ? "never" : "unknown";
output += `"${statusCode}": ${type};\n`;
const type = statusCode === 204 || Math.floor(+statusCode / 100) === 3 ? "never" : "unknown";
output += `${statusCode}: ${type};\n`;
return;
}
output += `"${statusCode}": {\n`;
output += `${statusCode}: {\n`;
Object.entries(response.content).forEach(([contentType, encodedResponse]) => {
output += `"${contentType}": ${transform(encodedResponse.schema)};\n`;
});
Expand Down
64 changes: 32 additions & 32 deletions tests/bin/expected/petstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ export interface operations {
/**
* Invalid ID supplied
*/
'400': unknown
400: unknown
/**
* Pet not found
*/
'404': unknown
404: unknown
/**
* Validation exception
*/
'405': unknown
405: unknown
}
}
addPet: {
Expand All @@ -84,7 +84,7 @@ export interface operations {
/**
* Invalid input
*/
'405': unknown
405: unknown
}
}
/**
Expand All @@ -103,14 +103,14 @@ export interface operations {
/**
* successful operation
*/
'200': {
200: {
'application/xml': components['schemas']['Pet'][]
'application/json': components['schemas']['Pet'][]
}
/**
* Invalid status value
*/
'400': unknown
400: unknown
}
}
/**
Expand All @@ -129,14 +129,14 @@ export interface operations {
/**
* successful operation
*/
'200': {
200: {
'application/xml': components['schemas']['Pet'][]
'application/json': components['schemas']['Pet'][]
}
/**
* Invalid tag value
*/
'400': unknown
400: unknown
}
}
/**
Expand All @@ -155,18 +155,18 @@ export interface operations {
/**
* successful operation
*/
'200': {
200: {
'application/xml': components['schemas']['Pet']
'application/json': components['schemas']['Pet']
}
/**
* Invalid ID supplied
*/
'400': unknown
400: unknown
/**
* Pet not found
*/
'404': unknown
404: unknown
}
}
updatePetWithForm: {
Expand Down Expand Up @@ -194,7 +194,7 @@ export interface operations {
/**
* Invalid input
*/
'405': unknown
405: unknown
}
}
deletePet: {
Expand All @@ -213,11 +213,11 @@ export interface operations {
/**
* Invalid ID supplied
*/
'400': unknown
400: unknown
/**
* Pet not found
*/
'404': unknown
404: unknown
}
}
uploadFile: {
Expand Down Expand Up @@ -245,7 +245,7 @@ export interface operations {
/**
* successful operation
*/
'200': {
200: {
'application/json': components['schemas']['ApiResponse']
}
}
Expand All @@ -258,7 +258,7 @@ export interface operations {
/**
* successful operation
*/
'200': {
200: {
'application/json': { [key: string]: number }
}
}
Expand All @@ -271,14 +271,14 @@ export interface operations {
/**
* successful operation
*/
'200': {
200: {
'application/xml': components['schemas']['Order']
'application/json': components['schemas']['Order']
}
/**
* Invalid Order
*/
'400': unknown
400: unknown
}
}
/**
Expand All @@ -297,18 +297,18 @@ export interface operations {
/**
* successful operation
*/
'200': {
200: {
'application/xml': components['schemas']['Order']
'application/json': components['schemas']['Order']
}
/**
* Invalid ID supplied
*/
'400': unknown
400: unknown
/**
* Order not found
*/
'404': unknown
404: unknown
}
}
/**
Expand All @@ -327,11 +327,11 @@ export interface operations {
/**
* Invalid ID supplied
*/
'400': unknown
400: unknown
/**
* Order not found
*/
'404': unknown
404: unknown
}
}
/**
Expand Down Expand Up @@ -387,14 +387,14 @@ export interface operations {
/**
* successful operation
*/
'200': {
200: {
'application/xml': string
'application/json': string
}
/**
* Invalid username/password supplied
*/
'400': unknown
400: unknown
}
}
logoutUser: {
Expand All @@ -418,18 +418,18 @@ export interface operations {
/**
* successful operation
*/
'200': {
200: {
'application/xml': components['schemas']['User']
'application/json': components['schemas']['User']
}
/**
* Invalid username supplied
*/
'400': unknown
400: unknown
/**
* User not found
*/
'404': unknown
404: unknown
}
}
/**
Expand All @@ -451,11 +451,11 @@ export interface operations {
/**
* Invalid user supplied
*/
'400': unknown
400: unknown
/**
* User not found
*/
'404': unknown
404: unknown
}
}
/**
Expand All @@ -474,11 +474,11 @@ export interface operations {
/**
* Invalid username supplied
*/
'400': unknown
400: unknown
/**
* User not found
*/
'404': unknown
404: unknown
}
}
}
Expand Down
Loading