apple / apple/swift-openapi-generator

Swift OpenAPI Generator Fails to Decode anyOf Structure for nullable Field

Open
#286 43 comments 0 reactions 0 assignees View on GitHub
area/generator kind/support status/blocked
Dominant language
Swift
Stars
2k
Forks
182
Avg merge
13h 28m
Merged PRs (30d)
5

Description

When using the Swift OpenAPI Generator and runtime to decode a JSON payload from an API endpoint, a decoding error occurs for a field defined with the anyOf keyword in the OpenAPI spec. The error message is "The anyOf structure did not decode into any child schema."

Here is a simplified snippet of the OpenAPI spec that defines the response schema:

```
"ResponseSchema": {
"properties": {
"status": { "type": "string", "title": "Status" },
"timestampUTC": {
"type": "string",
"format": "date-time",
"title": "Timestamputc"
},
"errorCode": {
"anyOf": [{ "type": "integer" }, { "type": "null" }],
"title": "Errorcode"
},
"errorMessage": {
"anyOf": [{ "type": "string" }, { "type": "null" }],
"title": "Errormessage"
},
"data": {
"type": "object"
}
},
"type": "object",
"required": [
"status",
"timestampUTC",
"errorCode",
"errorMessage",
"data"
]
}
```

Here is a snippet of the JSON payload received from the API endpoint:

```
{
"status": "success",
"timestampUTC": "2023-09-20T12:20:29.606380Z",
"errorCode": null,
"errorMessage": null,
"data": {}
}

```

### Error Message:
`DecodingError: valueNotFound errorCodePayload - at CodingKeys(stringValue: "errorCode", intValue: nil): The anyOf structure did not decode into any child schema. (underlying error: )`

### Steps to Reproduce:
Generate Swift code using the provided OpenAPI spec.
Make an API request to receive the JSON payload.
Attempt to decode the JSON payload using the generated Swift code.
Expected Behavior:
The JSON payload should be successfully decoded into the corresponding Swift model.

### Actual Behavior:
Decoding fails with the error message mentioned above.

### Environment:
Swift OpenAPI Generator version: 0.2.2
Swift version: 5.9
Runtime: 0.2.3
OpenAPI Spec version: 3.1.0

OpenAPI Generator config:
```
generate:
- types
- client
featureFlags:
- nullableSchemas
```

Contributor guide

Open the contributing guide

Research direction

Generate the Swift types and client from the shown OpenAPI 3.1 document with the nullableSchemas feature, then decode the provided payload using Swift 5.9 and runtime 0.2.3. Trace the nullable anyOf fields that contain null and confirm that the payload decodes successfully without the reported error.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, swift
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.