swagger-api / swagger-api/swagger-codegen

[Swift4] Primitive typealias produces void class

Open
#6,946 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Swift Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

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 ?

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 with the Swift4 generator path used by the java -jar swagger-codegen-cli-x.y.z.jar generate command and the handling of primitive definitions such as TokenId in the supplied Swagger YAML. Compare generated output with the expected typealias TokenId = String, and use the reproduction command to verify that primitive aliases no longer become empty Codable classes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, swift
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.