Skip to content

Conversation

czechboy0
Copy link
Contributor

Motivation

Fixes #82.

More background:

In JSON Schema, there are two fields that affect what we represent as optionality in Swift.

  1. In object schemas, not including a property in the required array.
  2. In all schemas, marking the schema as nullable. In OpenAPI 3.0, that used to be done using a dedicated nullable field, in OpenAPI 3.1, that field is removed and instead the same result is achieved by adding the null type in the type array. So for a non-nullable string, you'd use type: string, and for a nullable string, you'd use type: [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.

@czechboy0 czechboy0 merged commit 5dbd05d into apple:main Sep 6, 2023
@czechboy0 czechboy0 deleted the hd-nullable-schemas branch September 6, 2023 08:12
@Kyle-Ye
Copy link
Contributor

Kyle-Ye commented Sep 17, 2023

If I understand correctly, (2) has not been solved after this PR. So maybe #82 should be reopened instead of closed.

@czechboy0
Copy link
Contributor Author

@Kyle-Ye please confirm that you're including the feature flag, the tests in this PR passed so at least some scenarios are fixed.

@Kyle-Ye
Copy link
Contributor

Kyle-Ye commented Sep 17, 2023

@Kyle-Ye please confirm that you're including the feature flag, the tests in this PR passed so at least some scenarios are fixed.

Sorry, I wrongly thought it was already enabled by default in the current release.(0.2.1)

Following the doc here, I got an error after adding the feature flag.

image

I fixed it by using a main branch dependency after reading the source code that 0.2.1 release does not include the nullableSchemas feature flag.

The nullable issue is indeed solved in my scenario. Waiting for the 0.3.0 release 🎉

@czechboy0 czechboy0 added the 🔨 semver/patch No public API change. label Sep 18, 2023
@thunderball1
Copy link

Does anyone know why it disappeared from main? There is only empty in enums for feature flags

@czechboy0
Copy link
Contributor Author

czechboy0 commented May 31, 2024

All feature flags got enabled by default in 0.3.0 and removed. Same in 1.0.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support nullable values
4 participants