diff --git a/src/drf_yasg/management/commands/generate_swagger.py b/src/drf_yasg/management/commands/generate_swagger.py index 5cc290f1..53263781 100644 --- a/src/drf_yasg/management/commands/generate_swagger.py +++ b/src/drf_yasg/management/commands/generate_swagger.py @@ -126,6 +126,8 @@ def handle(self, output_file, overwrite, format, api_url, mock, user, private, * if output_file == '-': self.write_schema(schema, self.stdout, format) else: + # normally this would be easily done with open(mode='x'/'w'), + # but python 2 is a pain in the ass as usual flags = os.O_CREAT | os.O_WRONLY flags = flags | (os.O_TRUNC if overwrite else os.O_EXCL) with os.fdopen(os.open(output_file, flags), "w") as stream: diff --git a/src/drf_yasg/views.py b/src/drf_yasg/views.py index 7833226e..b7ddffa3 100644 --- a/src/drf_yasg/views.py +++ b/src/drf_yasg/views.py @@ -11,6 +11,7 @@ from rest_framework.views import APIView from drf_yasg.app_settings import swagger_settings + from .generators import OpenAPISchemaGenerator from .renderers import OpenAPIRenderer, ReDocRenderer, SwaggerJSONRenderer, SwaggerUIRenderer, SwaggerYAMLRenderer diff --git a/tests/conftest.py b/tests/conftest.py index 6569fa7b..e41845cc 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -9,8 +9,8 @@ from rest_framework.test import APIRequestFactory from rest_framework.views import APIView -from drf_yasg import openapi, codecs -from drf_yasg.codecs import yaml_sane_load, yaml_sane_dump +from drf_yasg import codecs, openapi +from drf_yasg.codecs import yaml_sane_dump, yaml_sane_load from drf_yasg.generators import OpenAPISchemaGenerator @@ -47,8 +47,8 @@ def swagger(mock_schema_request): @pytest.fixture -def swagger_dict(swagger): - json_bytes = codec_json().encode(swagger) +def swagger_dict(swagger, codec_json): + json_bytes = codec_json.encode(swagger) return json.loads(json_bytes.decode('utf-8'), object_pairs_hook=OrderedDict) diff --git a/tests/test_reference_schema.py b/tests/test_reference_schema.py index d423f4ac..638821b9 100644 --- a/tests/test_reference_schema.py +++ b/tests/test_reference_schema.py @@ -1,4 +1,9 @@ -from drf_yasg.inspectors import FieldInspector, SerializerInspector, PaginatorInspector, FilterInspector +import json +from collections import OrderedDict + +from drf_yasg import openapi +from drf_yasg.generators import OpenAPISchemaGenerator +from drf_yasg.inspectors import FieldInspector, FilterInspector, PaginatorInspector, SerializerInspector def test_reference_schema(swagger_dict, reference_schema, compare_schemas): @@ -21,13 +26,23 @@ class NoOpPaginatorInspector(PaginatorInspector): pass -def test_noop_inspectors(swagger_settings, swagger_dict, reference_schema, compare_schemas): +def test_noop_inspectors(swagger_settings, mock_schema_request, codec_json, reference_schema, compare_schemas): from drf_yasg import app_settings def set_inspectors(inspectors, setting_name): + inspectors = [__name__ + '.' + inspector.__name__ for inspector in inspectors] swagger_settings[setting_name] = inspectors + app_settings.SWAGGER_DEFAULTS[setting_name] set_inspectors([NoOpFieldInspector, NoOpSerializerInspector], 'DEFAULT_FIELD_INSPECTORS') set_inspectors([NoOpFilterInspector], 'DEFAULT_FILTER_INSPECTORS') set_inspectors([NoOpPaginatorInspector], 'DEFAULT_PAGINATOR_INSPECTORS') + + generator = OpenAPISchemaGenerator( + info=openapi.Info(title="Test generator", default_version="v1"), + version="v2", + ) + swagger = generator.get_schema(mock_schema_request, True) + + json_bytes = codec_json.encode(swagger) + swagger_dict = json.loads(json_bytes.decode('utf-8'), object_pairs_hook=OrderedDict) compare_schemas(swagger_dict, reference_schema)