datamodel-code-generator
datamodel-code-generator copied to clipboard
Keep underscores from jsonschema 'title' as Pydantic class name
Is your feature request related to a problem? Please describe. When parsing a jsonschema, if jsonschema title contains underscore, a pydantic class name will be created without underscore (it will be joined). Example title: "ACTION_NAME" Example class title name output: "ACTIONNAME"
Describe the solution you'd like I'd like an option to keep the title name
Describe alternatives you've considered I used custom_class_name_generator flag to pass dummy function to keep the name but it still gets translated, function is called, though (i added little print function before return to make sure it's called)
from datamodel_code_generator import generate
from pathlib import Path
def keep_underscore(name):
return name.replace('_', '_')
generate(Path('action_name.json'), output=Path('model.py'), input_file_type='jsonschema', custom_class_name_generator=keep_underscore,)
Additional context I believe there is already a way to do it just can't figure out out what.
Below worked for me
from datamodel_code_generator import reference
def new_snake_to_upper_camel(word: str, delimiter: str = '_') -> str:
return word
reference.snake_to_upper_camel = new_snake_to_upper_camel
It stops the conversion, probably not the recommended approach as it monkey patches the implementation.
Seems like the issue is on this line: https://github.com/koxudaxi/datamodel-code-generator/blob/16043791b02f69e57fc453a65dd0149eab01140f/datamodel_code_generator/parser/base.py#L646
This scoped model resolver
doesn't use the arguments the original one does.