Skip to content

[Swift3] Wrong enum types generated when enum is of custom type #5452

Closed
@davidpasztor

Description

@davidpasztor
Description

Swift enum types are incorrect using swagger-codegen when the type should be a custom class
codegen generates an enum of Strings instead of an enum of custom types
Code generated by codegen:

public enum SomeType: String { 
        case foo = "Foo"
        case bar = "Bar"
        case bee = "Bar"
    }

I have manually managed to fix the enum and this is the correct Swift enum with custom types, where Foo, Bar and Bee are all custom classes that inherit from the same class in which SomeType is declared.

public enum SomeType {
        case foo(Foo)
        case bar(Bar)
        case bee(Bee)
    }

The Java version of swagger-codegen has been fixed already and generates the following, correct code:

public enum SomeTypeEnum {
    @SerializedName("Foo")
    FOO("Foo"),
    
    @SerializedName("Bar")
    BAR("Bar"),
    
    @SerializedName("Bee")
    BEE("Bee");

    private String value;

    SomeTypeEnum(String value) {
      this.value = value;
    }

    @Override
    public String toString() {
      return String.valueOf(value);
    }
  }
Swagger-codegen version

2.2.2

Swagger declaration file content or url
Something:
    type: object
    description: "Something description"
    allOf:
      - $ref: '#/definitions/_Item'
      - type: object
        discriminator: customType
        required:
          - customType
          - category
        properties:
          propertyA:
            type: array
            description: Some description
            items:
              type: string
              pattern: '[a-zA-Z0-9-]{1,20}'
          propertyB:
            type: array
            description: Another description
            items:
              type: string
              pattern: '[a-zA-Z0-9-]{1,20}'
          propertyC:
            description: propertC
            enum:
              - first
              - second
              - other
            type: string
            default: first
          propertyD:
            description: something else
            type: string
            pattern: '[a-zA-Z -]{1,70}'
          customType:
            type: string
            description: customType description
            enum:
              - Foo
              - Bar
              - Bee
Command line used for generation

swagger-codegen generate -i /path/spec.yaml -l swift3

Steps to reproduce

Generate swift3 code using codegen from command line with the above specification file

Related issues

#4484

Suggest a Fix

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions