From c01585036af05bc96f7f24e89c9b72ce60e7d057 Mon Sep 17 00:00:00 2001 From: Koudai Aono Date: Thu, 30 Sep 2021 23:27:51 +0900 Subject: [PATCH] Fix a bug for # field (#558) --- datamodel_code_generator/reference.py | 2 +- .../expected/main/main_jsonschema_special_field_name/output.py | 1 + tests/data/jsonschema/special_field_name.json | 3 +++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/datamodel_code_generator/reference.py b/datamodel_code_generator/reference.py index 95615dd27..8fe24d3f1 100644 --- a/datamodel_code_generator/reference.py +++ b/datamodel_code_generator/reference.py @@ -146,7 +146,7 @@ def get_valid_name( if not name: name = self.empty_field_name if name[0] == '#': - name = name[1:] + name = name[1:] or self.empty_field_name # TODO: when first character is a number name = re.sub(r'\W', '_', name) if name[0].isnumeric(): diff --git a/tests/data/expected/main/main_jsonschema_special_field_name/output.py b/tests/data/expected/main/main_jsonschema_special_field_name/output.py index 8e9cb3482..93b41ff4f 100644 --- a/tests/data/expected/main/main_jsonschema_special_field_name/output.py +++ b/tests/data/expected/main/main_jsonschema_special_field_name/output.py @@ -15,3 +15,4 @@ class SpecialField(BaseModel): class_: Optional[int] = Field(None, alias='class') class_s: Optional[int] = Field(None, alias="class's") class_s_1: Optional[str] = Field(None, alias='class-s') + _: Optional[str] = Field(None, alias='#') diff --git a/tests/data/jsonschema/special_field_name.json b/tests/data/jsonschema/special_field_name.json index 1d959ba81..09e78d7b1 100644 --- a/tests/data/jsonschema/special_field_name.json +++ b/tests/data/jsonschema/special_field_name.json @@ -17,6 +17,9 @@ }, "class-s": { "type": "string" + }, + "#": { + "type": "string" } } } \ No newline at end of file