-
-
Notifications
You must be signed in to change notification settings - Fork 425
fix: nullable not respected for objects with nullable properties #533 #1550
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
Open
bankyadam
wants to merge
4
commits into
acacode:main
Choose a base branch
from
bankyadam:issue-533
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "swagger-typescript-api": patch | ||
| --- | ||
|
|
||
| Fixed incorrect null handling for nullable objects with nullable properties (#533) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ export type MyObject4 = Record< | |
| { | ||
| content?: string; | ||
| filename?: string | null; | ||
| } | ||
| } | null | ||
| >; | ||
| " | ||
| `; | ||
44 changes: 44 additions & 0 deletions
44
tests/spec/nullable-parent-with-nullable-children/__snapshots__/basic.test.ts.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html | ||
|
|
||
| exports[`nullable-parent-with-nullable-children > nullable parent object with nullable child properties 1`] = ` | ||
| "/* eslint-disable */ | ||
| /* tslint:disable */ | ||
| // @ts-nocheck | ||
| /* | ||
| * --------------------------------------------------------------- | ||
| * ## THIS FILE WAS GENERATED VIA SWAGGER-TYPESCRIPT-API ## | ||
| * ## ## | ||
| * ## AUTHOR: acacode ## | ||
| * ## SOURCE: https://github.com/acacode/swagger-typescript-api ## | ||
| * --------------------------------------------------------------- | ||
| */ | ||
|
|
||
| /** A nullable user object with nullable email property */ | ||
| export type UserWithNullableEmail = { | ||
| id: string; | ||
| email?: string | null; | ||
| name?: string | null; | ||
| } | null; | ||
|
|
||
| /** A nullable profile with all nullable properties */ | ||
| export type Profile = { | ||
| bio?: string | null; | ||
| avatar?: string | null; | ||
| age?: number | null; | ||
| } | null; | ||
|
|
||
| export interface NestedNullableObject { | ||
| outerField: string; | ||
| innerObject?: { | ||
| innerField?: string | null; | ||
| } | null; | ||
| } | ||
|
|
||
| export interface Container { | ||
| /** A nullable user object with nullable email property */ | ||
| user?: UserWithNullableEmail; | ||
| /** A nullable profile with all nullable properties */ | ||
| profile?: Profile; | ||
| } | ||
| " | ||
| `; |
33 changes: 33 additions & 0 deletions
33
tests/spec/nullable-parent-with-nullable-children/basic.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import * as fs from "node:fs/promises"; | ||
| import * as os from "node:os"; | ||
| import * as path from "node:path"; | ||
| import { afterAll, beforeAll, describe, expect, test } from "vitest"; | ||
| import { generateApi } from "../../../src/index.js"; | ||
|
|
||
| describe("nullable-parent-with-nullable-children", async () => { | ||
| let tmpdir = ""; | ||
|
|
||
| beforeAll(async () => { | ||
| tmpdir = await fs.mkdtemp(path.join(os.tmpdir(), "swagger-typescript-api")); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await fs.rm(tmpdir, { recursive: true }); | ||
| }); | ||
|
|
||
| test("nullable parent object with nullable child properties", async () => { | ||
| await generateApi({ | ||
| fileName: "schema", | ||
| input: path.resolve(import.meta.dirname, "schema.json"), | ||
| output: tmpdir, | ||
| silent: true, | ||
| generateClient: false, | ||
| }); | ||
|
|
||
| const content = await fs.readFile(path.join(tmpdir, "schema.ts"), { | ||
| encoding: "utf8", | ||
| }); | ||
|
|
||
| expect(content).toMatchSnapshot(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Missing detection of null in middle of unions
The
hasRootLevelNullcheck only detectsnullat the start or end of a union type string, but not whennullappears in the middle (e.g.,string | null | number). This can happen when aoneOf/anyOfschema has a{ "type": "null" }child positioned between other schema children and the parent is also markednullable: true. In such cases,isNullMissingInTypewould incorrectly returntrue, causingsafeAddNullToTypeto add a redundant| nullto the type. While TypeScript treatsA | null | nullequivalently toA | null, this produces unnecessarily verbose type output. The previous implementation usingincludes(" null")andincludes("null ")would correctly detect null anywhere in the union.