swagger-api / swagger-api/swagger-codegen
[Swift] handle decoding of unknown enum types if optional
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
If I have a model class, say vehicle which has an enum 'type' which can have two values, 'car' and 'motorbike'. This class is generated with Swagger and instantiated from a JSON response received from my backend(using the codable protocol). Say I ship my App version 1.0 with this.
Later on I decide to add another vehicle type in my backend which will be handled in my app 1.1, 'bus', my app version is was released earlier will not be able to deserialize Vehicle. It will throw an error even if the type is not 'required':
dataCorrupted(Swift.DecodingError.Context(codingPath: ["type"], debugDescription: "Cannot initialize ModelType from invalid String value Something", underlyingError: nil))
Swagger-codegen version
swagger-codegen-cli-2.3.0-20171207.164637-326
Swagger declaration file content or url
Command line used for generation
Steps to reproduce
Related issues/PRs
Suggest a fix/enhancement
It would be much nicer to have an option to deserialize optional unknown enum types to nil(like the option READ_UNKNOWN_ENUM_VALUES_AS_NULL in Jackson for Java) instead of throwing an error. This can be done by doing:
type = try? container.decodeIfPresent(ModelType.self, forKey: "type")
instead of
type = try container.decodeIfPresent(ModelType.self, forKey: "type")
in the generated method of the class containing the enum
public required init(from decoder: Decoder) throws
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the Swift generator template that emits the model's required init(from decoder:) method and compare its handling of optional enum properties with the issue's suggested decoding behavior. Confirm the generated model accepts an unknown value for an optional enum without throwing while preserving normal decoding for known values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100