|
1 |
| -__version__ = "0.0.1" |
| 1 | +from coreapi.codecs.base import BaseCodec |
| 2 | +from coreapi.compat import urlparse |
| 3 | +from coreapi.document import Document, Link, Field |
| 4 | +from coreapi.exceptions import ParseError |
| 5 | +from openapi_codec.utils import _get_string, _get_dict, _get_list, _get_bool, get_strings, get_dicts |
| 6 | +import json |
| 7 | + |
| 8 | + |
| 9 | +__version__ = "0.0.2" |
| 10 | + |
| 11 | + |
| 12 | +def _expand_schema(schema): |
| 13 | + """ |
| 14 | + When an OpenAPI parameter uses `in="body"`, and the schema type is "object", |
| 15 | + then we expand out the parameters of the object into individual fields. |
| 16 | + """ |
| 17 | + schema_type = schema.get('type') |
| 18 | + schema_properties = _get_dict(schema, 'properties') |
| 19 | + schema_required = _get_list(schema, 'required') |
| 20 | + if ((schema_type == ['object']) or (schema_type == 'object')) and schema_properties: |
| 21 | + return [ |
| 22 | + (key, key in schema_required) |
| 23 | + for key in schema_properties.keys() |
| 24 | + ] |
| 25 | + return None |
| 26 | + |
| 27 | + |
| 28 | +def _get_document_base_url(data, base_url=None): |
| 29 | + """ |
| 30 | + Get the base url to use when constructing absolute paths from the |
| 31 | + relative ones provided in the schema defination. |
| 32 | + """ |
| 33 | + prefered_schemes = ['http', 'https'] |
| 34 | + if base_url: |
| 35 | + url_components = urlparse.urlparse(base_url) |
| 36 | + default_host = url_components.netloc |
| 37 | + default_scheme = url_components.scheme |
| 38 | + else: |
| 39 | + default_host = '' |
| 40 | + default_scheme = None |
| 41 | + |
| 42 | + host = _get_string(data, 'host', default=default_host) |
| 43 | + path = _get_string(data, 'basePath', default='/') |
| 44 | + |
| 45 | + if not host: |
| 46 | + # No host is provided, and we do not have an initial URL. |
| 47 | + return path.strip('/') + '/' |
| 48 | + |
| 49 | + schemes = _get_list(data, 'schemes') |
| 50 | + |
| 51 | + if not schemes: |
| 52 | + # No schemes provided, use the initial URL, or a fallback. |
| 53 | + scheme = default_scheme or prefered_schemes[0] |
| 54 | + elif default_scheme in schemes: |
| 55 | + # Schemes provided, the initial URL matches one of them. |
| 56 | + scheme = default_scheme |
| 57 | + else: |
| 58 | + # Schemes provided, the initial URL does not match, pick a fallback. |
| 59 | + for scheme in prefered_schemes: |
| 60 | + if scheme in schemes: |
| 61 | + break |
| 62 | + else: |
| 63 | + raise ParseError('Unsupported transport schemes "%s"' % schemes) |
| 64 | + |
| 65 | + return '%s://%s/%s/' % (scheme, host, path.strip('/')) |
| 66 | + |
| 67 | + |
| 68 | +def _parse_document(data, base_url=None): |
| 69 | + schema_url = base_url |
| 70 | + base_url = _get_document_base_url(data, base_url) |
| 71 | + info = _get_dict(data, 'info') |
| 72 | + title = _get_string(info, 'title') |
| 73 | + paths = _get_dict(data, 'paths') |
| 74 | + content = {} |
| 75 | + for path in paths.keys(): |
| 76 | + url = urlparse.urljoin(base_url, path.lstrip('/')) |
| 77 | + spec = _get_dict(paths, path) |
| 78 | + default_parameters = get_dicts(_get_list(spec, 'parameters')) |
| 79 | + for action in spec.keys(): |
| 80 | + action = action.lower() |
| 81 | + if action not in ('get', 'put', 'post', 'delete', 'options', 'head', 'patch'): |
| 82 | + continue |
| 83 | + operation = _get_dict(spec, action) |
| 84 | + |
| 85 | + # Determine any fields on the link. |
| 86 | + fields = [] |
| 87 | + parameters = get_dicts(_get_list(operation, 'parameters', default_parameters), dereference_using=data) |
| 88 | + for parameter in parameters: |
| 89 | + name = _get_string(parameter, 'name') |
| 90 | + location = _get_string(parameter, 'in') |
| 91 | + required = _get_bool(parameter, 'required', default=(location == 'path')) |
| 92 | + if location == 'body': |
| 93 | + schema = _get_dict(parameter, 'schema', dereference_using=data) |
| 94 | + expanded = _expand_schema(schema) |
| 95 | + if expanded is not None: |
| 96 | + expanded_fields = [ |
| 97 | + Field(name=field_name, location='form', required=is_required) |
| 98 | + for field_name, is_required in expanded |
| 99 | + if not any([field.name == name for field in fields]) |
| 100 | + ] |
| 101 | + fields += expanded_fields |
| 102 | + else: |
| 103 | + field = Field(name=name, location='body', required=True) |
| 104 | + fields.append(field) |
| 105 | + else: |
| 106 | + field = Field(name=name, location=location, required=required) |
| 107 | + fields.append(field) |
| 108 | + link = Link(url=url, action=action, fields=fields) |
| 109 | + |
| 110 | + # Add the link to the document content. |
| 111 | + tags = get_strings(_get_list(operation, 'tags')) |
| 112 | + operation_id = _get_string(operation, 'operationId') |
| 113 | + if tags: |
| 114 | + for tag in tags: |
| 115 | + if tag not in content: |
| 116 | + content[tag] = {} |
| 117 | + content[tag][operation_id] = link |
| 118 | + else: |
| 119 | + content[operation_id] = link |
| 120 | + |
| 121 | + return Document(url=schema_url, title=title, content=content) |
| 122 | + |
| 123 | + |
| 124 | +class OpenAPICodec(BaseCodec): |
| 125 | + media_type = "application/json" |
| 126 | + |
| 127 | + def load(self, bytes, base_url=None): |
| 128 | + """ |
| 129 | + Takes a bytestring and returns a document. |
| 130 | + """ |
| 131 | + try: |
| 132 | + data = json.loads(bytes.decode('utf-8')) |
| 133 | + except ValueError as exc: |
| 134 | + raise ParseError('Malformed JSON. %s' % exc) |
| 135 | + |
| 136 | + doc = _parse_document(data, base_url) |
| 137 | + if not isinstance(doc, Document): |
| 138 | + raise ParseError('Top level node must be a document.') |
| 139 | + |
| 140 | + return doc |
0 commit comments