Skip to content

[BUG] [swift] wrong json encoding in some enums #4593

@4brunu

Description

@4brunu

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

When encoding or decoding Swift string enums to json or to a http url, sometimes the value that is used it's the case name instead of the rawValue.

openapi-generator version

4.2.1

OpenAPI declaration file content or url

Here is an example that show's the bug.
https://gist.github.com/4brunu/0bb9de272f84794554bcf73db7c514bb

Command line used for generation
	openapi-generator generate \
		--input-spec openapi.yaml \
		--generator-name swift4 \
		--config openapi-config.json \
		--output openapi/ 

Here is a link to a sample project that demonstrates the issue.

https://github.com/4brunu/openapi-generator-swift-enum-http-parameter

Steps to reproduce

The problem is here
https://github.com/4brunu/openapi-generator-swift-enum-http-parameter/blob/d64978a556ed21c1d78730b2fc102a91eda50a1d/openapi/OpenAPIClient/Classes/OpenAPIs/APIs/OperationsAPI.swift#L39-L50

The problem is in line 39
category is of type MotivesCategory which is an enum, so the value that should be used is the rawValue, otherwise, the value that will be used it's the name of the case, not the value of the case.

To fix the problem, the line 39 should be - "category": category.rawValue

let me show an example to explain the problem
let categoryTemp = MotivesCategory.createOperation
print(categoryTemp) // output: createOperation
print(categoryTemp.rawValue) // output: CREATE_OPERATION which is the right value
Suggest a fix

The thing that I don't understand is the here

"{{baseName}}": {{paramName}}{{^isEnum}}{{#isInteger}}{{^required}}?{{/required}}.encodeToJSON(){{/isInteger}}{{#isLong}}{{^required}}?{{/required}}.encodeToJSON(){{/isLong}}{{/isEnum}}{{#isEnum}}{{^isContainer}}{{^required}}?{{/required}}.rawValue{{/isContainer}}{{/isEnum}}{{#isDate}}{{^required}}?{{/required}}.encodeToJSON(){{/isDate}}{{#isDateTime}}{{^required}}?{{/required}}.encodeToJSON(){{/isDateTime}}

If the type is an enum and it's not a container, which is not, it should add the .rawValue, but it's not.
In some enum's it add's the rawValue but not here.
Is this not an enum?

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