From e705909f2b15d8b207be4be22f329e29e5f6444b Mon Sep 17 00:00:00 2001 From: Hongkun Peng Date: Wed, 17 Jul 2024 22:09:25 +0800 Subject: [PATCH] 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 --- .changeset/dull-birds-pump.md | 5 ++ .../src/transform/schema-object.ts | 3 +- .../transform/request-body-object.test.ts | 56 +++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 .changeset/dull-birds-pump.md diff --git a/.changeset/dull-birds-pump.md b/.changeset/dull-birds-pump.md new file mode 100644 index 000000000..c4cad9f52 --- /dev/null +++ b/.changeset/dull-birds-pump.md @@ -0,0 +1,5 @@ +--- +"openapi-typescript": patch +--- + +fix: Correct handling of default parameter values in referenced component schema diff --git a/packages/openapi-typescript/src/transform/schema-object.ts b/packages/openapi-typescript/src/transform/schema-object.ts index 0885324b3..42f9dbd26 100644 --- a/packages/openapi-typescript/src/transform/schema-object.ts +++ b/packages/openapi-typescript/src/transform/schema-object.ts @@ -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 = diff --git a/packages/openapi-typescript/test/transform/request-body-object.test.ts b/packages/openapi-typescript/test/transform/request-body-object.test.ts index 4f8965350..7a94498c1 100644 --- a/packages/openapi-typescript/test/transform/request-body-object.test.ts +++ b/packages/openapi-typescript/test/transform/request-body-object.test.ts @@ -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; + }; + }; +}`, + options: { + path: "#/components/requestBodies/social_profiles__Create/application~1json", + ctx: { ...DEFAULT_CTX }, + }, + }, + ], ]; for (const [testName, { given, want, options = DEFAULT_OPTIONS, ci }] of tests) {