Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #12605
Closed
Open
Issue created Jun 15, 2022 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

[BUG][Csharp] UUID properties with default values trigger ClassCastException

Created by: alekskuc

Bug Report Checklist

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

If I define a schema that has a UUID property, and that property has a default value, the csharp code generator throws a ClassCastException.

openapi-generator version

6.0.0

OpenAPI declaration file content or url

https://gist.github.com/alekskuc/ebd082d005b735caf7d018c779beb1ae

Generation Details
java -jar openapi-generator-cli.jar generate -i swagger.yaml -g csharp   -o Api_2022-06-15-130401
[main] INFO  o.o.codegen.DefaultGenerator - Generating with dryRun=false
[main] INFO  o.o.c.ignore.CodegenIgnoreProcessor - No .openapi-generator-ignore file found.
[main] INFO  o.o.codegen.DefaultGenerator - OpenAPI Generator: csharp (client)
[main] INFO  o.o.codegen.DefaultGenerator - Generator 'csharp' is considered stable.
[main] INFO  o.o.c.l.AbstractCSharpCodegen - Environment variable CSHARP_POST_PROCESS_FILE not defined so the C# code may not be properly formatted by uncrustify (0.66 or later) or other code formatter. To define it, try `export CSHARP_POST_PROCESS_FILE="/usr/local/bin/uncrustify --no-backup" && export UNCRUSTIFY_CONFIG=/path/to/uncrustify-rules.cfg` (Linux/Mac). Note: replace /path/to with the location of uncrustify-rules.cfg
[main] INFO  o.o.c.l.AbstractCSharpCodegen - NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).
[main] INFO  o.o.c.languages.CSharpClientCodegen - Generating code for .NET Framework v4.5
[main] INFO  o.o.codegen.TemplateManager - writing file \Api_2022-06-15-130401\src\Org.OpenAPITools\Model\Pet.cs
[main] INFO  o.o.codegen.TemplateManager - writing file \Api_2022-06-15-130401\src\Org.OpenAPITools.Test\Model\PetTests.cs
[main] INFO  o.o.codegen.TemplateManager - writing file \Api_2022-06-15-130401\docs\Pet.md
Exception in thread "main" java.lang.RuntimeException: Could not process operation:
  Tag: class Tag {
    name: default
    description: null
    externalDocs: null
}
  Operation: find pet by id
  Resource: get /pets/{id}
  Schemas: {Pet=class ObjectSchema {
    class Schema {
        type: object
        format: null
        $ref: null
        description: null
        title: null
        multipleOf: null
        maximum: null
        exclusiveMaximum: null
        minimum: null
        exclusiveMinimum: null
        maxLength: null
        minLength: null
        pattern: null
        maxItems: null
        minItems: null
        uniqueItems: null
        maxProperties: null
        minProperties: null
        required: [id]
        not: null
        properties: {id=class UUIDSchema {
            class Schema {
                type: string
                format: uuid
                $ref: null
                description: null
                title: null
                multipleOf: null
                maximum: null
                exclusiveMaximum: null
                minimum: null
                exclusiveMinimum: null
                maxLength: null
                minLength: null
                pattern: null
                maxItems: null
                minItems: null
                uniqueItems: null
                maxProperties: null
                minProperties: null
                required: null
                not: null
                properties: null
                additionalProperties: null
                nullable: null
                readOnly: null
                writeOnly: null
                example: null
                externalDocs: null
                deprecated: null
                discriminator: null
                xml: null
            }
        }}
        additionalProperties: null
        nullable: null
        readOnly: null
        writeOnly: null
        example: {id=046b6c7f-0b8a-43b9-b35d-6489e6daee91}
        externalDocs: null
        deprecated: null
        discriminator: null
        xml: null
    }
}}
  Exception: java.util.UUID cannot be cast to java.lang.String
        at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1167)
        at org.openapitools.codegen.DefaultGenerator.processPaths(DefaultGenerator.java:1058)
        at org.openapitools.codegen.DefaultGenerator.generateApis(DefaultGenerator.java:553)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:895)
        at org.openapitools.codegen.cmd.Generate.execute(Generate.java:449)
        at org.openapitools.codegen.cmd.OpenApiGeneratorCommand.run(OpenApiGeneratorCommand.java:32)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:66)
Caused by: java.lang.ClassCastException: java.util.UUID cannot be cast to java.lang.String
        at org.openapitools.codegen.languages.AbstractCSharpCodegen.toDefaultValue(AbstractCSharpCodegen.java:983)
        at org.openapitools.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:3507)
        at org.openapitools.codegen.DefaultCodegen.fromParameter(DefaultCodegen.java:4626)
        at org.openapitools.codegen.DefaultCodegen.fromOperation(DefaultCodegen.java:4162)
        at org.openapitools.codegen.DefaultGenerator.processOperation(DefaultGenerator.java:1135)
        ... 6 more
Steps to reproduce

Try to generate csharp code

java -jar openapi-generator-cli.jar generate -i swagger.yaml -g csharp

using spec in file https://gist.github.com/alekskuc/ebd082d005b735caf7d018c779beb1ae

Related issues/PRs

#3516

Suggest a fix

Replace line 983 in AbstractCSharpCodegen.java

String _default = (String) p.getDefault();

With

String _default = p.getDefault().toString();
Assignee
Assign to
Time tracking