@@ -124,7 +124,7 @@ def clean_output_file(output_filename: str) -> None:
124
124
f .writelines (new_lines )
125
125
126
126
127
- def clean_schema (schema : Dict [str , Any ]) -> None :
127
+ def clean_schema (schema : Dict [str , Any ], to_camel : bool ) -> None :
128
128
"""
129
129
Clean up the resulting JSON schemas by:
130
130
@@ -134,14 +134,25 @@ def clean_schema(schema: Dict[str, Any]) -> None:
134
134
2) Getting rid of the useless "An enumeration." description applied to Enums
135
135
which don't have a docstring.
136
136
"""
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
139
150
140
151
if "enum" in schema and schema .get ("description" ) == "An enumeration." :
141
152
del schema ["description" ]
142
153
143
154
144
- def generate_json_schema (models : List [Type [BaseModel ]]) -> str :
155
+ def generate_json_schema (models : List [Type [BaseModel ]], to_camel : bool ) -> str :
145
156
"""
146
157
Create a top-level '_Master_' model with references to each of the actual models.
147
158
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:
168
179
schema = json .loads (master_model .schema_json ())
169
180
170
181
for d in schema .get ("definitions" , {}).values ():
171
- clean_schema (d )
182
+ clean_schema (d , to_camel )
172
183
173
184
return json .dumps (schema , indent = 2 )
174
185
@@ -179,7 +190,7 @@ def generate_json_schema(models: List[Type[BaseModel]]) -> str:
179
190
180
191
181
192
def 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"
183
194
) -> None :
184
195
"""
185
196
Convert the pydantic models in a python module into typescript interfaces.
@@ -205,7 +216,7 @@ def generate_typescript_defs(
205
216
206
217
logger .info ("Generating JSON schema from pydantic models..." )
207
218
208
- schema = generate_json_schema (models )
219
+ schema = generate_json_schema (models , to_camel )
209
220
schema_dir = mkdtemp ()
210
221
schema_file_path = os .path .join (schema_dir , "schema.json" )
211
222
@@ -254,6 +265,11 @@ def parse_cli_args(args: List[str] = None) -> argparse.Namespace:
254
265
help = "name of a pydantic model which should be omitted from the results.\n "
255
266
"This option can be defined multiple times." ,
256
267
)
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
+ )
257
273
parser .add_argument (
258
274
"--json2ts-cmd" ,
259
275
dest = "json2ts_cmd" ,
0 commit comments