swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
16.96k stars 6.04k forks source link

[SWIFT] Bug generating models with enum discriminator #9582

Open AlexCogniteq opened 5 years ago

AlexCogniteq commented 5 years ago
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
    }

}
goors commented 1 year ago

Same here.