Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
5 of 6 tasks
alekskuc opened this issue Jun 15, 2022 · 2 comments · Fixed by #13698
Closed
5 of 6 tasks

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

alekskuc opened this issue Jun 15, 2022 · 2 comments · Fixed by #13698

Comments

@alekskuc
Copy link

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();
@wing328
Copy link
Member

wing328 commented Jun 16, 2022

@alekskuc thanks for reporting the issue. Can you please file a PR with the suggested fix?

@alekskuc
Copy link
Author

@wing328 I've tried to file a PR but it turns out currently I can't do it.

Please delete this PR #12640 branch and file changes only in the AbstractCSharpCodegen.java file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants