From 80f0ffb9080fb5906027b87ab90ef40c4686b91f Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Mon, 18 Nov 2024 08:30:19 +0100 Subject: [PATCH 1/4] Fixes #1409 - Allow nullable and default null for schemas --- packages/plugin-oas/src/SchemaGenerator.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/plugin-oas/src/SchemaGenerator.ts b/packages/plugin-oas/src/SchemaGenerator.ts index 080b199cf..48725d675 100644 --- a/packages/plugin-oas/src/SchemaGenerator.ts +++ b/packages/plugin-oas/src/SchemaGenerator.ts @@ -395,8 +395,9 @@ export class SchemaGenerator< const min = schema.minimum ?? schema.minLength ?? schema.minItems ?? undefined const max = schema.maximum ?? schema.maxLength ?? schema.maxItems ?? undefined const nullable = schema.nullable ?? schema['x-nullable'] ?? false + const defaultNullAndNullable = schema.default === null && nullable - if (schema.default !== undefined && !Array.isArray(schema.default)) { + if (schema.default !== undefined && !defaultNullAndNullable && !Array.isArray(schema.default)) { if (typeof schema.default === 'string') { baseItems.push({ keyword: schemaKeywords.default, From acf73d7d25bfa0c94ecbc2df8465824dbadedee4 Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Mon, 18 Nov 2024 08:30:38 +0100 Subject: [PATCH 2/4] Add mocks --- packages/plugin-oas/mocks/petStore.yaml | 17 ++++++++++++++++- packages/plugin-zod/mocks/petStore.yaml | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/packages/plugin-oas/mocks/petStore.yaml b/packages/plugin-oas/mocks/petStore.yaml index f8b213247..282420c71 100644 --- a/packages/plugin-oas/mocks/petStore.yaml +++ b/packages/plugin-oas/mocks/petStore.yaml @@ -136,7 +136,22 @@ components: type: array items: $ref: '#/components/schemas/Pet' - + Toy: + type: object + required: + - id + properties: + id: + type: string + format: uuid + name: + type: string + description: + type: string + default: null + nullable: true + default: null + nullble: true PageSize: type: integer minimum: 1 diff --git a/packages/plugin-zod/mocks/petStore.yaml b/packages/plugin-zod/mocks/petStore.yaml index bd913644f..0edf7d224 100644 --- a/packages/plugin-zod/mocks/petStore.yaml +++ b/packages/plugin-zod/mocks/petStore.yaml @@ -215,6 +215,22 @@ components: required: ['id', 'name'], properties: { id: { type: 'integer', format: 'int64' }, name: { type: 'string' }, tag: { type: 'string' } }, } + Toy: + type: object + required: + - id + properties: + id: + type: string + format: uuid + name: + type: string + description: + type: string + default: null + nullable: true + default: null + nullable: true Error: type: object required: From dfc1bd0c56ad13225ec0dd77c228b683fc7f22d3 Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Mon, 18 Nov 2024 08:30:52 +0100 Subject: [PATCH 3/4] Include schemas in tests --- packages/plugin-oas/src/SchemaGenerator.test.ts | 10 ++++++++++ .../plugin-zod/src/generators/zodGenerator.test.tsx | 6 ++++++ 2 files changed, 16 insertions(+) diff --git a/packages/plugin-oas/src/SchemaGenerator.test.ts b/packages/plugin-oas/src/SchemaGenerator.test.ts index c153bb5b0..63c42a437 100644 --- a/packages/plugin-oas/src/SchemaGenerator.test.ts +++ b/packages/plugin-oas/src/SchemaGenerator.test.ts @@ -36,6 +36,16 @@ describe('SchemaGenerator core', async () => { unknownType: 'unknown', }, }, + { + name: 'Toy', + input: '../mocks/petStore.yaml', + path: 'Toy', + options: { + dateType: 'date', + transformers: {}, + unknownType: 'unknown', + }, + }, { name: 'PageSize', input: '../mocks/petStore.yaml', diff --git a/packages/plugin-zod/src/generators/zodGenerator.test.tsx b/packages/plugin-zod/src/generators/zodGenerator.test.tsx index 69ae7f33c..8e6d992a2 100644 --- a/packages/plugin-zod/src/generators/zodGenerator.test.tsx +++ b/packages/plugin-zod/src/generators/zodGenerator.test.tsx @@ -85,6 +85,12 @@ describe('zodGenerator schema', async () => { dateType: 'string', }, }, + { + name: 'Toy', + path: 'Toy', + input: '../../mocks/petStore.yaml', + options: {}, + }, { name: 'OrderDateTypeFalse', path: 'Order', From d36896fb223520e555271484d346575f16bfeeaf Mon Sep 17 00:00:00 2001 From: Thijmen Stavenuiter Date: Mon, 18 Nov 2024 08:30:59 +0100 Subject: [PATCH 4/4] Update snapshots --- .../SchemaGenerator.test.ts.snap | 81 +++++++++++++++++++ .../src/generators/__snapshots__/toy.ts | 3 + 2 files changed, 84 insertions(+) create mode 100644 packages/plugin-zod/src/generators/__snapshots__/toy.ts diff --git a/packages/plugin-oas/src/__snapshots__/SchemaGenerator.test.ts.snap b/packages/plugin-oas/src/__snapshots__/SchemaGenerator.test.ts.snap index 5a51ee814..c094b0925 100644 --- a/packages/plugin-oas/src/__snapshots__/SchemaGenerator.test.ts.snap +++ b/packages/plugin-oas/src/__snapshots__/SchemaGenerator.test.ts.snap @@ -264,3 +264,84 @@ exports[`SchemaGenerator core > 'Pets' 1`] = ` }, ] `; + +exports[`SchemaGenerator core > 'Toy' 1`] = ` +[ + { + "args": { + "additionalProperties": [], + "properties": { + "description": [ + { + "keyword": "string", + }, + { + "args": { + "format": undefined, + "type": "string", + }, + "keyword": "schema", + }, + { + "keyword": "nullable", + }, + { + "args": "description", + "keyword": "name", + }, + { + "keyword": "nullish", + }, + ], + "id": [ + { + "keyword": "uuid", + }, + { + "args": { + "format": "uuid", + "type": "string", + }, + "keyword": "schema", + }, + { + "args": "id", + "keyword": "name", + }, + ], + "name": [ + { + "keyword": "string", + }, + { + "args": { + "format": undefined, + "type": "string", + }, + "keyword": "schema", + }, + { + "args": "name", + "keyword": "name", + }, + { + "keyword": "optional", + }, + ], + }, + }, + "keyword": "object", + }, + { + "args": { + "format": undefined, + "type": "object", + }, + "keyword": "schema", + }, + { + "args": null, + "keyword": "default", + }, +] +`; diff --git a/packages/plugin-zod/src/generators/__snapshots__/toy.ts b/packages/plugin-zod/src/generators/__snapshots__/toy.ts new file mode 100644 index 000000000..38272500d --- /dev/null +++ b/packages/plugin-zod/src/generators/__snapshots__/toy.ts @@ -0,0 +1,3 @@ +import { z } from "zod"; + + export const toy = z.object({ "id": z.string().uuid(), "name": z.string().optional(), "description": z.string().nullable().nullish() }).nullable();