swagger-api / swagger-api/swagger-codegen

[SWIFT] Bug generating models with enum discriminator

Open
#9,582 1 comment 2 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

Issue appears when generating swift4 client and in *.yaml file you have model with enum discriminator. Example of models definition:

  Foo:
    discriminator: modelType
    properties:
      modelName:
        type: string
      modelType:
        enum:
          - Bar
        type: string
    required:
      - modelType
    type: object

  Bar:
    allOf:
      - $ref: '#/definitions/Foo'
      - properties:
          name:
            type: string
        required: 
          - name
        type: object
    type: object

Generated models:
Foo:

public struct Foo: Codable {

    public enum ModelType: String, Codable { 
        case bar = "Bar"
    }
    public var modelType: ModelType

    public init(modelType: ModelType) {
        self.modelType = modelType
    }

}

Bar:

public struct Bar: Codable {

    public enum ModelType: String, Codable { 
    }
    public var modelType: ModelType
    public var name: String

    public init(modelType: ModelType, name: String) {
        self.modelType = modelType
        self.name = name
    }

}

In Bar struct you can see enum without any case which won't compile.
When using the same yaml file but generating swift3 client - everything is fine.

Swagger-codegen version

swagger-codegen version is 2.4.7

Command line used for generation

swagger-codegen generate -i api.yaml -l swift4 -o client

Suggest a fix/enhancement

Possible fix may be generating Bar in the following way:

public struct Bar: Codable {

    public typealias ModelType = Foo.ModelType

    public var modelType: ModelType
    public var name: String

    public init(modelType: ModelType, name: String) {
        self.modelType = modelType
        self.name = name
    }

}

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 by running swagger-codegen generate -i api.yaml -l swift4 -o client with the YAML models shown in the issue, then compare the generated Swift 4 output with Swift 3 output. Trace where the inherited enum discriminator is represented in the generated Bar model; done means Swift 4 output compiles without an empty enum while preserving the discriminator cases.

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
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.