Skip to content

Commit 8e87250

Browse files
authored
Allow schema-less responses (#565)
1 parent 8ba6c48 commit 8e87250

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

src/transform/responses.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,11 @@ export function transformResponsesObj(responsesObj: Record<string, any>, options
4747
// V3
4848
output += ` ${readonly}content: {\n`; // open content
4949
Object.entries(response.content).forEach(([contentType, contentResponse]) => {
50-
output += ` ${readonly}"${contentType}": ${transformSchemaObj(
51-
(contentResponse as any).schema,
52-
options
53-
)};\n`;
50+
const responseType =
51+
contentResponse && (contentResponse as any).schema
52+
? transformSchemaObj((contentResponse as any).schema, options)
53+
: "unknown";
54+
output += ` ${readonly}"${contentType}": ${responseType};\n`;
5455
});
5556
output += ` }\n`; //close content
5657
} else if (response.schema) {

src/utils.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,18 @@ export function nodeType(obj: any): SchemaObjectType | undefined {
6363
export function swaggerVersion(definition: OpenAPI2 | OpenAPI3): 2 | 3 {
6464
// OpenAPI 3
6565
if ("openapi" in definition) {
66+
// OpenAPI version requires semver, therefore will always be string
6667
if (parseInt(definition.openapi, 10) === 3) {
6768
return 3;
6869
}
6970
}
7071

7172
// OpenAPI 2
7273
if ("swagger" in definition) {
74+
// note: swagger 2.0 may be parsed as a number
75+
if (typeof definition.swagger === "number" && Math.round(definition.swagger as number) === 2) {
76+
return 2;
77+
}
7378
if (parseInt(definition.swagger, 10) === 2) {
7479
return 2;
7580
}

tests/paths.test.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,17 @@ describe("transformPathsObj", () => {
155155
}\n`);
156156
});
157157

158-
it("empty responses (#333)", () => {
158+
it("empty responses (#333, #536)", () => {
159159
const emptyResponsesSchema = {
160160
"/no-content": {
161161
get: {
162162
responses: {
163+
200: {
164+
description: "OK",
165+
content: {
166+
"application/json": {},
167+
},
168+
},
163169
204: {
164170
description: "Empty response",
165171
},
@@ -190,6 +196,12 @@ describe("transformPathsObj", () => {
190196
"/no-content": {
191197
get: {
192198
responses: {
199+
/** OK */
200+
200: {
201+
content: {
202+
"application/json": unknown;
203+
};
204+
};
193205
/** Empty response */
194206
204: never;
195207
};
@@ -217,6 +229,12 @@ describe("transformPathsObj", () => {
217229
readonly "/no-content": {
218230
readonly get: {
219231
readonly responses: {
232+
/** OK */
233+
readonly 200: {
234+
readonly content: {
235+
readonly "application/json": unknown;
236+
};
237+
};
220238
/** Empty response */
221239
readonly 204: never;
222240
};

0 commit comments

Comments
 (0)