Skip to content

fix: add support for nullable arrays in Swift 5,6 - #24770

Open
HarelM wants to merge 2 commits into
OpenAPITools:masterfrom
HarelM:issue-22355
Open

fix: add support for nullable arrays in Swift 5,6#24770
HarelM wants to merge 2 commits into
OpenAPITools:masterfrom
HarelM:issue-22355

Conversation

@HarelM

@HarelM HarelM commented Aug 24, 2026

Copy link
Copy Markdown

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

CC: @jgavris @ehyche @Edubits @jaz-ah @4brunu @dydus0x14


Summary by cubic

Respect nullable array items in Swift 5 and 6 generators. Previously arrays and sets always used non-optional element types; now when item schemas are nullable, elements are optional (e.g., [String?], Set<String?>). Fixes #22355.

  • Affected generators: Swift5ClientCodegen, Swift6ClientCodegen; non-nullable items are unchanged.
  • Detects nullability via nullable: true, x-nullable, and aliased schemas, including nested arrays and sets.
  • Update call sites to handle nil elements; decoding arrays containing null now succeeds.

Written for commit 5189517. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 6 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 issues found across 6 files (changes from recent commits).

Prompt for AI agents (unresolved issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java:783">
P2: Nullable array items that are `$ref`s to an enum, composed, or object model are never marked Optional. `unaliasSchema` deliberately returns the ref unchanged for enum/composed/model refs, so `isNullable` on its result is always false there, and the item loses the `?` even when the referenced schema declares `nullable: true` (or X-NULLABLE). The added test only covers an aliased primitive (`NullableString -> [String?]`). Resolve the referenced schema for the nullability check instead of relying on `unaliasSchema` when `items` carries a `$ref`.</violation>
</file>

<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java:829">
P2: When an array item is a `$ref` to a nullable object model, `unaliasSchema(items)` preserves the `$ref`, so `isNullable` never sees the referenced model's `nullable: true` and the generated Swift element type remains non-optional. Check the referenced schema's nullability as well as the unaliased item schema.</violation>
</file>

Reply with feedback, questions, or to request a fix.

Re-trigger cubic


private String getItemsTypeDeclaration(Schema items) {
String itemsTypeDeclaration = getTypeDeclaration(items);
String nullable = ModelUtils.isNullable(unaliasSchema(items)) && !itemsTypeDeclaration.endsWith("?") ? "?" : "";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: Nullable array items that are $refs to an enum, composed, or object model are never marked Optional. unaliasSchema deliberately returns the ref unchanged for enum/composed/model refs, so isNullable on its result is always false there, and the item loses the ? even when the referenced schema declares nullable: true (or X-NULLABLE). The added test only covers an aliased primitive (NullableString -> [String?]). Resolve the referenced schema for the nullability check instead of relying on unaliasSchema when items carries a $ref.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java, line 783:

<comment>Nullable array items that are `$ref`s to an enum, composed, or object model are never marked Optional. `unaliasSchema` deliberately returns the ref unchanged for enum/composed/model refs, so `isNullable` on its result is always false there, and the item loses the `?` even when the referenced schema declares `nullable: true` (or X-NULLABLE). The added test only covers an aliased primitive (`NullableString -> [String?]`). Resolve the referenced schema for the nullability check instead of relying on `unaliasSchema` when `items` carries a `$ref`.</comment>

<file context>
@@ -780,7 +780,7 @@ public String getTypeDeclaration(Schema p) {
     private String getItemsTypeDeclaration(Schema items) {
         String itemsTypeDeclaration = getTypeDeclaration(items);
-        String nullable = items.getNullable() != null && items.getNullable() && !itemsTypeDeclaration.endsWith("?") ? "?" : "";
+        String nullable = ModelUtils.isNullable(unaliasSchema(items)) && !itemsTypeDeclaration.endsWith("?") ? "?" : "";
         return itemsTypeDeclaration + nullable;
     }
</file context>

private String getItemsTypeDeclaration(Schema items) {
String itemsTypeDeclaration = getTypeDeclaration(items);
String nullable = items.getNullable() != null && items.getNullable() && !itemsTypeDeclaration.endsWith("?") ? "?" : "";
String nullable = ModelUtils.isNullable(unaliasSchema(items)) && !itemsTypeDeclaration.endsWith("?") ? "?" : "";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: When an array item is a $ref to a nullable object model, unaliasSchema(items) preserves the $ref, so isNullable never sees the referenced model's nullable: true and the generated Swift element type remains non-optional. Check the referenced schema's nullability as well as the unaliased item schema.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift6ClientCodegen.java, line 829:

<comment>When an array item is a `$ref` to a nullable object model, `unaliasSchema(items)` preserves the `$ref`, so `isNullable` never sees the referenced model's `nullable: true` and the generated Swift element type remains non-optional. Check the referenced schema's nullability as well as the unaliased item schema.</comment>

<file context>
@@ -826,7 +826,7 @@ public String getTypeDeclaration(Schema p) {
     private String getItemsTypeDeclaration(Schema items) {
         String itemsTypeDeclaration = getTypeDeclaration(items);
-        String nullable = items.getNullable() != null && items.getNullable() && !itemsTypeDeclaration.endsWith("?") ? "?" : "";
+        String nullable = ModelUtils.isNullable(unaliasSchema(items)) && !itemsTypeDeclaration.endsWith("?") ? "?" : "";
         return itemsTypeDeclaration + nullable;
     }
</file context>
Suggested change
String nullable = ModelUtils.isNullable(unaliasSchema(items)) && !itemsTypeDeclaration.endsWith("?") ? "?" : "";
String nullable = (ModelUtils.isNullable(unaliasSchema(items)) || ModelUtils.isNullable(ModelUtils.getReferencedSchema(openAPI, items))) && !itemsTypeDeclaration.endsWith("?") ? "?" : "";

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [SWIFT] Array items cannot be nullable

2 participants