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

Keep underscores from jsonschema 'title' as Pydantic class name

Open maretodoric opened this issue 1 year ago • 2 comments

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.

maretodoric avatar May 12 '23 14:05 maretodoric

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.

peterchenadded avatar Jun 26 '23 12:06 peterchenadded

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.

AurisAudentis avatar Nov 27 '23 14:11 AurisAudentis