Skip to content

Commit 2599785

Browse files
authored
Add support for components.responses (#429)
1 parent 72babbf commit 2599785

File tree

8 files changed

+5926
-6155
lines changed

8 files changed

+5926
-6155
lines changed

package-lock.json

Lines changed: 4634 additions & 5007 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"js-yaml": "^3.14.0",
6464
"kleur": "^4.1.3",
6565
"meow": "^9.0.0",
66-
"prettier": "^2.2.0"
66+
"prettier": "^2.2.1"
6767
},
6868
"devDependencies": {
6969
"@pika/pack": "^0.5.0",
@@ -81,6 +81,6 @@
8181
"eslint-plugin-prettier": "^3.1.4",
8282
"jest": "^26.5.3",
8383
"ts-jest": "^26.4.1",
84-
"typescript": "^4.1.2"
84+
"typescript": "^4.1.3"
8585
}
8686
}

src/types/OpenAPI3.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,21 @@ export interface OpenAPI3Parameter {
5050
}
5151

5252
export interface OpenAPI3ResponseObject {
53+
$ref?: string; // this is technically a lie; this should be a discriminated union type apart from OpenAPI3ResponseObject. However, in that scenario TS causes more trouble than it’s worth, so it’s here for convenience
5354
description?: string;
54-
content?: {
55-
[contentType: string]: { schema: OpenAPI3SchemaObject | OpenAPI3Reference };
56-
};
55+
content?: { [contentType: string]: { schema: OpenAPI3SchemaObject } };
5756
}
5857

5958
export interface OpenAPI3RequestBody {
6059
description?: string;
6160
content?: {
62-
[contentType: string]: { schema: OpenAPI3SchemaObject | { $ref: string } };
61+
[contentType: string]: { schema: OpenAPI3SchemaObject };
6362
};
6463
}
6564

6665
export interface OpenAPI3Components {
6766
schemas: OpenAPI3Schemas;
68-
responses?: { [key: string]: OpenAPI3ResponseObject };
67+
responses?: { [statusCode: string]: OpenAPI3ResponseObject };
6968
parameters?: { [key: string]: OpenAPI3Parameter };
7069
}
7170

src/v3.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export default function generateTypesV3(input: OpenAPI3 | OpenAPI3Schemas, optio
127127
}
128128

129129
// 4. transform
130-
output += transform(value.schema ? value.schema : value);
130+
output += transform(value.schema || value);
131131

132132
// 5. close nullable
133133
if (value.nullable) {
@@ -205,7 +205,15 @@ export default function generateTypesV3(input: OpenAPI3 | OpenAPI3Schemas, optio
205205
Object.entries(operation.responses).forEach(([statusCodeString, response]) => {
206206
// NOTE: Numeric status codes and the "default" response.
207207
const statusCode = Number(statusCodeString) || statusCodeString;
208-
if (!response) return;
208+
if (!response || typeof response !== "object") return;
209+
210+
// option 1: $ref
211+
if (response.$ref) {
212+
output += `${statusCode}: ${transformRef(response.$ref)};\n`;
213+
return;
214+
}
215+
216+
// option 2: inline schema
209217
if (response.description) output += comment(response.description);
210218
if (!response.content || !Object.keys(response.content).length) {
211219
const type = statusCode === 204 || Math.floor(+statusCode / 100) === 3 ? "never" : "unknown";
@@ -295,11 +303,22 @@ parameters: {
295303
${createKeys(propertyMapped, Object.keys(propertyMapped))}
296304
}`;
297305
}
306+
307+
// add responses
298308
if (components.responses && Object.keys(components.responses).length) {
299-
finalOutput += `
300-
responses: {
301-
${createKeys(components.responses, Object.keys(components.responses))}
302-
}`;
309+
finalOutput += "\nresponses: {\n"; // open response
310+
for (const [contentType, responseComplete] of Object.entries(components.responses)) {
311+
const { description, ...response } = responseComplete;
312+
if (description) finalOutput += comment(description);
313+
finalOutput += ` "${contentType}": {\n`; // open content type
314+
Object.entries(response).forEach(([property, value]) => {
315+
finalOutput += ` "${property}": {\n`; // open property
316+
finalOutput += ` ${createKeys(value, Object.keys(value))}\n`;
317+
finalOutput += ` }\n`; // close property
318+
});
319+
finalOutput += " }\n"; // close content type
320+
}
321+
finalOutput += "}\n"; // close response
303322
}
304323

305324
// close components wrapper

0 commit comments

Comments
 (0)