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 18, 2021
1 parent eb1f380 commit f8f6b3a
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
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:`re.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
30 changes: 30 additions & 0 deletions drf_spectacular/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import date, datetime, time, timedelta
from decimal import Decimal
from ipaddress import IPv4Address, IPv6Address
from re import Pattern
from uuid import UUID

from drf_spectacular.settings import spectacular_settings
Expand Down Expand Up @@ -54,6 +55,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 @@ -62,6 +71,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 @@ -81,6 +92,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:`~re.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 @@ -117,15 +137,24 @@ def build_generic_type():
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.OBJECT: build_generic_type(),
OpenApiTypes.ANY: {},
OpenApiTypes.NONE: None,
Expand All @@ -145,6 +174,7 @@ def build_generic_type():
timedelta: OpenApiTypes.DURATION,
IPv4Address: OpenApiTypes.IP4,
IPv6Address: OpenApiTypes.IP6,
Pattern: OpenApiTypes.REGEX
dict: OpenApiTypes.OBJECT,
typing.Any: OpenApiTypes.ANY,
None: OpenApiTypes.NONE,
Expand Down

0 comments on commit f8f6b3a

Please sign in to comment.