OpenAPITools / OpenAPITools/openapi-generator

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

Open
#13,984 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Swift Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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
    }
}

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 from the generated Swift URL-building code that calls APIHelper.mapValueToPathItem(type), and compare enum names with their raw values for the provided GenericCardType example. Check how generated enums implement string conversion and verify that a URL path uses SR_SUMMARY rather than srSummary. The issue does not name a repository file or test to run.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
api
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.