Fix: honor nullable: true on object types with additionalProperties#65
Open
david-velasquez-v wants to merge 2 commits into
Open
Fix: honor nullable: true on object types with additionalProperties#65david-velasquez-v wants to merge 2 commits into
nullable: true on object types with additionalProperties#65david-velasquez-v wants to merge 2 commits into
Conversation
`nonArraySchemaObjectTypeToTs` wraps the non-dictionary return in its
`case "object"` branch via `appendNullToUnion(..., item.nullable)`, but
the dictionary return (`additionalProperties` is truthy) did not. The
same pattern exists in `schemaObjectTypeNode`, which short-circuits to
`createDictionaryType` before reaching the switch and also dropped
`item.nullable`. Every other type branch handles `item.nullable`;
these two dictionary paths were the only exceptions.
Result: schemas like
type: object
additionalProperties: true
nullable: true
generated `{ [key: string]: any }` instead of
`{ [key: string]: any } | null`, silently dropping the null case.
Added regression coverage for both shapes:
- `additionalProperties: true` + `nullable: true`
- `additionalProperties: { type: "string" }` + `nullable: true`
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
src/common/util.tshandlesnullable: truecorrectly for every type case except whenadditionalPropertiesis set on anobject. There are two code paths that produce a dictionary type, and both forgot to wrap withappendNullToUnion:schemaObjectTypeNodeshort-circuits onitem.additionalPropertiesbefore reaching the switch.nonArraySchemaObjectTypeToTscase "object"returns directly from theadditionalPropertiesbranch.This PR adds the missing wrap at both sites.
Repro
Given the following OpenAPI 3.0 schema:
Before:
type SomeNullableDictionary = { [key: string]: any };(no| null)After:
type SomeNullableDictionary = { [key: string]: any } | null;Same applies to
additionalProperties: { type: "string" }withnullable: true, etc.Test
Added a regression test in
spec/common/types.spec.tscovering both shapes:type: object, additionalProperties: true, nullable: truetype: object, additionalProperties: { type: "string" }, nullable: true