OpenAPITools / OpenAPITools/openapi-generator

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

Open
#3,516 5 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

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

openapi-generator version

4.0.3

OpenAPI declaration file content or url
---
components:
  schemas:
    MyModel:
      properties:
        id:
          default: "00000000-0000-4000-8000-000000000000"
          format: uuid
          type: string
      title: MyModel
      type: object

info:
  title: 'My API'
  version: 0.1.0dev

openapi: 3.0.2

paths: {}
Command line used for generation

docker run --rm -v ${PWD}:/local --env 'PYTHON_POST_PROCESS_FILE="/usr/local/bin/yapf -i"' openapitools/openapi-generator-cli:v4.0.3 generate -i /local/swagger.yml -g python -o /local/build

Steps to reproduce

Run the generator with the above declaration file; it will throw an exception that looks like this:

Exception in thread "main" java.lang.RuntimeException: Could not process model 'MyModel'.Please make sure that your schema is correct!
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:470)
        at org.openapitools.codegen.DefaultGenerator.generate(DefaultGenerator.java:920)
        at org.openapitools.codegen.cmd.Generate.run(Generate.java:396)
        at org.openapitools.codegen.OpenAPIGenerator.main(OpenAPIGenerator.java:60)
Caused by: java.lang.ClassCastException: java.util.UUID cannot be cast to java.lang.String
        at org.openapitools.codegen.languages.PythonClientCodegen.toDefaultValue(PythonClientCodegen.java:643)
        at org.openapitools.codegen.DefaultCodegen.fromProperty(DefaultCodegen.java:1981)
        at org.openapitools.codegen.DefaultCodegen.addVars(DefaultCodegen.java:3627)
        at org.openapitools.codegen.DefaultCodegen.addVars(DefaultCodegen.java:3575)
        at org.openapitools.codegen.DefaultCodegen.fromModel(DefaultCodegen.java:1845)
        at org.openapitools.codegen.DefaultGenerator.processModels(DefaultGenerator.java:1197)
        at org.openapitools.codegen.DefaultGenerator.generateModels(DefaultGenerator.java:465)
        ... 3 more

If you remove the default and run the generator again, code generation will succeed.

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/2209 - similar exception, with a different cause.

Suggest a fix

Rather than just casting to a string, perhaps just calling p.getDefault().toString() would work.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with PythonClientCodegen.java at the toDefaultValue method around line 643, using the minimal OpenAPI YAML and Docker generation command from the issue to reproduce the exception. Done means the Python generator completes successfully for a UUID property with a default value and produces the client model without the ClassCastException.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.