Skip to content

Commit

Permalink
Add type mappings for IP4, IP6, TIME & DURATION.
Browse files Browse the repository at this point in the history
  • Loading branch information
ngnpope committed Sep 18, 2021
1 parent 799c509 commit eb1f380
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
8 changes: 4 additions & 4 deletions docs/drf_yasg.rst
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ provides the :py:class:`~drf_spectacular.types.OpenApiTypes` enum:
- :py:data:`~drf_yasg.openapi.TYPE_STRING` with :py:data:`~drf_yasg.openapi.FORMAT_EMAIL` is called
:py:attr:`~drf_spectacular.types.OpenApiTypes.EMAIL`
- :py:data:`~drf_yasg.openapi.TYPE_STRING` with :py:data:`~drf_yasg.openapi.FORMAT_IPV4` is called
:py:attr:`~drf_spectacular.types.OpenApiTypes.IP4`
:py:attr:`~drf_spectacular.types.OpenApiTypes.IP4`, but you can use :py:class:`ipaddress.IPv4Address`.
- :py:data:`~drf_yasg.openapi.TYPE_STRING` with :py:data:`~drf_yasg.openapi.FORMAT_IPV6` is called
:py:attr:`~drf_spectacular.types.OpenApiTypes.IP6`
:py:attr:`~drf_spectacular.types.OpenApiTypes.IP6`, but you can use :py:class:`ipaddress.IPv6Address`.
- :py:data:`~drf_yasg.openapi.TYPE_STRING` with :py:data:`~drf_yasg.openapi.FORMAT_PASSWORD` is called
:py:attr:`~drf_spectacular.types.OpenApiTypes.PASSWORD`
- :py:data:`~drf_yasg.openapi.TYPE_STRING` with :py:data:`~drf_yasg.openapi.FORMAT_URI` is called
Expand All @@ -120,10 +120,10 @@ provides the :py:class:`~drf_spectacular.types.OpenApiTypes` enum:
- The following additional types are also available:

- :py:attr:`~drf_spectacular.types.OpenApiTypes.ANY` for which you can use :py:class:`typing.Any`.
- :py:attr:`~drf_spectacular.types.OpenApiTypes.DURATION`
- :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.NONE` for which you can use :py:data:`None`.
- :py:attr:`~drf_spectacular.types.OpenApiTypes.TIME`
- :py:attr:`~drf_spectacular.types.OpenApiTypes.TIME` for which you can use :py:class:`datetime.time`.

Parameter Location
------------------
Expand Down
9 changes: 8 additions & 1 deletion drf_spectacular/types.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import enum
import typing
from datetime import date, datetime
from datetime import date, datetime, time, timedelta
from decimal import Decimal
from ipaddress import IPv4Address, IPv6Address
from uuid import UUID

from drf_spectacular.settings import spectacular_settings
Expand Down Expand Up @@ -54,8 +55,10 @@ class OpenApiTypes(enum.Enum):
#: Converted to ``{"type": "string", "format": "uri"}``.
URI = enum.auto()
#: Converted to ``{"type": "string", "format": "ipv4"}``.
#: Equivalent to :py:class:`~ipaddress.IPv4Address`.
IP4 = enum.auto()
#: Converted to ``{"type": "string", "format": "ipv6"}``.
#: Equivalent to :py:class:`~ipaddress.IPv6Address`.
IP6 = enum.auto()
#: Converted to ``{"type": "string", "format": "hostname"}``.
HOSTNAME = enum.auto()
Expand Down Expand Up @@ -138,6 +141,10 @@ def build_generic_type():
Decimal: OpenApiTypes.DECIMAL,
datetime: OpenApiTypes.DATETIME,
date: OpenApiTypes.DATE,
time: OpenApiTypes.TIME,
timedelta: OpenApiTypes.DURATION,
IPv4Address: OpenApiTypes.IP4,
IPv6Address: OpenApiTypes.IP6,
dict: OpenApiTypes.OBJECT,
typing.Any: OpenApiTypes.ANY,
None: OpenApiTypes.NONE,
Expand Down

0 comments on commit eb1f380

Please sign in to comment.