Skip to content

Commit 19b9383

Browse files
authored
fix: schema.content might be omitted (#1763)
references #369
1 parent 41cb9a2 commit 19b9383

File tree

6 files changed

+31
-3
lines changed

6 files changed

+31
-3
lines changed

.changeset/rich-olives-scream.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
fix: schema.content might be omitted

packages/openapi-typescript/src/transform/header-object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export default function transformHeaderObject(headerObject: HeaderObject, option
1717

1818
if (headerObject.content) {
1919
const type: ts.TypeElement[] = [];
20-
for (const [contentType, mediaTypeObject] of getEntries(headerObject.content, options.ctx)) {
20+
for (const [contentType, mediaTypeObject] of getEntries(headerObject.content ?? {}, options.ctx)) {
2121
const nextPath = `${options.path ?? "#"}/${escapePointer(contentType)}`;
2222
const mediaType =
2323
"$ref" in mediaTypeObject

packages/openapi-typescript/src/transform/request-body-object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function transformRequestBodyObject(
1414
options: TransformNodeOptions,
1515
): ts.TypeNode {
1616
const type: ts.TypeElement[] = [];
17-
for (const [contentType, mediaTypeObject] of getEntries(requestBodyObject.content, options.ctx)) {
17+
for (const [contentType, mediaTypeObject] of getEntries(requestBodyObject.content ?? {}, options.ctx)) {
1818
const nextPath = createRef([options.path, contentType]);
1919
const mediaType =
2020
"$ref" in mediaTypeObject

packages/openapi-typescript/src/transform/response-object.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export default function transformResponseObject(
7474
// content
7575
const contentObject: ts.TypeElement[] = [];
7676
if (responseObject.content) {
77-
for (const [contentType, mediaTypeObject] of getEntries(responseObject.content, options.ctx)) {
77+
for (const [contentType, mediaTypeObject] of getEntries(responseObject.content ?? {}, options.ctx)) {
7878
const property = ts.factory.createPropertySignature(
7979
/* modifiers */ tsModifiers({ readonly: options.ctx.immutable }),
8080
/* name */ tsPropertyIndex(contentType),

packages/openapi-typescript/test/transform/request-body-object.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ describe("transformRequestBodyObject", () => {
4747
content: {
4848
"*/*"?: never;
4949
};
50+
}`,
51+
},
52+
],
53+
[
54+
"no-content",
55+
{
56+
given: {},
57+
want: `{
58+
content: {
59+
"*/*"?: never;
60+
};
5061
}`,
5162
},
5263
],

packages/openapi-typescript/test/transform/response-object.test.ts

+12
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ describe("transformResponseObject", () => {
7070
// options: DEFAULT_OPTIONS,
7171
},
7272
],
73+
[
74+
"no-content",
75+
{
76+
given: {},
77+
want: `{
78+
headers: {
79+
[name: string]: unknown;
80+
};
81+
content?: never;
82+
}`,
83+
},
84+
],
7385
];
7486

7587
for (const [testName, { given, want, options = DEFAULT_OPTIONS, ci }] of tests) {

0 commit comments

Comments
 (0)