[BUG] [Swift] fails to generate with integer type enum and default value
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] What's the version of OpenAPI Generator used?
- [x] Have you search for related issues/PRs?
- [x] What's the actual output vs expected output?
- [ ] [Optional] Bounty to sponsor the fix (example)
Description
I declare an enum with integer type and default value in my OpenAPI declaration. But while generating a swift4 client, the generate command fails with following error.
[main] INFO o.o.codegen.languages.Swift4Codegen - NOTE: To enable file post-processing, ‘enablePostProcessFile’ must be set to true (--enable-post-process-file for CLI).
[main] WARN o.o.codegen.utils.URLPathUtils - ‘host’ (OAS 2.0) or ‘servers’ (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
[main] WARN o.o.codegen.utils.URLPathUtils - ‘host’ (OAS 2.0) or ‘servers’ (OAS 3.0) not defined in the spec. Default to [http://localhost] for server URL [http://localhost/]
Exception in thread “main” java.lang.RuntimeException: Could not process model ‘NewItem’.Please make sure that your schema is correct!
at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:472)
at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:923)
at org.openapitools.codegen.cmd.Generate.run(Generate.java:416)
at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:61)
Caused by: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at org.openapitools.codegen.languages.Swift4Codegen.toDefaultValue(Swift4Codegen.java:558)
at org.openapitools.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:2079)
at org.openapitools.codegen.DefaultCodegen.addVars(DefaultCodegen.java:3752)
at org.openapitools.codegen.DefaultCodegen.addVars(DefaultCodegen.java:3700)
at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:1938)
at org.openapitools.codegen.languages.Swift4Codegen.fromModel(Swift4Codegen.java:691)
at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1166)
at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:467)
... 3 more
openapi-generator version
master (4.2.1)
OpenAPI declaration file content or url
openapi: 3.0.0
info:
description: ''
version: 1.0.0
title: ''
paths:
/items:
post:
description: ''
operationId: addItem
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/NewItem'
responses:
'405':
description: Invalid input
components:
schemas:
NewItem:
properties:
size:
type: integer
enum:
- 10
- 150
- 300
default: 150
Expected output swift code is as follows.
import Foundation
public struct NewItem: Codable {
public enum Size: Int, Codable {
case _10 = 10
case _150 = 150
case _300 = 300
}
public var size: Size? = ._150
public init(size: Size?) {
self.size = size
}
}
Command line used for generation
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
-i ./sample.yaml \
-g swift4 \
-o output
Steps to reproduce
Related issues/PRs
Suggest a fix
In toDefaultValue, Convert to String by toString if the schema is enum and not string type.
https://github.com/OpenAPITools/openapi-generator/blob/c2ad14ea02f47dcca2acd8458428777b38e752fa/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift4Codegen.java#L556-L560
👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.
The team will review the labels and make any necessary changes.