yonaskolb / SwagGen

OpenAPI/Swagger 3.0 Parser and Swift code generator
MIT License
625 stars 146 forks source link

Fix number/integer enum case formatting #312

Closed kodlian closed 1 year ago

kodlian commented 1 year ago

CodeFormatter uses String(describing: to generate value and case names if no enum names are provided. This results in wrong integer enumations that do not compile:

Take the following spec

"foo: {
     "type": "integer",
      "enum": [
            1,
            2,
            3
       ],
 }

Currently, it will generate:

enum Foo: Int, Codable, Equatable, CaseIterable {
    case `true` = true
    case _20 = 2.0
    case _30 = 3.0
}

This MR fixes the issue by leveraging the stringValue property from NSNumber.

enum Foo: Int, Codable, Equatable, CaseIterable {
    case _1 = 1
    case _2 = 2
    case _3 = 3
}
yonaskolb commented 1 year ago

Thanks @kodlian! Could you add an example enum somewhere in the test spec and commit the diffs after running the tests https://github.com/yonaskolb/SwagGen/blob/master/Specs/TestSpec/spec.yml And also a changelog entry

kodlian commented 1 year ago

Thanks for asking me to add a test JSON spec file @yonaskolb. It makes me realize that the JSON dictionary I was using was not correct as the testing one was working. This was fixing it, just not in the right place. I will close the ticket.