@@ -124,7 +124,7 @@ def clean_output_file(output_filename: str) -> None:
124124 f .writelines (new_lines )
125125
126126
127- def clean_schema (schema : Dict [str , Any ]) -> None :
127+ def clean_schema (schema : Dict [str , Any ], to_camel : bool ) -> None :
128128 """
129129 Clean up the resulting JSON schemas by:
130130
@@ -134,14 +134,25 @@ def clean_schema(schema: Dict[str, Any]) -> None:
134134 2) Getting rid of the useless "An enumeration." description applied to Enums
135135 which don't have a docstring.
136136 """
137- for prop in schema .get ("properties" , {}).values ():
138- prop .pop ("title" , None )
137+ update_props = {}
138+ for name , value in schema .get ("properties" , {}).items ():
139+ value .pop ("title" , None )
140+ if to_camel and ("_" in name ):
141+ name = "" .join (
142+ [
143+ word .capitalize () if i != 0 else word
144+ for i , word in enumerate (name .split ("_" ))
145+ ]
146+ )
147+ update_props [name ] = value
148+
149+ schema ["properties" ] = update_props
139150
140151 if "enum" in schema and schema .get ("description" ) == "An enumeration." :
141152 del schema ["description" ]
142153
143154
144- def generate_json_schema (models : List [Type [BaseModel ]]) -> str :
155+ def generate_json_schema (models : List [Type [BaseModel ]], to_camel : bool ) -> str :
145156 """
146157 Create a top-level '_Master_' model with references to each of the actual models.
147158 Generate the schema for this model, which will include the schemas for all the
@@ -168,7 +179,7 @@ def generate_json_schema(models: List[Type[BaseModel]]) -> str:
168179 schema = json .loads (master_model .schema_json ())
169180
170181 for d in schema .get ("definitions" , {}).values ():
171- clean_schema (d )
182+ clean_schema (d , to_camel )
172183
173184 return json .dumps (schema , indent = 2 )
174185
@@ -179,7 +190,7 @@ def generate_json_schema(models: List[Type[BaseModel]]) -> str:
179190
180191
181192def generate_typescript_defs (
182- module : str , output : str , exclude : Tuple [str ] = (), json2ts_cmd : str = "json2ts"
193+ module : str , output : str , exclude : Tuple [str ] = (), to_camel : bool = False , json2ts_cmd : str = "json2ts"
183194) -> None :
184195 """
185196 Convert the pydantic models in a python module into typescript interfaces.
@@ -205,7 +216,7 @@ def generate_typescript_defs(
205216
206217 logger .info ("Generating JSON schema from pydantic models..." )
207218
208- schema = generate_json_schema (models )
219+ schema = generate_json_schema (models , to_camel )
209220 schema_dir = mkdtemp ()
210221 schema_file_path = os .path .join (schema_dir , "schema.json" )
211222
@@ -254,6 +265,11 @@ def parse_cli_args(args: List[str] = None) -> argparse.Namespace:
254265 help = "name of a pydantic model which should be omitted from the results.\n "
255266 "This option can be defined multiple times." ,
256267 )
268+ parser .add_argument (
269+ "--to-camel" ,
270+ action = "store_true" ,
271+ help = "flag to convert model field names from snake_case to CamelCase." ,
272+ )
257273 parser .add_argument (
258274 "--json2ts-cmd" ,
259275 dest = "json2ts_cmd" ,
0 commit comments