Skip to content

Commit e705909

Browse files
authored
fix: Correct handling of default parameter values in referenced component schema (#1746)
* fix: Correct handling of default parameter values in referenced component schema * chore: lint code * chore: lint code * Create dull-birds-pump.md
1 parent 184ff13 commit e705909

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

.changeset/dull-birds-pump.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"openapi-typescript": patch
3+
---
4+
5+
fix: Correct handling of default parameter values in referenced component schema

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,8 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
457457
("default" in v &&
458458
options.ctx.defaultNonNullable &&
459459
!options.path?.includes("parameters") &&
460-
!options.path?.includes("requestBody")) // parameters can’t be required, even with defaults
460+
!options.path?.includes("requestBody") &&
461+
!options.path?.includes("requestBodies")) // can’t be required, even with defaults
461462
? undefined
462463
: QUESTION_TOKEN;
463464
let type =

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

+56
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,62 @@ describe("transformRequestBodyObject", () => {
8989
},
9090
},
9191
],
92+
[
93+
"requestBodies -> POST data with default values",
94+
{
95+
given: {
96+
content: {
97+
"application/json": {
98+
schema: {
99+
properties: {
100+
card_title: {
101+
type: "string",
102+
title: "Card Title",
103+
default: "Social Profile",
104+
},
105+
template: {
106+
type: "string",
107+
title: "Template",
108+
},
109+
socials: {
110+
type: "object",
111+
title: "Socials",
112+
default: {},
113+
},
114+
},
115+
type: "object",
116+
required: ["template"],
117+
title: "Create",
118+
description: "Social Profile schema for create.",
119+
},
120+
},
121+
},
122+
description: "description",
123+
},
124+
want: `{
125+
content: {
126+
"application/json": {
127+
/**
128+
* Card Title
129+
* @default Social Profile
130+
*/
131+
card_title?: string;
132+
/** Template */
133+
template: string;
134+
/**
135+
* Socials
136+
* @default {}
137+
*/
138+
socials?: Record<string, never>;
139+
};
140+
};
141+
}`,
142+
options: {
143+
path: "#/components/requestBodies/social_profiles__Create/application~1json",
144+
ctx: { ...DEFAULT_CTX },
145+
},
146+
},
147+
],
92148
];
93149

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

0 commit comments

Comments
 (0)