Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Migrate to PyYAML for yaml generator. #845

Merged
merged 2 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@
('py:class', 'Exception'),
('py:class', 'collections.OrderedDict'),

('py:class', 'ruamel.yaml.dumper.SafeDumper'),
('py:class', 'yaml.CSafeDumper'),
('py:class', 'rest_framework.serializers.Serializer'),
('py:class', 'rest_framework.renderers.BaseRenderer'),
('py:class', 'rest_framework.parsers.BaseParser'),
Expand Down
3,420 changes: 587 additions & 2,833 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
},
"private": true,
"devDependencies": {
"cspell": "^6.2.3"
"cspell": "^6.30.0"
}
}
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ coreapi>=2.3.3
coreschema>=0.0.4
djangorestframework>=3.10.3
django>=2.2.16
ruamel.yaml>=0.16.13
pyyaml>=5.1
inflection>=0.3.1
packaging>=21.0
pytz>=2021.1
Expand Down
20 changes: 8 additions & 12 deletions src/drf_yasg/codecs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from collections import OrderedDict

from django.utils.encoding import force_bytes
from ruamel import yaml
import yaml

from . import openapi
from .errors import SwaggerValidationError
Expand Down Expand Up @@ -126,22 +126,17 @@ def _dump_dict(self, spec):


YAML_MAP_TAG = u'tag:yaml.org,2002:map'
YamlDumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
YamlLoader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)


class SaneYamlDumper(yaml.SafeDumper):
class SaneYamlDumper(YamlDumper):
"""YamlDumper class usable for dumping ``OrderedDict`` and list instances in a standard way."""

def ignore_aliases(self, data):
"""Disable YAML references."""
return True

def increase_indent(self, flow=False, indentless=False, **kwargs):
"""https://stackoverflow.com/a/39681672

Indent list elements.
"""
return super(SaneYamlDumper, self).increase_indent(flow=flow, indentless=False, **kwargs)

def represent_odict(self, mapping, flow_style=None): # pragma: no cover
"""https://gist.github.com/miracle2k/3184458

Expand Down Expand Up @@ -204,11 +199,12 @@ def yaml_sane_dump(data, binary):
Dumper=SaneYamlDumper,
default_flow_style=False,
encoding='utf-8' if binary else None,
allow_unicode=binary
allow_unicode=binary,
sort_keys=False,
)


class SaneYamlLoader(yaml.SafeLoader):
class SaneYamlLoader(YamlLoader):
def construct_odict(self, node, deep=False):
self.flatten_mapping(node)
return OrderedDict(self.construct_pairs(node))
Expand All @@ -223,7 +219,7 @@ def yaml_sane_load(stream):
:param stream: YAML stream (can be a string or a file-like object)
:rtype: OrderedDict
"""
return yaml.load(stream, Loader=SaneYamlLoader)
return yaml.load(stream, Loader=YamlLoader)


class OpenAPICodecYaml(_OpenAPICodec):
Expand Down
2 changes: 1 addition & 1 deletion src/drf_yasg/static/drf-yasg/redoc-old/redoc.min.js

Large diffs are not rendered by default.

1,805 changes: 1,804 additions & 1 deletion src/drf_yasg/static/drf-yasg/redoc/redoc.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/drf_yasg/static/drf-yasg/redoc/redoc.standalone.js.map

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions src/drf_yasg/static/drf-yasg/swagger-ui-dist/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
html {
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
}

*,
*:before,
*:after {
box-sizing: inherit;
}

body {
margin: 0;
background: #fafafa;
}
16 changes: 10 additions & 6 deletions src/drf_yasg/static/drf-yasg/swagger-ui-dist/oauth2-redirect.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
var isValid, qp, arr;

if (/code|token|error/.test(window.location.hash)) {
qp = window.location.hash.substring(1);
qp = window.location.hash.substring(1).replace('?', '&');
} else {
qp = location.search.substring(1);
}
Expand All @@ -38,7 +38,7 @@
authId: oauth2.auth.name,
source: "auth",
level: "warning",
message: "Authorization may be unsafe, passed state was changed in server Passed state wasn't returned from auth server"
message: "Authorization may be unsafe, passed state was changed in server. The passed state wasn't returned from auth server."
});
}

Expand All @@ -58,7 +58,7 @@
authId: oauth2.auth.name,
source: "auth",
level: "error",
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server"
message: oauthErrorMsg || "[Authorization failed]: no accessCode received from the server."
});
}
} else {
Expand All @@ -67,9 +67,13 @@
window.close();
}

window.addEventListener('DOMContentLoaded', function () {
run();
});
if (document.readyState !== 'loading') {
run();
} else {
document.addEventListener('DOMContentLoaded', function () {
run();
});
}
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
window.onload = function() {
//<editor-fold desc="Changeable Configuration Block">

// the following lines will be replaced by docker/configurator, when it runs in a docker-container
window.ui = SwaggerUIBundle({
url: "https://petstore.swagger.io/v2/swagger.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIStandalonePreset
],
plugins: [
SwaggerUIBundle.plugins.DownloadUrl
],
layout: "StandaloneLayout"
});

//</editor-fold>
};

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/drf_yasg/static/drf-yasg/swagger-ui-dist/swagger-ui.css

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.