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

Option to add Config details to specific classes

Open gk12277 opened this issue 3 years ago • 3 comments

Is your feature request related to a problem? Please describe. There are a number of options which can be given for Config class. Need a option to define config and its attributes for a particular class which will be generated.

Describe the solution you'd like Need a way to specify that a specific class should have a config class with some options

type: object
properties:
    Foo:
       type: object
       properties:
         name:
            type: string

Need some way to specify that generated class Foo should have a config class with arbitrary_types_allowed = True.

class Foo:
  
  Class Config:
        arbitrary_types_allowed = True

Here class Foo has Config with arbitrary_types_allowed

gk12277 avatar Sep 24 '21 06:09 gk12277

@gk12277 There is --custom-template-dir option. And you can add the option to your custom template (jinja2 template). What do you think about it?

koxudaxi avatar Sep 30 '21 15:09 koxudaxi

@koxudaxi --custom-template-dir option is common for all classes right. We wont be able to specify one particular property for one class and another property for another class. eg:

 Class Foo:
   Class Config:
        arbitrary_types_allowed = True
        
 Class Bar:
   Class Config:
        use_enum_values = True                

Here class Foo needs arbitrary_types_allowed and class Bar needs use_enum_values.

gk12277 avatar Oct 11 '21 16:10 gk12277

@gk12277 I'm sorry for my late reply. I think we can use if condition in the jinja2 template. But, I feel good that we can add an option from the CLI option or the schema. Did you have an idea for giving config details to specific classes?

koxudaxi avatar Oct 18 '21 06:10 koxudaxi

you can give the options to config with --extra-template-data EXTRA_TEMPLATE_DATA https://github.com/koxudaxi/datamodel-code-generator/pull/861

koxudaxi avatar Dec 31 '22 15:12 koxudaxi