Add support for nullable schemas #248
Merged
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.
Motivation
Fixes #82.
More background:
In JSON Schema, there are two fields that affect what we represent as optionality in Swift.
required
array.nullable
field, in OpenAPI 3.1, that field is removed and instead the same result is achieved by adding thenull
type in thetype
array. So for a non-nullable string, you'd usetype: string
, and for a nullable string, you'd usetype: [string, null]
.Up until now, we've only supported (1) in how we decide which types are generated as optional, and that seems to be how most adopters control nullability. However, (2) is also supported in JSON Schema, so we should also support it, and as evidenced by the comments in #82, a few folks have hit the lack of nullability support in Swift OpenAPI Generator.
Modifications
This PR takes nullability on the schema into account when deciding whether to generate a type as optional or not.
Since this is a source-breaking change, it's introduced behind the feature flag
nullableSchemas
, and it'll become default behavior in 0.3.0.Most of this change is just propagating the feature flag to the right place.
Result
Marking a schema as nullable will result in an optional type being generated.
Test Plan
Added a snippet test, both with and without the feature flag, which shows the change.