koxudaxi / datamodel-code-generator

Pydantic model and dataclasses.dataclass generator for easy conversion of JSON, OpenAPI, JSON Schema, and YAML data sources.
https://koxudaxi.github.io/datamodel-code-generator/
MIT License
2.74k stars 301 forks source link

workaround: better class names #2042

Open straz opened 3 months ago

straz commented 3 months ago

Is your feature request related to a problem? Please describe. The current behavior generates unhelpful enumerated class names, like Address1, Address2, Address3 when the source has multiple definitions for Address.

The current codebase includes a comment:

# TODO: Improve naming for nested ClassName

Describe the solution you'd like The enclosed code is a workaround. We define PatchJsonSchemaParser which can be used like this:

from datamodel_code_generator import generate

with patch('datamodel_code_generator.parser.jsonschema.JsonSchemaParser', PatchJsonSchemaParser):
      generate(...)

This generates more appropriate and verbose classnames, like CustomerAddress, UserAddress, VendorAddress.

Describe alternatives you've considered Ugly regex on the output

Additional context This seems to be an FAQ for this library: https://github.com/koxudaxi/datamodel-code-generator/discussions/1792

Here is the proposed workaround: patch_datamodel_code_generator.py (remove .md suffix as github won't accept a .py file here)

tsuga commented 2 weeks ago

@straz, I came across this workaround. Thank you for sharing this. It saved the day! I believe this should be supported officially.