Skip to content

Commit e8abcba

Browse files
committed
Add --to-camel option to convert from snake_case to camelCase
1 parent 83ec9e9 commit e8abcba

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

pydantic2ts/cli/script.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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,11 @@ def generate_json_schema(models: List[Type[BaseModel]]) -> str:
179190

180191

181192
def generate_typescript_defs(
182-
module: str, output: str, exclude: Tuple[str] = (), json2ts_cmd: str = "json2ts"
193+
module: str,
194+
output: str,
195+
exclude: Tuple[str] = (),
196+
to_camel: bool = False,
197+
json2ts_cmd: str = "json2ts",
183198
) -> None:
184199
"""
185200
Convert the pydantic models in a python module into typescript interfaces.
@@ -205,7 +220,7 @@ def generate_typescript_defs(
205220

206221
logger.info("Generating JSON schema from pydantic models...")
207222

208-
schema = generate_json_schema(models)
223+
schema = generate_json_schema(models, to_camel)
209224
schema_dir = mkdtemp()
210225
schema_file_path = os.path.join(schema_dir, "schema.json")
211226

@@ -254,6 +269,11 @@ def parse_cli_args(args: List[str] = None) -> argparse.Namespace:
254269
help="name of a pydantic model which should be omitted from the results.\n"
255270
"This option can be defined multiple times.",
256271
)
272+
parser.add_argument(
273+
"--to-camel",
274+
action="store_true",
275+
help="flag to convert model field names from snake_case to CamelCase.",
276+
)
257277
parser.add_argument(
258278
"--json2ts-cmd",
259279
dest="json2ts_cmd",

0 commit comments

Comments
 (0)