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.94k stars 6.03k forks source link

[Swift4] Primitive typealias produces void class #6946

Open pavel-zdenek opened 6 years ago

pavel-zdenek commented 6 years ago
Description

We have a handful of primitive "typealias" definitions in yaml, like so

TokenId:
  type: string

which in codegen 2.2 produces typealias TokenId = String

but in codegen 2.3.3 and 3.0.0 produces

open class TokenId: Codable {
    public func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: String.self)
    }
    public required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: String.self)
    }
}

which is a "void" implementation, as it does not declare any usable properties.

Swagger-codegen version

2.3.3, 3.0.0

Swagger declaration file content or url

paths is not in focus of this issue and may be empty per OpenAPI spec, but codegen throws NPE without it.

swagger: "2.0"
info:
  title: "Test"
  version: "1.0"
paths:
  '/':
    get:
      responses:
        '200':
          schema:
            $ref: '#/definitions/TokenId'
definitions:
  TokenId:
    type: string
Command line used for generation

java -jar swagger-codegen-cli-x.y.z.jar generate -i swagger.yaml -l swift4 -o ./codegen

Steps to reproduce

Run codegen with the above yaml

Related issues/PRs

Possibly, vaguely, in other language(s) https://github.com/swagger-api/swagger-codegen/issues/3483 https://github.com/swagger-api/swagger-codegen/issues/4804 but mind that this was working up to 2.2, just not since 2.3 with introduction of Codable.

Suggest a fix/enhancement

As Swift primitives are Codables right away, use typealias as in 2.2 ?

wing328 commented 6 years ago

Is it correct to say that if you replace the typealias with "type: string", the result is good again?

pavel-zdenek commented 6 years ago

Sure. If UserId is not used in the endpoints code, then its content is not relevant. If we really badly wanted to use Codables, we could do a mass replace. But we got many places of UserId usage, it was evolving from int through string to uuid even, and we always had to change just couple of lines of code. We consider primitive typealiases useful, have more of them, and must stick with codegen 2.2 to keep them usable.

greuze commented 5 years ago

Same is happening with typescript-node, with 2.3.3 version the alias works, but with 2.4.x it generates an empty object (in my case the alias is to a number).

Using inline definition works, but I must copy the minimum and maximum to every use of the type, instead of reuse them with the alias.