Skip to content

Commit 49a9082

Browse files
authored
Fix response key for HTTP ranges (openapi-ts#1010)
OpenAPI allows to use HTTP ranges for response keys, see: https://swagger.io/docs/specification/describing-responses/#status-codes Before these changes, openapi-typescript would output invalid TypeScript when processing schemas with HTTP ranges.
1 parent e91b1a8 commit 49a9082

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/transform/operation-object.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,13 @@ export default function transformOperationObject(
129129
output.push(indent(`responses: {`, indentLv));
130130
indentLv++;
131131
for (const [responseCode, responseObject] of getEntries(operationObject.responses, ctx.alphabetize)) {
132+
const key = escObjKey(responseCode);
132133
const c = getSchemaObjectComment(responseObject, indentLv);
133134
if (c) output.push(indent(c, indentLv));
134135
if ("$ref" in responseObject) {
135136
output.push(
136137
indent(
137-
`${responseCode}: ${transformSchemaObject(responseObject, {
138+
`${key}: ${transformSchemaObject(responseObject, {
138139
path: `${path}/responses/${responseCode}`,
139140
ctx,
140141
})};`,
@@ -146,7 +147,7 @@ export default function transformOperationObject(
146147
path: `${path}/responses/${responseCode}`,
147148
ctx: { ...ctx, indentLv },
148149
});
149-
output.push(indent(`${responseCode}: ${responseType};`, indentLv));
150+
output.push(indent(`${key}: ${responseType};`, indentLv));
150151
}
151152
}
152153
indentLv--;

test/path-item-object.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,21 @@ describe("Path Item Object", () => {
3939
},
4040
},
4141
404: { $ref: 'components["responses"]["NotFound"]' },
42+
"5xx": {
43+
description: "Server error",
44+
content: {
45+
"application/json": {
46+
schema: {
47+
type: "object",
48+
properties: {
49+
message: { type: "string" },
50+
code: { type: "string" },
51+
},
52+
required: ["message"],
53+
},
54+
},
55+
},
56+
},
4257
},
4358
},
4459
post: {
@@ -66,6 +81,15 @@ describe("Path Item Object", () => {
6681
};
6782
};
6883
404: components["responses"]["NotFound"];
84+
/** @description Server error */
85+
"5xx": {
86+
content: {
87+
"application/json": {
88+
message: string;
89+
code?: string;
90+
};
91+
};
92+
};
6993
};
7094
};
7195
post: {

0 commit comments

Comments
 (0)