[BUG][Python] UUID properties with default values trigger ClassCastException
Created by: alexras
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.