Skip to content

fix: Correct handling of default parameter values in referenced component schema #1746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/dull-birds-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": patch
---

fix: Correct handling of default parameter values in referenced component schema
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,8 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
("default" in v &&
options.ctx.defaultNonNullable &&
!options.path?.includes("parameters") &&
!options.path?.includes("requestBody")) // parameters can’t be required, even with defaults
!options.path?.includes("requestBody") &&
!options.path?.includes("requestBodies")) // can’t be required, even with defaults
? undefined
: QUESTION_TOKEN;
let type =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,62 @@ describe("transformRequestBodyObject", () => {
},
},
],
[
"requestBodies -> POST data with default values",
{
given: {
content: {
"application/json": {
schema: {
properties: {
card_title: {
type: "string",
title: "Card Title",
default: "Social Profile",
},
template: {
type: "string",
title: "Template",
},
socials: {
type: "object",
title: "Socials",
default: {},
},
},
type: "object",
required: ["template"],
title: "Create",
description: "Social Profile schema for create.",
},
},
},
description: "description",
},
want: `{
content: {
"application/json": {
/**
* Card Title
* @default Social Profile
*/
card_title?: string;
/** Template */
template: string;
/**
* Socials
* @default {}
*/
socials?: Record<string, never>;
};
};
}`,
options: {
path: "#/components/requestBodies/social_profiles__Create/application~1json",
ctx: { ...DEFAULT_CTX },
},
},
],
];

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