Skip to content

Commit

Permalink
Fix snake_case generation (#1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Jan 20, 2023
1 parent 41de6c4 commit 933ca10
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
2 changes: 1 addition & 1 deletion datamodel_code_generator/reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def context_variable(
setter(previous_value)


_UNDER_SCORE_1: Pattern[str] = re.compile(r'(.)([A-Z][a-z]+)')
_UNDER_SCORE_1: Pattern[str] = re.compile(r'([^_])([A-Z][a-z]+)')
_UNDER_SCORE_2: Pattern[str] = re.compile('([a-z0-9])([A-Z])')


Expand Down
17 changes: 17 additions & 0 deletions tests/data/expected/main/main_json_snake_case_field/output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# generated by datamodel-codegen:
# filename: snake_case.json
# timestamp: 2019-07-26T00:00:00+00:00

from __future__ import annotations

from pydantic import BaseModel, Field


class Model(BaseModel):
snake_case: str
camel_case: str = Field(..., alias='camelCase')
kebab_case: str = Field(..., alias='kebab-case')
pascal_case: str = Field(..., alias='PascalCase')
upper_case: str = Field(..., alias='UPPER_CASE')
dev_info: str = Field(..., alias='Dev_Info')
clone_device: str = Field(..., alias='CLONE_Device')
9 changes: 9 additions & 0 deletions tests/data/json/snake_case.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"snake_case": "Snake case",
"camelCase": "Camel case",
"kebab-case": "Kebab case",
"PascalCase": "Pascal case",
"UPPER_CASE": "Upper case",
"Dev_Info": "example 1",
"CLONE_Device": "example 2"
}
26 changes: 26 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4567,3 +4567,29 @@ def test_main_use_default_kwarg():
)
with pytest.raises(SystemExit):
main()


@freeze_time('2019-07-26')
def test_main_json_snake_case_field():
with TemporaryDirectory() as output_dir:
output_file: Path = Path(output_dir) / 'output.py'
return_code: Exit = main(
[
'--input',
str(JSON_DATA_PATH / 'snake_case.json'),
'--output',
str(output_file),
'--input-file-type',
'json',
'--snake-case-field',
]
)
assert return_code == Exit.OK
assert (
output_file.read_text()
== (
EXPECTED_MAIN_PATH / 'main_json_snake_case_field' / 'output.py'
).read_text()
)
with pytest.raises(SystemExit):
main()

0 comments on commit 933ca10

Please sign in to comment.