Skip to content
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

Allow nullable with default null option #1415

Merged
merged 4 commits into from
Nov 18, 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
17 changes: 16 additions & 1 deletion packages/plugin-oas/mocks/petStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions packages/plugin-oas/src/SchemaGenerator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-oas/src/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
81 changes: 81 additions & 0 deletions packages/plugin-oas/src/__snapshots__/SchemaGenerator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
]
`;
16 changes: 16 additions & 0 deletions packages/plugin-zod/mocks/petStore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions packages/plugin-zod/src/generators/__snapshots__/toy.ts
Original file line number Diff line number Diff line change
@@ -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();
6 changes: 6 additions & 0 deletions packages/plugin-zod/src/generators/zodGenerator.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ describe('zodGenerator schema', async () => {
dateType: 'string',
},
},
{
name: 'Toy',
path: 'Toy',
input: '../../mocks/petStore.yaml',
options: {},
},
{
name: 'OrderDateTypeFalse',
path: 'Order',
Expand Down
Loading