Skip to content

[BUG] [Swift5] Enums are incorrectly URL coded (uses name vs. value) #13984

Open
@rsoffer

Description

@rsoffer

Bug Report Checklist

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

Actual:

/** Unique type for the UI card. */
public enum GenericCardType: String, Codable, CaseIterable {
    case srSummary = "SR_SUMMARY"
}

Expected:

/** Unique type for the UI card. */
public enum GenericCardType: String, Codable, CaseIterable {
    case srSummary = "SR_SUMMARY"
}

extension GenericCardType: CustomStringConvertible {
    public var description: String {
        return rawValue
    }
}
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When using enums as properties in a URL query, the enum is encoded using its name vs. its rawValue.

In our case:
enum is: case srSummary = "SR_SUMMARY"

When the request is created, the path has /srSummary instead of /SR_SUMMARY.

The code to create the URL string looks like this:

let typePreEscape = "\(APIHelper.mapValueToPathItem(type))"
        let typePostEscape = typePreEscape.addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? ""
        localVariablePath = localVariablePath.replacingOccurrences(of: "{type}", with: typePostEscape, options: .literal, range: nil)
        let localVariableURLString = OpenAPIClientAPI.basePath + localVariablePath

This just uses the description of the enum, which is the name. Simplest solution is to add a CustomStringConvertible extension.

openapi-generator version

6.2.1

OpenAPI declaration file content or url

GenericCardType:
type: string
description: Unique type for the UI card.
enum:
- SR_SUMMARY

Suggest a fix

Add a CustomStringConvertible extension for each enum to resolve to the rawValue instead of the name:

extension GenericCardType: CustomStringConvertible {
    public var description: String {
        return rawValue
    }
}

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