Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
5 tasks done
janodev opened this issue Jan 30, 2025 · 1 comment
Closed
5 tasks done

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

janodev opened this issue Jan 30, 2025 · 1 comment

Comments

@janodev
Copy link
Contributor

janodev commented Jan 30, 2025

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
@4brunu
Copy link
Contributor

4brunu commented Jan 31, 2025

Hi, could you please create a PR with the fix?
I can help with the review and merge.
Thanks

janodev added a commit to janodev/openapi-generator that referenced this issue Jan 31, 2025
…ix OpenAPITools#20560

Cases previously generated as "case type[Int]([Int])" become "case typeArrayOfInt([Int])"
janodev added a commit to janodev/openapi-generator that referenced this issue Jan 31, 2025
janodev added a commit to janodev/openapi-generator that referenced this issue Jan 31, 2025
…ix OpenAPITools#20560

* Add test for file reproducing OpenAPITools#20560
* Cases previously generated as "case type[Int]([Int])" become "case typeArrayOfInt([Int])"
janodev added a commit to janodev/openapi-generator that referenced this issue Jan 31, 2025
…ix OpenAPITools#20560

* Cases previously generated as "case type[Int]([Int])" become "case typeArrayOfInt([Int])"
@4brunu 4brunu closed this as completed in e553180 Feb 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants