Skip to content

[BUG] [Swift] fails to generate with integer type enum and default value #4480

@tasuwo

Description

@tasuwo

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

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.

if (p.getEnum() != null && !p.getEnum().isEmpty()) {
if (p.getDefault() != null) {
return "." + escapeText((String) p.getDefault());
}
}

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