Skip to content

[BUG][Swift6] associated value appears as suffix of enum case #20560

@janodev

Description

@janodev

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

File example.yaml passes validation at https://validator.swagger.io/ but generates code like this:

public enum CreateCompletionRequestPrompt: Codable, JSONEncodable, Hashable {
    case typeString(String)
    case type[Int]([Int])  <----------------- case is suffixed with the type of the associated value
    case type[String]([String])
    case type[[Int]]([[Int]])

    public func encode(to encoder: Encoder) throws {
        var container = encoder.singleValueContainer()
        switch self {
        case .typeString(let value):
            try container.encode(value)
        case .type[Int](let value):
            try container.encode(value)
        case .type[String](let value):
            try container.encode(value)
        case .type[[Int]](let value):
            try container.encode(value)
        }
    }

    public init(from decoder: Decoder) throws {
        let container = try decoder.singleValueContainer()
        if let value = try? container.decode(String.self) {
            self = .typeString(value)
        } else if let value = try? container.decode([Int].self) {
            self = .type[Int](value)
        } else if let value = try? container.decode([String].self) {
            self = .type[String](value)
        } else if let value = try? container.decode([[Int]].self) {
            self = .type[[Int]](value)
        } else {
            throw DecodingError.typeMismatch(Self.Type.self, .init(codingPath: decoder.codingPath, debugDescription: "Unable to decode instance of CreateCompletionRequestPrompt"))
        }
    }
}

It can be trivially fixed manually by replacing the name with typeOfArrayOfString etc. but still annoying to generate faulty code each time.

openapi-generator version
openapi-generator-cli 7.12.0-SNAPSHOT
  commit : 8c337f0
OpenAPI declaration file content or url

See https://gist.github.com/janodev/103b048498c94d5f3d44751687727a5b#file-example-yaml-L44-L68

Generation Details

Default options: ./openapi-generator-cli.sh generate -i example.yaml -g swift6 -o openai

Steps to reproduce
./openapi-generator-cli.sh generate -i example.yaml -g swift6 -o openai

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions