@@ -5,6 +5,7 @@ import json
|
||||
from collections import OrderedDict
|
||||
|
||||
from coreapi.compat import force_bytes
|
||||
from django.utils.safestring import SafeData, SafeText
|
||||
from ruamel import yaml
|
||||
|
||||
from . import openapi
|
||||
|
||||
@@ -5,7 +5,6 @@ from collections import OrderedDict, defaultdict
|
||||
|
||||
import uritemplate
|
||||
from coreapi.compat import urlparse
|
||||
from django.utils.encoding import force_text
|
||||
from rest_framework import versioning
|
||||
from rest_framework.compat import URLPattern, URLResolver, get_original_route
|
||||
from rest_framework.schemas.generators import EndpointEnumerator as _EndpointEnumerator
|
||||
@@ -18,7 +17,7 @@ from .app_settings import swagger_settings
|
||||
from .errors import SwaggerGenerationError
|
||||
from .inspectors.field import get_basic_type_info, get_queryset_field
|
||||
from .openapi import ReferenceResolver
|
||||
from .utils import get_consumes, get_produces
|
||||
from .utils import force_real_str, get_consumes, get_produces
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -435,7 +434,7 @@ class OpenAPISchemaGenerator(object):
|
||||
attrs['pattern'] = getattr(view_cls, 'lookup_value_regex', attrs.get('pattern', None))
|
||||
|
||||
if model_field and getattr(model_field, 'help_text', False):
|
||||
description = force_text(model_field.help_text)
|
||||
description = model_field.help_text
|
||||
elif model_field and getattr(model_field, 'primary_key', False):
|
||||
description = get_pk_description(model, model_field)
|
||||
else:
|
||||
@@ -443,7 +442,7 @@ class OpenAPISchemaGenerator(object):
|
||||
|
||||
field = openapi.Parameter(
|
||||
name=variable,
|
||||
description=description,
|
||||
description=force_real_str(description),
|
||||
required=True,
|
||||
in_=openapi.IN_PATH,
|
||||
**attrs
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import inspect
|
||||
import logging
|
||||
|
||||
from django.utils.encoding import force_text
|
||||
from rest_framework import serializers
|
||||
from rest_framework.utils import encoders, json
|
||||
|
||||
from .. import openapi
|
||||
from ..utils import decimal_as_float, is_list_view
|
||||
from ..utils import decimal_as_float, force_real_str, is_list_view
|
||||
|
||||
#: Sentinel value that inspectors must return to signal that they do not know how to handle an object
|
||||
NotHandled = object()
|
||||
@@ -196,9 +195,9 @@ class FieldInspector(BaseInspector):
|
||||
"""
|
||||
assert swagger_object_type in (openapi.Schema, openapi.Parameter, openapi.Items)
|
||||
assert not isinstance(field, openapi.SwaggerDict), "passed field is already a SwaggerDict object"
|
||||
title = force_text(field.label) if field.label else None
|
||||
title = force_real_str(field.label) if field.label else None
|
||||
title = title if swagger_object_type == openapi.Schema else None # only Schema has title
|
||||
description = force_text(field.help_text) if field.help_text else None
|
||||
description = force_real_str(field.help_text) if field.help_text else None
|
||||
description = description if swagger_object_type != openapi.Items else None # Items has no description either
|
||||
|
||||
def SwaggerType(existing_object=None, **instance_kwargs):
|
||||
|
||||
@@ -3,6 +3,8 @@ from collections import OrderedDict
|
||||
import coreschema
|
||||
from rest_framework.pagination import CursorPagination, LimitOffsetPagination, PageNumberPagination
|
||||
|
||||
from drf_yasg.utils import force_real_str
|
||||
|
||||
from .. import openapi
|
||||
from .base import FilterInspector, PaginatorInspector
|
||||
|
||||
@@ -48,7 +50,7 @@ class CoreAPICompatInspector(PaginatorInspector, FilterInspector):
|
||||
in_=location_to_in[field.location],
|
||||
type=coreapi_types.get(type(field.schema), openapi.TYPE_STRING),
|
||||
required=field.required,
|
||||
description=field.schema.description if field.schema else None,
|
||||
description=force_real_str(field.schema.description) if field.schema else None,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ from rest_framework.status import is_success
|
||||
from .. import openapi
|
||||
from ..errors import SwaggerGenerationError
|
||||
from ..utils import (
|
||||
force_serializer_instance, get_consumes, get_produces, guess_response_status, is_list_view, no_body,
|
||||
force_real_str, force_serializer_instance, get_consumes, get_produces, guess_response_status, is_list_view, no_body,
|
||||
param_list_to_odict
|
||||
)
|
||||
from .base import ViewInspector
|
||||
@@ -42,7 +42,7 @@ class SwaggerAutoSchema(ViewInspector):
|
||||
|
||||
return openapi.Operation(
|
||||
operation_id=operation_id,
|
||||
description=description,
|
||||
description=force_real_str(description),
|
||||
responses=responses,
|
||||
parameters=parameters,
|
||||
consumes=consumes,
|
||||
@@ -246,7 +246,7 @@ class SwaggerAutoSchema(ViewInspector):
|
||||
for sc, serializer in response_serializers.items():
|
||||
if isinstance(serializer, str):
|
||||
response = openapi.Response(
|
||||
description=serializer
|
||||
description=force_real_str(serializer)
|
||||
)
|
||||
elif not serializer:
|
||||
continue
|
||||
|
||||
@@ -3,6 +3,7 @@ import logging
|
||||
from collections import OrderedDict
|
||||
|
||||
from django.db import models
|
||||
from django.utils.encoding import force_text
|
||||
from rest_framework import serializers, status
|
||||
from rest_framework.mixins import DestroyModelMixin, RetrieveModelMixin, UpdateModelMixin
|
||||
from rest_framework.request import is_form_media_type
|
||||
@@ -319,3 +320,17 @@ def get_serializer_ref_name(serializer):
|
||||
if ref_name.endswith('Serializer'):
|
||||
ref_name = ref_name[:-len('Serializer')]
|
||||
return ref_name
|
||||
|
||||
|
||||
def force_real_str(s, encoding='utf-8', strings_only=False, errors='strict'):
|
||||
"""
|
||||
Force `s` into a ``str`` instance.
|
||||
|
||||
Fix for https://github.com/axnsan12/drf-yasg/issues/159
|
||||
"""
|
||||
if s is not None:
|
||||
s = force_text(s, encoding, strings_only, errors)
|
||||
if type(s) != str:
|
||||
s = '' + s
|
||||
|
||||
return s
|
||||
|
||||
Reference in New Issue
Block a user