fix: add support for nullable arrays in Swift 5,6 - #24770
Conversation
There was a problem hiding this comment.
All reported issues were addressed across 6 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
There was a problem hiding this comment.
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("?") ? "?" : ""; |
There was a problem hiding this comment.
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("?") ? "?" : ""; |
There was a problem hiding this comment.
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>
| String nullable = ModelUtils.isNullable(unaliasSchema(items)) && !itemsTypeDeclaration.endsWith("?") ? "?" : ""; | |
| String nullable = (ModelUtils.isNullable(unaliasSchema(items)) || ModelUtils.isNullable(ModelUtils.getReferencedSchema(openAPI, items))) && !itemsTypeDeclaration.endsWith("?") ? "?" : ""; |
PR checklist
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.
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.
Swift5ClientCodegen,Swift6ClientCodegen; non-nullable items are unchanged.nullable: true,x-nullable, and aliased schemas, including nested arrays and sets.Written for commit 5189517. Summary will update on new commits.