Skip to content

Commit

Permalink
Merge pull request #64 from axnsan12/release/1.4.1
Browse files Browse the repository at this point in the history
Release version 1.4.1
  • Loading branch information
axnsan12 committed Feb 21, 2018
2 parents 71dee6e + 6c497b3 commit 10c7e22
Show file tree
Hide file tree
Showing 16 changed files with 159 additions and 87 deletions.
11 changes: 11 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@
Changelog
#########

*********
**1.4.1**
*********

- **FIXED:** the ``coerce_to_string`` is now respected when setting the type, default value and min/max values of
``DecimalField`` in the OpenAPI schema (:issue:`62`)
- **FIXED:** error responses from web UI views are now rendered with ``TemplateHTMLRenderer`` instead of throwing
confusing errors (:issue:`58`)
- **IMPROVED:** updated ``swagger-ui`` to version 3.10.0
- **IMPROVED:** updated ``ReDoc`` to version 1.21.0

*********
**1.4.0**
*********
Expand Down
46 changes: 23 additions & 23 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,18 @@

nitpick_ignore = [
('py:class', 'object'),
('py:class', 'bool'),
('py:class', 'dict'),
('py:class', 'list'),
('py:class', 'str'),
('py:class', 'int'),
('py:class', 'bytes'),
('py:class', 'tuple'),
('py:class', 'callable'),
('py:class', 'type'),
('py:class', 'OrderedDict'),
('py:class', 'None'),

('py:class', 'Exception'),
('py:class', 'collections.OrderedDict'),

Expand All @@ -174,29 +186,17 @@
('py:class', 'OpenAPICodecJson'),
('py:class', 'OpenAPISchemaGenerator'),

('py:obj', 'bool'),
('py:obj', 'dict'),
('py:obj', 'list'),
('py:obj', 'str'),
('py:obj', 'int'),
('py:obj', 'bytes'),
('py:obj', 'tuple'),
('py:obj', 'callable'),
('py:obj', 'type'),
('py:obj', 'OrderedDict'),
('py:obj', 'None'),

('py:obj', 'coreapi.Field'),
('py:obj', 'BaseFilterBackend'),
('py:obj', 'BasePagination'),
('py:obj', 'Request'),
('py:obj', 'rest_framework.request.Request'),
('py:obj', 'rest_framework.serializers.Field'),
('py:obj', 'serializers.Field'),
('py:obj', 'serializers.BaseSerializer'),
('py:obj', 'Serializer'),
('py:obj', 'BaseSerializer'),
('py:obj', 'APIView'),
('py:class', 'coreapi.Field'),
('py:class', 'BaseFilterBackend'),
('py:class', 'BasePagination'),
('py:class', 'Request'),
('py:class', 'rest_framework.request.Request'),
('py:class', 'rest_framework.serializers.Field'),
('py:class', 'serializers.Field'),
('py:class', 'serializers.BaseSerializer'),
('py:class', 'Serializer'),
('py:class', 'BaseSerializer'),
('py:class', 'APIView'),
]

# even though the package should be already installed, the sphinx build on RTD
Expand Down
87 changes: 45 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "drf-yasg",
"dependencies": {
"redoc": "^1.20.0",
"swagger-ui-dist": "^3.9.3"
"redoc": "^1.21.0",
"swagger-ui-dist": "^3.10.0"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion requirements/docs.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# used by the 'docs' tox env for building the documentation
Sphinx>=1.6.5
Sphinx>=1.7.0
sphinx_rtd_theme>=0.2.4
Pillow>=4.3.0
readme_renderer>=17.2
Expand Down
4 changes: 3 additions & 1 deletion src/drf_yasg/inspectors/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from rest_framework.utils import encoders, json

from .. import openapi
from ..utils import is_list_view
from ..utils import decimal_as_float, is_list_view

#: Sentinel value that inspectors must return to signal that they do not know how to handle an object
NotHandled = object()
Expand Down Expand Up @@ -224,6 +224,8 @@ def SwaggerType(existing_object=None, **instance_kwargs):
# JSON roundtrip ensures that the value is valid JSON;
# for example, sets and tuples get transformed into lists
default = json.loads(json.dumps(default, cls=encoders.JSONEncoder))
if decimal_as_float(field):
default = float(default)
except Exception: # pragma: no cover
logger.warning("'default' on schema for %s will not be set because "
"to_representation raised an exception", field, exc_info=True)
Expand Down
28 changes: 22 additions & 6 deletions src/drf_yasg/inspectors/field.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import operator
from collections import OrderedDict
from decimal import Decimal

from django.core import validators
from django.db import models
Expand All @@ -8,7 +9,7 @@

from .. import openapi
from ..errors import SwaggerGenerationError
from ..utils import filter_none
from ..utils import decimal_as_float, filter_none
from .base import FieldInspector, NotHandled, SerializerInspector


Expand Down Expand Up @@ -258,26 +259,37 @@ def find_limits(field):
if isinstance(field, field_class)
]

