Added support for passing pathlib.Path as a format to JSON schema (#1879)

* 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>
This commit is contained in:
Brandon Sorensen
2024-03-16 11:57:14 +01:00
committed by GitHub
parent 862a98cb7e
commit 33d6666882
6 changed files with 7 additions and 0 deletions

View File

@@ -116,5 +116,6 @@ IMPORT_DICT = Import.from_full_path('typing.Dict')
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')

View File

@@ -9,6 +9,7 @@ from datamodel_code_generator.imports import (
IMPORT_DATE,
IMPORT_DATETIME,
IMPORT_DECIMAL,
IMPORT_PATH,
IMPORT_TIME,
IMPORT_UUID,
)
@@ -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),

View File

@@ -118,6 +118,7 @@ json_schema_data_formats: Dict[str, Dict[str, Types]] = {
'date-time': Types.date_time,
'time': Types.time,
'password': Types.password,
'path': Types.path,
'email': Types.email,
'idn-email': Types.email,
'uuid': Types.uuid,

View File

@@ -548,6 +548,7 @@ class Types(Enum):
date = auto()
date_time = auto()
password = auto()
path = auto()
email = auto()
uuid = auto()
uuid1 = auto()

View File

@@ -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

View File

@@ -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'),