Skip to content

Commit

Permalink
fixed parsing responses bodies, see jdiegodcp#12
Browse files Browse the repository at this point in the history
- The responses bodies map is now accepting more then just
  'application/json' media types. If there's no valid media
  type, the default one is used and the body spec is
  inspected for 'schema' and 'example' parameters
- Added XSDs for thingy and thingy-list
- Added parser tests wrt response bodies parsing
  • Loading branch information
jenner committed May 26, 2015
1 parent eb7cb5a commit f79a469
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 20 deletions.
50 changes: 32 additions & 18 deletions ramlfications/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)
from .raml import RootNode, ResourceNode, ResourceTypeNode, TraitNode
from .utils import load_schema
from .config import MEDIA_TYPES

__all__ = ["parse_raml"]

Expand Down Expand Up @@ -927,24 +928,37 @@ def resp_headers(headers):
def resp_body(body):
"""Set response body."""
body_list = []
if body.get("schema"):
schema = body.get("schema")
example = body.get("example")
elif body.get("application/json"):
schema = body.get("application/json", {}).get("schema")
example = body.get("application/json", {}).get("example")
else:
schema = {}
example = {}
b = Body(
mime_type="application/json",
raw=body,
schema=load_schema(schema),
example=load_schema(example),
form_params=None,
config=root.config
)
body_list.append(b)
default_body = {}
for (key, spec) in body.items():
if key not in MEDIA_TYPES:
# if a root mediaType was defined, the response body
# may omit the mime_type definition
if key == 'schema':
default_body['schema'] = load_schema(spec) if spec else {}
if key == 'example':
default_body['example'] = load_schema(spec) if spec else {}
else:
mime_type = key
schema = spec.get('schema', '')
example = spec.get('example', '')
body_list.append(Body(
mime_type=mime_type,
raw=spec,
schema=load_schema(schema) if schema else {},
example=load_schema(example) if example else {},
form_params=None,
config=root.config
))
if default_body:
body_list.append(Body(
mime_type=root.media_type,
raw=body,
schema=default_body['schema'],
example=default_body['example'],
form_params=None,
config=root.config
))

return body_list or None

resps = get_attribute_levels("responses")
Expand Down
9 changes: 9 additions & 0 deletions tests/data/examples/complete-valid-example.raml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ documentation:
specification, and is meant for testing purposes within this RAML specification.
schemas:
- Thingy: !include includes/post-thingy-schema.json
- ThingyXsd: !include includes/thingy.xsd
- ThingyListXsd: !include includes/thingy-list.xsd
resourceTypes:
- base:
uriParameters:
Expand Down Expand Up @@ -510,6 +512,13 @@ traits:
"$ref": "schemas/thingy.json"
}
}
text/xml:
schema: ThingyListXsd
example: |
<thingies>
<thingy><name>Foo</name></thingy>
<thingy><name>Bar</name></thingy>
</thingies>
post:
description: |
[Create a Thingy](https://developer.example.com/create-thingy/)
Expand Down
10 changes: 10 additions & 0 deletions tests/data/examples/includes/thingy-list.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:include schemaLocation="./thingy.xsd"/>
<xs:element name="thingies">
<xs:complexType>
<xs:sequence minOccurs="0" maxOccurs="unbounded">
<xs:element type="thingyType" name="thingy"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
8 changes: 8 additions & 0 deletions tests/data/examples/includes/thingy.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="thingy" type="thingyType" />
<xs:complexType name="thingyType">
<xs:sequence>
<xs:element type="xs:string" name="name" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
7 changes: 5 additions & 2 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -631,15 +631,18 @@ def test_resource_responses(resources):

assert res.responses[0].code == 200
assert res.responses[0].body[0].mime_type == "application/json"
assert res.responses[0].body[1].mime_type == "text/xml"
assert len(res.responses[0].body) == 2

schema = {
json_schema = {
"$schema": "http://json-schema.org/draft-03/schema",
"type": "array",
"items": {
"$ref": "schemas/thingy.json"
}
}
assert res.responses[0].body[0].schema == schema
assert res.responses[0].body[0].schema == json_schema
assert res.responses[0].body[1].schema == 'ThingyListXsd'

res = resources[10]
headers = [h.name for h in res.responses[0].headers]
Expand Down

0 comments on commit f79a469

Please sign in to comment.