if isinstance(field, serializers.DecimalField) and not decimal_as_float(field):
return limits

for validator in field.validators:
if not hasattr(validator, 'limit_value'):
continue

limit_value = validator.limit_value
if isinstance(limit_value, Decimal) and decimal_as_float(field):
limit_value = float(limit_value)

for validator_class, attr, improves in applicable_limits:
if isinstance(validator, validator_class):
if attr not in limits or improves(validator.limit_value, limits[attr]):
limits[attr] = validator.limit_value
if attr not in limits or improves(limit_value, limits[attr]):
limits[attr] = limit_value

return OrderedDict(sorted(limits.items()))


def decimal_field_type(field):
return openapi.TYPE_NUMBER if decimal_as_float(field) else openapi.TYPE_STRING


model_field_to_basic_type = [
(models.AutoField, (openapi.TYPE_INTEGER, None)),
(models.BinaryField, (openapi.TYPE_STRING, openapi.FORMAT_BINARY)),
(models.BooleanField, (openapi.TYPE_BOOLEAN, None)),
(models.NullBooleanField, (openapi.TYPE_BOOLEAN, None)),
(models.DateTimeField, (openapi.TYPE_STRING, openapi.FORMAT_DATETIME)),
(models.DateField, (openapi.TYPE_STRING, openapi.FORMAT_DATE)),
(models.DecimalField, (openapi.TYPE_NUMBER, None)),
(models.DecimalField, (decimal_field_type, openapi.FORMAT_DECIMAL)),
(models.DurationField, (openapi.TYPE_INTEGER, None)),
(models.FloatField, (openapi.TYPE_NUMBER, None)),
(models.IntegerField, (openapi.TYPE_INTEGER, None)),
Expand All @@ -300,9 +312,11 @@ def find_limits(field):
(serializers.UUIDField, (openapi.TYPE_STRING, openapi.FORMAT_UUID)),
(serializers.RegexField, (openapi.TYPE_STRING, None)),
(serializers.CharField, (openapi.TYPE_STRING, None)),
((serializers.BooleanField, serializers.NullBooleanField), (openapi.TYPE_BOOLEAN, None)),
(serializers.BooleanField, (openapi.TYPE_BOOLEAN, None)),
(serializers.NullBooleanField, (openapi.TYPE_BOOLEAN, None)),
(serializers.IntegerField, (openapi.TYPE_INTEGER, None)),
((serializers.FloatField, serializers.DecimalField), (openapi.TYPE_NUMBER, None)),
(serializers.FloatField, (openapi.TYPE_NUMBER, None)),
(serializers.DecimalField, (decimal_field_type, openapi.FORMAT_DECIMAL)),
(serializers.DurationField, (openapi.TYPE_NUMBER, None)), # ?
(serializers.DateField, (openapi.TYPE_STRING, openapi.FORMAT_DATE)),
(serializers.DateTimeField, (openapi.TYPE_STRING, openapi.FORMAT_DATETIME)),
Expand All @@ -326,6 +340,8 @@ def get_basic_type_info(field):
for field_class, type_format in basic_type_info:
if isinstance(field, field_class):
swagger_type, format = type_format
if callable(swagger_type):
swagger_type = swagger_type(field)
if callable(format):
format = format(field)
break
Expand Down
1 change: 1 addition & 0 deletions src/drf_yasg/openapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
# pulled out of my ass
FORMAT_UUID = "uuid" #:
FORMAT_SLUG = "slug" #:
FORMAT_DECIMAL = "decimal"

IN_BODY = 'body' #:
IN_PATH = 'path' #:
Expand Down

0 comments on commit 10c7e22

Please sign in to comment.