Skip to content

Commit a0a560d

Browse files
authored
Merge pull request #90 from cwacek/feature/explain-multiple-objects
feature: Explain how to build multiple top level objects.
2 parents 7da07c4 + 99880c6 commit a0a560d

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

python_jsonschema_objects/examples/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,60 @@ schemas are unique.
174174
}
175175
```
176176

177+
## Generating Multiple Top Level Objects
178+
179+
Sometimes what you really want to do is define a couple
180+
of different objects in a schema, and then be able to use
181+
them flexibly.
182+
183+
Any object built as a reference can be obtained from the top
184+
level namespace. Thus, to obtain multiple top level classes,
185+
define them separately in a definitions structure, then simply
186+
make the top level schema refer to each of them as a `oneOf`.
187+
188+
The schema and code example below show how this works.
189+
190+
``` schema
191+
{
192+
"title": "MultipleObjects",
193+
"id": "foo",
194+
"type": "object",
195+
"oneOf":[
196+
{"$ref": "#/definitions/ErrorResponse"},
197+
{"$ref": "#/definitions/VersionGetResponse"}
198+
],
199+
"definitions": {
200+
"ErrorResponse": {
201+
"title": "Error Response",
202+
"id": "Error Response",
203+
"type": "object",
204+
"properties": {
205+
"message": {"type": "string"},
206+
"status": {"type": "integer"}
207+
},
208+
"required": ["message", "status"]
209+
},
210+
"VersionGetResponse": {
211+
"title": "Version Get Response",
212+
"type": "object",
213+
"properties": {
214+
"local": {"type": "boolean"},
215+
"version": {"type": "string"}
216+
},
217+
"required": ["version"]
218+
}
219+
}
220+
}
221+
```
222+
223+
``` python
224+
>>> builder = pjs.ObjectBuilder('multiple_objects.json')
225+
>>> classes = builder.build_classes()
226+
>>> print(dir(classes))
227+
[u'ErrorResponse', 'Local', 'Message', u'Multipleobjects',
228+
'Status', 'Version', u'VersionGetResponse']
229+
```
230+
177231
## Installation
178232

179233
pip install python_jsonschema_objects

test/test_pytest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,8 @@ def test_build_classes_is_idempotent():
6464
}
6565
}
6666
builder = pjs.ObjectBuilder(schema)
67+
x = builder.build_classes()
6768
builder.build_classes()
68-
builder.build_classes()
69-
7069

7170

7271
def test_underscore_properties():

test/test_regression_87.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import pytest
2+
3+
import python_jsonschema_objects as pjs
4+
5+
6+
def test_multiple_objects_are_defined(markdown_examples):
7+
builder = pjs.ObjectBuilder(
8+
markdown_examples['MultipleObjects'],
9+
resolved=markdown_examples)
10+
11+
assert builder
12+
classes = builder.build_classes()
13+
assert 'ErrorResponse' in classes
14+
assert 'VersionGetResponse' in classes
15+
print(dir(classes))

0 commit comments

Comments
 (0)