-
Notifications
You must be signed in to change notification settings - Fork 118
Add support for parsing YAML specs #198
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| version = '8.0.1' | ||
| version = '8.1.0' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,10 @@ | |
| import contextlib | ||
| import logging | ||
| import os | ||
| import os.path | ||
| import yaml | ||
|
|
||
| from bravado_core.spec import is_yaml | ||
| from six.moves import urllib | ||
| from six.moves.urllib import parse as urlparse | ||
|
|
||
|
|
@@ -24,22 +27,25 @@ class FileEventual(object): | |
| class FileResponse(object): | ||
|
|
||
| def __init__(self, data): | ||
| self.data = data | ||
| self.text = data | ||
| self.headers = {} | ||
|
|
||
| def json(self): | ||
| return self.data | ||
| return json.loads(self.text) | ||
|
|
||
| def __init__(self, path): | ||
| self.path = path | ||
| self.is_yaml = is_yaml(path) | ||
|
|
||
| def get_path(self): | ||
| if not self.path.endswith('.json'): | ||
| if not self.path.endswith('.json') and not self.is_yaml: | ||
| return self.path + '.json' | ||
| return self.path | ||
|
|
||
| def wait(self, timeout=None): | ||
| with contextlib.closing(urllib.request.urlopen(self.get_path())) as fp: | ||
| return self.FileResponse(json.load(fp)) | ||
| content = fp.read() | ||
| return self.FileResponse(content) | ||
|
|
||
| def result(self, *args, **kwargs): | ||
| return self.wait(*args, **kwargs) | ||
|
|
@@ -86,12 +92,40 @@ def load_spec(self, spec_url, base_url=None): | |
| :param base_url: TODO: need this? | ||
| :returns: json spec in dict form | ||
| """ | ||
| spec_json = request( | ||
| response = request( | ||
| self.http_client, | ||
| spec_url, | ||
| self.request_headers, | ||
| ).result().json() | ||
| return spec_json | ||
| ).result() | ||
|
|
||
| content_type = response.headers.get('content-type', '').lower() | ||
| if is_yaml(spec_url, content_type): | ||
| return self.load_yaml(response.text) | ||
| else: | ||
| return response.json() | ||
|
|
||
| def load_yaml(self, text): | ||
| """Load a YAML Swagger spec from the given string, transforming | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This suggests to me that your YAML spec is incorrect, we shouldn't need to cast these to string. Need to wrap in quotes.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh, gotcha, sounds like specs out there aren't well formed in general and yaml might be inconsistent. Cool |
||
| integer response status codes to strings. This is to keep | ||
| compatibility with the existing YAML spec examples in | ||
| https://github.com/OAI/OpenAPI-Specification/tree/master/examples/v2.0/yaml | ||
| :param text: String from which to parse the YAML. | ||
| :type text: basestring | ||
| :return: Python dictionary representing the spec. | ||
| :raise: yaml.parser.ParserError: If the text is not valid YAML. | ||
| """ | ||
| data = yaml.load(text) | ||
| for path, methods in iter(data.get('paths', {}).items()): | ||
| for method, operation in iter(methods.items()): | ||
| if 'responses' in operation: | ||
| operation['responses'] = dict( | ||
| (str(code), response) | ||
| for code, response in iter( | ||
| operation['responses'].items() | ||
| ) | ||
| ) | ||
|
|
||
| return data | ||
|
|
||
|
|
||
| # TODO: Adding the file scheme here just adds complexity to request() | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we just load them all as if they're yaml? Json is just a yaml subset
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes but YAML is not a JSON subset, we'd be potentially parsing/ingesting files that are not valid JSON.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm I guess that's fair.