datamodel-code-generator icon indicating copy to clipboard operation
datamodel-code-generator copied to clipboard

Enum, infinite loop with exotic enum value

Open PTank opened this issue 3 years ago • 0 comments
trafficstars

Describe the bug When I run the datamodel-code-generator for json-schema the field name converter with enum do an infinite loop.

To Reproduce

Example schema:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "rent_unit": {
  	"type": "string",
  	"default": "€/m²/year",
  	"enum": [
  	  "€/m²/month",
  	  "€/m²/year",
  	  "€/year",
  	  "€/month"
  	]
     }
    }
  }
}

Used commandline:

$ datamodel-codegen  --input schema.json  --input-file-type jsonschema --output model.py

Expected behavior Create Enum or crash with an error.

Version:

  • OS: linux-gnu
  • Python version: 3.9.9
  • datamodel-code-generator version: 0.12.0

Additional context I think the error come from https://github.com/koxudaxi/datamodel-code-generator/blob/fc1c25756a0b41ff68ce541c644456363cd1418b/datamodel_code_generator/reference.py#L162=

--enum-field-as-literal all can fix the problem by skipping Enum creation.

PTank avatar Apr 18 '22 20:04 PTank