Skip to content

Commit

Permalink
Added support for passing pathlib.Path as a format to JSON schema (#1879
Browse files Browse the repository at this point in the history
)

* Added support for passing pathlib.Path as a format to JSON schema

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Added unit test

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Koudai Aono <koxudaxi@gmail.com>
  • Loading branch information
3 people authored Mar 16, 2024
1 parent 862a98c commit 33d6666
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions datamodel_code_generator/imports.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,6 @@ def remove_referenced_imports(self, reference_path: str) -> None:
IMPORT_DECIMAL = Import.from_full_path('decimal.Decimal')
IMPORT_DATE = Import.from_full_path('datetime.date')
IMPORT_DATETIME = Import.from_full_path('datetime.datetime')
IMPORT_PATH = Import.from_full_path('pathlib.Path')
IMPORT_TIME = Import.from_full_path('datetime.time')
IMPORT_UUID = Import.from_full_path('uuid.UUID')
2 changes: 2 additions & 0 deletions datamodel_code_generator/model/pydantic/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
IMPORT_DATE,
IMPORT_DATETIME,
IMPORT_DECIMAL,
IMPORT_PATH,
IMPORT_TIME,
IMPORT_UUID,
)
Expand Down Expand Up @@ -70,6 +71,7 @@ def type_map_factory(
Types.binary: data_type(type='bytes'),
Types.date: data_type.from_import(IMPORT_DATE),
Types.date_time: data_type.from_import(IMPORT_DATETIME),
Types.path: data_type.from_import(IMPORT_PATH),
Types.password: data_type.from_import(IMPORT_SECRET_STR),
Types.email: data_type.from_import(IMPORT_EMAIL_STR),
Types.uuid: data_type.from_import(IMPORT_UUID),
Expand Down
1 change: 1 addition & 0 deletions datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def get_model_by_path(
'date-time': Types.date_time,
'time': Types.time,
'password': Types.password,
'path': Types.path,
'email': Types.email,
'idn-email': Types.email,
'uuid': Types.uuid,
Expand Down
1 change: 1 addition & 0 deletions datamodel_code_generator/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ class Types(Enum):
date = auto()
date_time = auto()
password = auto()
path = auto()
email = auto()
uuid = auto()
uuid1 = auto()
Expand Down
1 change: 1 addition & 0 deletions docs/supported-data-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Below are the data types and features recognized by datamodel-code-generator for
- password
- email
- idn-email
- path
- uuid (uuid1/uuid2/uuid3/uuid4/uuid5)
- ipv4
- ipv6
Expand Down
1 change: 1 addition & 0 deletions tests/parser/test_jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ def test_parse_nested_array():
('boolean', None, 'bool', None, None),
('string', 'date', 'date', 'datetime', 'date'),
('string', 'date-time', 'datetime', 'datetime', 'datetime'),
('string', 'path', 'Path', 'pathlib', 'Path'),
('string', 'password', 'SecretStr', 'pydantic', 'SecretStr'),
('string', 'email', 'EmailStr', 'pydantic', 'EmailStr'),
('string', 'uri', 'AnyUrl', 'pydantic', 'AnyUrl'),
Expand Down

0 comments on commit 33d6666

Please sign in to comment.