Skip to content

Commit

Permalink
Add missing types defined in specification.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngnpope committed Sep 19, 2021
1 parent f300649 commit e9a57bc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
9 changes: 9 additions & 0 deletions docs/drf_yasg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,17 @@ provides the :py:class:`~drf_spectacular.types.OpenApiTypes` enum:
- :py:attr:`~drf_spectacular.types.OpenApiTypes.ANY` for which you can use :py:class:`typing.Any`.
- :py:attr:`~drf_spectacular.types.OpenApiTypes.DURATION` for which you can use :py:class:`datetime.timedelta`.
- :py:attr:`~drf_spectacular.types.OpenApiTypes.HOSTNAME`
- :py:attr:`~drf_spectacular.types.OpenApiTypes.IDN_EMAIL`
- :py:attr:`~drf_spectacular.types.OpenApiTypes.IDN_HOSTNAME`
- :py:attr:`~drf_spectacular.types.OpenApiTypes.IRI_REF`
- :py:attr:`~drf_spectacular.types.OpenApiTypes.IRI`
- :py:attr:`~drf_spectacular.types.OpenApiTypes.JSON_PTR_REL`
- :py:attr:`~drf_spectacular.types.OpenApiTypes.JSON_PTR`
- :py:attr:`~drf_spectacular.types.OpenApiTypes.NONE` for which you can use :py:data:`None`.
- :py:attr:`~drf_spectacular.types.OpenApiTypes.REGEX` for which you can use :py:class:`typing.Pattern`.
- :py:attr:`~drf_spectacular.types.OpenApiTypes.TIME` for which you can use :py:class:`datetime.time`.
- :py:attr:`~drf_spectacular.types.OpenApiTypes.URI_REF`
- :py:attr:`~drf_spectacular.types.OpenApiTypes.URI_TPL`

Parameter Location
------------------
Expand Down
31 changes: 30 additions & 1 deletion drf_spectacular/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import date, datetime, time, timedelta
from decimal import Decimal
from ipaddress import IPv4Address, IPv6Address
from typing import Any, Type, Union
from typing import Any, Pattern, Type, Union
from uuid import UUID

_KnownPythonTypes = Type[Union[
Expand Down Expand Up @@ -52,6 +52,14 @@ class OpenApiTypes(enum.Enum):
UUID = enum.auto()
#: Converted to ``{"type": "string", "format": "uri"}``.
URI = enum.auto()
#: Converted to ``{"type": "string", "format": "uri-reference"}``.
URI_REF = enum.auto()
#: Converted to ``{"type": "string", "format": "uri-template"}``.
URI_TPL = enum.auto()
#: Converted to ``{"type": "string", "format": "iri"}``.
IRI = enum.auto()
#: Converted to ``{"type": "string", "format": "iri-reference"}``.
IRI_REF = enum.auto()
#: Converted to ``{"type": "string", "format": "ipv4"}``.
#: Equivalent to :py:class:`~ipaddress.IPv4Address`.
IP4 = enum.auto()
Expand All @@ -60,6 +68,8 @@ class OpenApiTypes(enum.Enum):
IP6 = enum.auto()
#: Converted to ``{"type": "string", "format": "hostname"}``.
HOSTNAME = enum.auto()
#: Converted to ``{"type": "string", "format": "idn-hostname"}``.
IDN_HOSTNAME = enum.auto()
#: Converted to ``{"type": "number", "format": "double"}``.
#: The same as :py:attr:`~drf_spectacular.types.OpenApiTypes.DOUBLE`.
#: Equivalent to :py:class:`~decimal.Decimal`.
Expand All @@ -79,6 +89,15 @@ class OpenApiTypes(enum.Enum):
DURATION = enum.auto()
#: Converted to ``{"type": "string", "format": "email"}``.
EMAIL = enum.auto()
#: Converted to ``{"type": "string", "format": "idn-email"}``.
IDN_EMAIL = enum.auto()
#: Converted to ``{"type": "string", "format": "json-pointer"}``.
JSON_PTR = enum.auto()
#: Converted to ``{"type": "string", "format": "relative-json-pointer"}``.
JSON_PTR_REL = enum.auto()
#: Converted to ``{"type": "string", "format": "regex"}``.
#: Equivalent to :py:class:`~typing.Pattern`.
REGEX = enum.auto()
#: Converted to ``{"type": "object", ...}``.
#: Use this for arbitrary free-form objects (usually a :py:class:`dict`).
#: The ``additionalProperties`` item is added depending on the ``GENERIC_ADDITIONAL_PROPERTIES`` setting.
Expand Down Expand Up @@ -106,15 +125,24 @@ class OpenApiTypes(enum.Enum):
OpenApiTypes.INT64: {'type': 'integer', 'format': 'int64'},
OpenApiTypes.UUID: {'type': 'string', 'format': 'uuid'},
OpenApiTypes.URI: {'type': 'string', 'format': 'uri'},
OpenApiTypes.URI_REF: {'type': 'string', 'format': 'uri-reference'},
OpenApiTypes.URI_TPL: {'type': 'string', 'format': 'uri-template'},
OpenApiTypes.IRI: {'type': 'string', 'format': 'iri'},
OpenApiTypes.IRI_REF: {'type': 'string', 'format': 'iri-reference'},
OpenApiTypes.IP4: {'type': 'string', 'format': 'ipv4'},
OpenApiTypes.IP6: {'type': 'string', 'format': 'ipv6'},
OpenApiTypes.HOSTNAME: {'type': 'string', 'format': 'hostname'},
OpenApiTypes.IDN_HOSTNAME: {'type': 'string', 'format': 'idn-hostname'},
OpenApiTypes.DECIMAL: {'type': 'number', 'format': 'double'},
OpenApiTypes.DATETIME: {'type': 'string', 'format': 'date-time'},
OpenApiTypes.DATE: {'type': 'string', 'format': 'date'},
OpenApiTypes.TIME: {'type': 'string', 'format': 'time'},
OpenApiTypes.DURATION: {'type': 'string', 'format': 'duration'}, # ISO 8601
OpenApiTypes.EMAIL: {'type': 'string', 'format': 'email'},
OpenApiTypes.IDN_EMAIL: {'type': 'string', 'format': 'idn-email'},
OpenApiTypes.JSON_PTR: {'type': 'string', 'format': 'json-pointer'},
OpenApiTypes.JSON_PTR_REL: {'type': 'string', 'format': 'relative-json-pointer'},
OpenApiTypes.REGEX: {'type': 'string', 'format': 'regex'},
OpenApiTypes.ANY: {},
OpenApiTypes.NONE: None,
# OpenApiTypes.OBJECT is inserted at runtime due to dependency on settings
Expand All @@ -134,6 +162,7 @@ class OpenApiTypes(enum.Enum):
timedelta: OpenApiTypes.DURATION,
IPv4Address: OpenApiTypes.IP4,
IPv6Address: OpenApiTypes.IP6,
Pattern: OpenApiTypes.REGEX,
dict: OpenApiTypes.OBJECT,
Any: OpenApiTypes.ANY,
None: OpenApiTypes.NONE,
Expand Down

0 comments on commit e9a57bc

Please sign in to comment.