From a72e5b28991fe96d592baf8e8b4f6a3b9d192b9e Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Wed, 2 Oct 2019 19:06:05 -0400 Subject: [PATCH] Write multi-line strings in block style (#466) Closes #439 --- src/drf_yasg/codecs.py | 11 +++++++++-- testproj/testproj/urls.py | 6 +++--- tests/reference.yaml | 4 ++-- tests/test_schema_generator.py | 17 +++++++++++++++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/drf_yasg/codecs.py b/src/drf_yasg/codecs.py index 03fd1ed2..24ec252e 100644 --- a/src/drf_yasg/codecs.py +++ b/src/drf_yasg/codecs.py @@ -1,5 +1,3 @@ -from six import raise_from - import copy import json import logging @@ -8,6 +6,8 @@ from coreapi.compat import force_bytes from ruamel import yaml +from six import binary_type, raise_from, text_type + from . import openapi from .errors import SwaggerValidationError @@ -176,7 +176,14 @@ def represent_odict(self, mapping, flow_style=None): # pragma: no cover node.flow_style = best_style return node + def represent_text(self, text): + if "\n" in text: + return self.represent_scalar('tag:yaml.org,2002:str', text, style='|') + return self.represent_scalar('tag:yaml.org,2002:str', text) + +SaneYamlDumper.add_representer(binary_type, SaneYamlDumper.represent_text) +SaneYamlDumper.add_representer(text_type, SaneYamlDumper.represent_text) SaneYamlDumper.add_representer(OrderedDict, SaneYamlDumper.represent_odict) SaneYamlDumper.add_multi_representer(OrderedDict, SaneYamlDumper.represent_odict) diff --git a/testproj/testproj/urls.py b/testproj/testproj/urls.py index 09e8211b..c7169064 100644 --- a/testproj/testproj/urls.py +++ b/testproj/testproj/urls.py @@ -13,9 +13,9 @@ default_version='v1', description="""This is a demo project for the [drf-yasg](https://github.com/axnsan12/drf-yasg) Django Rest Framework library. -The `swagger-ui` view can be found [here](/cached/swagger). -The `ReDoc` view can be found [here](/cached/redoc). -The swagger YAML document can be found [here](/cached/swagger.yaml). +The `swagger-ui` view can be found [here](/cached/swagger). +The `ReDoc` view can be found [here](/cached/redoc). +The swagger YAML document can be found [here](/cached/swagger.yaml). You can log in using the pre-existing `admin` user with password `passwordadmin`.""", # noqa terms_of_service="https://www.google.com/policies/terms/", diff --git a/tests/reference.yaml b/tests/reference.yaml index 6af34753..9b9b8a60 100644 --- a/tests/reference.yaml +++ b/tests/reference.yaml @@ -3,8 +3,8 @@ info: title: Snippets API description: "This is a demo project for the [drf-yasg](https://github.com/axnsan12/drf-yasg)\ \ Django Rest Framework library.\n\nThe `swagger-ui` view can be found [here](/cached/swagger).\ - \ \nThe `ReDoc` view can be found [here](/cached/redoc). \nThe swagger YAML\ - \ document can be found [here](/cached/swagger.yaml). \n\nYou can log in using\ + \nThe `ReDoc` view can be found [here](/cached/redoc).\nThe swagger YAML\ + \ document can be found [here](/cached/swagger.yaml).\n\nYou can log in using\ \ the pre-existing `admin` user with password `passwordadmin`." termsOfService: https://www.google.com/policies/terms/ contact: diff --git a/tests/test_schema_generator.py b/tests/test_schema_generator.py index b232db1b..61dd5a59 100644 --- a/tests/test_schema_generator.py +++ b/tests/test_schema_generator.py @@ -334,3 +334,20 @@ def retrieve(self, request, pk=None): swagger = generator.get_schema(None, True) property_schema = swagger["definitions"]["OptionalMethod"]["properties"]["x"] assert property_schema == openapi.Schema(title='X', type=expected_type, readOnly=True) + + +EXPECTED_DESCRIPTION = """\ + description: |- + This is a demo project for the [drf-yasg](https://github.com/axnsan12/drf-yasg) Django Rest Framework library. + + The `swagger-ui` view can be found [here](/cached/swagger). + The `ReDoc` view can be found [here](/cached/redoc). + The swagger YAML document can be found [here](/cached/swagger.yaml). + + You can log in using the pre-existing `admin` user with password `passwordadmin`. +""" + +def test_multiline_strings(call_generate_swagger): + output = call_generate_swagger(format='yaml') + print("|\n|".join(output.splitlines()[:20])) + assert EXPECTED_DESCRIPTION in output