Skip to content

Commit

Permalink
add duration field #78
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Jun 6, 2020
1 parent bed451c commit 916a072
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions drf_spectacular/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,9 @@ def _map_serializer_field(self, field, direction):
if isinstance(field, serializers.UUIDField):
return append_meta(build_basic_type(OpenApiTypes.UUID), meta)

if isinstance(field, serializers.DurationField):
return append_meta(build_basic_type(OpenApiTypes.STR), meta)

if isinstance(field, serializers.IPAddressField):
# TODO this might be a DRF bug. protocol is not propagated to serializer although it
# should have been. results in always 'both' (thus no format)
Expand Down
4 changes: 3 additions & 1 deletion tests/test_fields.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import uuid
from datetime import datetime, date
from datetime import datetime, date, timedelta
from decimal import Decimal

import pytest
Expand Down Expand Up @@ -44,6 +44,7 @@ class AllFields(models.Model):
field_possmallint = models.PositiveSmallIntegerField()
field_nullbool = models.NullBooleanField()
field_time = models.TimeField()
field_duration = models.DurationField()
# field_image = models.ImageField()
# field_filepath = models.FilePathField()

Expand Down Expand Up @@ -191,6 +192,7 @@ def test_model_setup_is_valid():
field_possmallint=1,
field_nullbool=None,
field_time='00:05:23.283',
field_duration=timedelta(seconds=10),
# relations
field_foreign=aux,
field_o2o=aux,
Expand Down
3 changes: 3 additions & 0 deletions tests/test_fields.yml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ components:
field_time:
type: string
format: time
field_duration:
type: string
field_foreign:
type: string
format: uuid
Expand All @@ -251,6 +253,7 @@ components:
- field_datetime
- field_decimal
- field_decimal_uncoerced
- field_duration
- field_email
- field_file
- field_float
Expand Down

0 comments on commit 916a072

Please sign in to comment.