swagger-api / swagger-api/swagger-codegen

[Swift4] Generating empty class/struct when type is string

Open
#9,020 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

The following Swagger 2.0 code (full file here: https://raw.githubusercontent.com/OpenBankingUK/read-write-api-specs/v3.1.0/dist/account-info-swagger.json ):

  "definitions": {
    "AccountId": {
      "description": "A unique and immutable identifier used to identify the account resource. This identifier has no meaning to the account owner.",
      "type": "string",
      "minLength": 1,
      "maxLength": 40
    },

produces the following non-String empty Swift class instead of a string:

import Foundation


/** A unique and immutable identifier used to identify the account resource. This identifier has no meaning to the account owner. */

open class AccountId: Codable {



    

    // Encodable protocol methods

    public func encode(to encoder: Encoder) throws {

        var container = encoder.container(keyedBy: String.self)

    }

    // Decodable protocol methods

    public required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: String.self)

    }
}

This in turn causes issues with JSON decoding etc via Codable.

There are multiple string definitions in the same Swagger file that show the same issue.

Swagger-codegen version

Ran via https://generator.swagger.io/api/gen/clients/swift4 on 19 Dec 2018 (see next section).

Steps to reproduce
curl -X POST -H "content-type:application/json" -d '{"swaggerUrl":"https://raw.githubusercontent.com/OpenBankingUK/read-write-api-specs/v3.1.0/dist/account-info-swagger.json"}' https://generator.swagger.io/api/gen/clients/swift4
Related issues/PRs

https://github.com/swagger-api/swagger-codegen/issues/7707

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

Reproduce the problem with the provided Swagger URL and the generator.swagger.io Swift4 endpoint. Trace how string definitions such as AccountId are mapped into generated Swift models; done means those definitions generate usable String types rather than empty Codable classes, with JSON decoding working as expected.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.