Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix to schema generation for IPv{4,6}{Address,Interface,Network} #532

Merged
merged 5 commits into from May 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions HISTORY.rst
Expand Up @@ -10,6 +10,7 @@ v0.26 (unreleased)
* fix return type hint for ``create_model``, #526 by @dmontagu
* **Breaking Change:** fix ``.dict(skip_keys=True)`` skipping values set via alias (this involves changing
``validate_model()`` to always returns ``Tuple[Dict[str, Any], Set[str], Optional[ValidationError]]``), #517 by @sommd
* fix to schema generation for ``IPv4Address``, ``IPv6Address``, ``IPv4Interface``, ``IPv6Interface``, ``IPv4Network``, ``IPv6Network`` #532 by @euri10

v0.25 (2019-05-05)
..................
Expand Down
42 changes: 42 additions & 0 deletions docs/schema_mapping.py
Expand Up @@ -254,20 +254,62 @@
'Pydantic standard "format" extension',
''
],
[
'IPv4Address',
'string',
'{"format": "ipv4"}',
'JSON Schema Validation',
''
],
tiangolo marked this conversation as resolved.
Show resolved Hide resolved
[
'IPv6Address',
'string',
'{"format": "ipv6"}',
'JSON Schema Validation',
''
],
tiangolo marked this conversation as resolved.
Show resolved Hide resolved
[
'IPvAnyAddress',
'string',
'{"format": "ipvanyaddress"}',
'Pydantic standard "format" extension',
'IPv4 or IPv6 address as used in ``ipaddress`` module',
],
[
'IPv4Interface',
'string',
'{"format": "ipv4interface"}',
'Pydantic standard "format" extension',
'IPv4 interface as used in ``ipaddress`` module',
],
[
'IPv6Interface',
'string',
'{"format": "ipv6interface"}',
'Pydantic standard "format" extension',
'IPv6 interface as used in ``ipaddress`` module',
],
[
'IPvAnyInterface',
'string',
'{"format": "ipvanyinterface"}',
'Pydantic standard "format" extension',
'IPv4 or IPv6 interface as used in ``ipaddress`` module',
],
[
'IPv4Network',
'string',
'{"format": "ipv4network"}',
'Pydantic standard "format" extension',
'IPv4 network as used in ``ipaddress`` module',
],
[
'IPv6Network',
'string',
'{"format": "ipv6network"}',
'Pydantic standard "format" extension',
'IPv6 network as used in ``ipaddress`` module',
],
[
'IPvAnyNetwork',
'string',
Expand Down
16 changes: 14 additions & 2 deletions pydantic/schema.py
Expand Up @@ -22,6 +22,12 @@
DirectoryPath,
EmailStr,
FilePath,
IPv4Address,
IPv4Interface,
IPv4Network,
IPv6Address,
IPv6Interface,
IPv6Network,
IPvAnyAddress,
IPvAnyInterface,
IPvAnyNetwork,
Expand Down Expand Up @@ -635,9 +641,15 @@ def field_singleton_sub_fields_schema(
(time, {'type': 'string', 'format': 'time'}),
(timedelta, {'type': 'number', 'format': 'time-delta'}),
(Json, {'type': 'string', 'format': 'json-string'}),
(IPvAnyAddress, {'type': 'string', 'format': 'ipvanyaddress'}),
(IPvAnyInterface, {'type': 'string', 'format': 'ipvanyinterface'}),
(IPv4Network, {'type': 'string', 'format': 'ipv4network'}),
(IPv6Network, {'type': 'string', 'format': 'ipv6network'}),
(IPvAnyNetwork, {'type': 'string', 'format': 'ipvanynetwork'}),
(IPv4Interface, {'type': 'string', 'format': 'ipv4interface'}),
(IPv6Interface, {'type': 'string', 'format': 'ipv6interface'}),
(IPvAnyInterface, {'type': 'string', 'format': 'ipvanyinterface'}),
(IPv4Address, {'type': 'string', 'format': 'ipv4'}),
(IPv6Address, {'type': 'string', 'format': 'ipv6'}),
(IPvAnyAddress, {'type': 'string', 'format': 'ipvanyaddress'}),
)


Expand Down
84 changes: 84 additions & 0 deletions tests/test_schema.py
Expand Up @@ -26,6 +26,12 @@
DirectoryPath,
EmailStr,
FilePath,
IPv4Address,
IPv4Interface,
IPv4Network,
IPv6Address,
IPv6Interface,
IPv6Network,
IPvAnyAddress,
IPvAnyInterface,
IPvAnyNetwork,
Expand Down Expand Up @@ -665,6 +671,32 @@ class Model(BaseModel):
}


def test_ipv4address_type():
class Model(BaseModel):
ip_address: IPv4Address

model_schema = Model.schema()
assert model_schema == {
'title': 'Model',
'type': 'object',
'properties': {'ip_address': {'title': 'Ip_Address', 'type': 'string', 'format': 'ipv4'}},
'required': ['ip_address'],
}


def test_ipv6address_type():
class Model(BaseModel):
ip_address: IPv6Address

model_schema = Model.schema()
assert model_schema == {
'title': 'Model',
'type': 'object',
'properties': {'ip_address': {'title': 'Ip_Address', 'type': 'string', 'format': 'ipv6'}},
'required': ['ip_address'],
}


def test_ipvanyaddress_type():
class Model(BaseModel):
ip_address: IPvAnyAddress
Expand All @@ -678,6 +710,32 @@ class Model(BaseModel):
}


def test_ipv4interface_type():
class Model(BaseModel):
ip_interface: IPv4Interface

model_schema = Model.schema()
assert model_schema == {
'title': 'Model',
'type': 'object',
'properties': {'ip_interface': {'title': 'Ip_Interface', 'type': 'string', 'format': 'ipv4interface'}},
'required': ['ip_interface'],
}


def test_ipv6interface_type():
class Model(BaseModel):
ip_interface: IPv6Interface

model_schema = Model.schema()
assert model_schema == {
'title': 'Model',
'type': 'object',
'properties': {'ip_interface': {'title': 'Ip_Interface', 'type': 'string', 'format': 'ipv6interface'}},
'required': ['ip_interface'],
}


def test_ipvanyinterface_type():
class Model(BaseModel):
ip_interface: IPvAnyInterface
Expand All @@ -691,6 +749,32 @@ class Model(BaseModel):
}


def test_ipv4network_type():
class Model(BaseModel):
ip_network: IPv4Network

model_schema = Model.schema()
assert model_schema == {
'title': 'Model',
'type': 'object',
'properties': {'ip_network': {'title': 'Ip_Network', 'type': 'string', 'format': 'ipv4network'}},
'required': ['ip_network'],
}


def test_ipv6network_type():
class Model(BaseModel):
ip_network: IPv6Network

model_schema = Model.schema()
assert model_schema == {
'title': 'Model',
'type': 'object',
'properties': {'ip_network': {'title': 'Ip_Network', 'type': 'string', 'format': 'ipv6network'}},
'required': ['ip_network'],
}


def test_ipvanynetwork_type():
class Model(BaseModel):
ip_network: IPvAnyNetwork
Expand Down