Skip to content

Commit

Permalink
Write multi-line strings in block style (#466)
Browse files Browse the repository at this point in the history
Closes  #439
  • Loading branch information
nedbat authored and axnsan12 committed Oct 2, 2019
1 parent 1331158 commit a72e5b2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
11 changes: 9 additions & 2 deletions src/drf_yasg/codecs.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from six import raise_from

import copy
import json
import logging
Expand All @@ -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

Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions testproj/testproj/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
Expand Down
4 changes: 2 additions & 2 deletions tests/reference.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 17 additions & 0 deletions tests/test_schema_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a72e5b2

Please sign in to comment.