swagger-api / swagger-api/swagger-codegen

[Swift3] Array Model Error in Generated Code (v2.3.0)

Open Beginner friendly
#5,592 7 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

There seems to be an error in generated Swift 3 code being created in version 2.3.0 due to the placement of a return statement at the beginning of the "isArrayModel" section in the default Swift 3 Models.mustache file.

The effect of this is that generated Swift 3 model code does not correctly compile when an array model is being used. I noticed this while generating Swift code for an in-house API using the version 2.3.0 branch. Our API does use an array model, hence the triggering of this error.

Swagger-codegen version

2.3.0

Steps to reproduce

To reproduce this, run Swagger Codegen V2.3.0 against an API which uses an array model. The resulting Swift model file will not compile in Xcode due to early return statements.

Suggest a Fix

It looks like the fix for this should be fairly simple and to edit the following section of the default Swift 3 Models.mustache file to edit the following section:

{{^isArrayModel}}
        // Decoder for [{{{classname}}}]
            return Decoders.decode(clazz: [{{{classname}}}].self, source: source, instance: instance)
        }
        // Decoder for {{{classname}}}
        Decoders.addDecoder(clazz: {{{classname}}}.self) { (source: AnyObject, instance: AnyObject?) -> Decoded<{{{classname}}}> in
{{#isEnum}}
...

To add the decoder declaration for an array type as follows (giving the return statement a closure to return from):

{{^isArrayModel}}
        // Decoder for [{{{classname}}}]
        Decoders.addDecoder(clazz: [{{{classname}}}].self) { (source: AnyObject, instance: AnyObject?) -> Decoded<[{{{classname}}}]> in
            return Decoders.decode(clazz: [{{{classname}}}].self, source: source, instance: instance)
        }
        
        // Decoder for {{{classname}}}
        Decoders.addDecoder(clazz: {{{classname}}}.self) { (source: AnyObject, instance: AnyObject?) -> Decoded<{{{classname}}}> in
{{#isEnum}}
...

I have trialled this within the app I am working on and this seems to fix the issue. I can raise a PR for this if it would be useful. It would be useful to know if this change has a wider impact, or if this is simply a bug in the template. The same issue does not appear in the current master version of swagger-codegen, but it does seem as if there have been some changes made to the Swift template for the 2.3.0 version.

I am using the 2.3.0 version as it supports the ability to retrieve data payloads from API errors via the ErrorResponse enum, and therefore fixing this issue is pretty important for our project (we are currently using a branched version with the fix outlined above in place.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/swagger-codegen/src/main/resources/swift3/Models.mustache and inspect the decoder section controlled by isArrayModel. Generate Swift 3 code from an API containing an array model, then compile the resulting model in Xcode. Done means the generated array-model file compiles without early-return errors and ordinary model generation remains intact.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
65/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.