Skip to content

Commit

Permalink
fix: Correct handling of default parameter values in referenced compo…
Browse files Browse the repository at this point in the history
…nent schema (#1746)

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

* chore: lint code

* chore: lint code

* Create dull-birds-pump.md
  • Loading branch information
phk422 authored Jul 17, 2024
1 parent 184ff13 commit e705909
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
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
3 changes: 2 additions & 1 deletion packages/openapi-typescript/src/transform/schema-object.ts
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

0 comments on commit e705909

Please sign in to comment.