Skip to content

Commit 79c1a49

Browse files
authored
Merge pull request #50 from olivier-heurtier/json-example
Get example from the schema when available
2 parents 3df3e9c + 0c56ef1 commit 79c1a49

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

sphinxcontrib/openapi/openapi30.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ def _example(media_type_objects, method=None, endpoint=None, status=None,
179179
examples = content.get('examples')
180180
example = content.get('example')
181181

182+
# Try to get the example from the schema
183+
if example is None and 'schema' in content:
184+
example = content['schema'].get('example')
185+
182186
if examples is None:
183187
examples = {}
184188
if not example:
@@ -198,6 +202,7 @@ def _example(media_type_objects, method=None, endpoint=None, status=None,
198202
}
199203

200204
for example in examples.values():
205+
# According to OpenAPI v3 specs, string examples should be left unchanged
201206
if not isinstance(example['value'], str):
202207
example['value'] = json.dumps(
203208
example['value'], indent=4, separators=(',', ': '))

tests/test_openapi.py

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,127 @@ def test_callback(self):
15291529
15301530
''').lstrip()
15311531

1532+
def test_string_example(self):
1533+
renderer = renderers.HttpdomainOldRenderer(None, {'examples': True})
1534+
text = '\n'.join(renderer.render_restructuredtext_markup({
1535+
'openapi': '3.0.0',
1536+
'paths': {
1537+
'/resources': {
1538+
'get': {
1539+
'summary': 'Get resources',
1540+
'responses': {
1541+
'200': {
1542+
'description': 'Something',
1543+
'content': {
1544+
'application/json': {
1545+
'schema': {
1546+
'type': 'string',
1547+
'example': '"A sample"',
1548+
}
1549+
}
1550+
}
1551+
},
1552+
},
1553+
},
1554+
},
1555+
},
1556+
}))
1557+
1558+
assert text == textwrap.dedent('''
1559+
.. http:get:: /resources
1560+
:synopsis: Get resources
1561+
1562+
**Get resources**
1563+
1564+
1565+
**Example request:**
1566+
1567+
.. sourcecode:: http
1568+
1569+
GET /resources HTTP/1.1
1570+
Host: example.com
1571+
1572+
:status 200:
1573+
Something
1574+
1575+
**Example response:**
1576+
1577+
.. sourcecode:: http
1578+
1579+
HTTP/1.1 200 OK
1580+
Content-Type: application/json
1581+
1582+
"A sample"
1583+
1584+
''').lstrip()
1585+
1586+
def test_ref_example(self):
1587+
renderer = renderers.HttpdomainOldRenderer(None, {'examples': True})
1588+
text = '\n'.join(renderer.render_restructuredtext_markup({
1589+
'openapi': '3.0.0',
1590+
'paths': {
1591+
'/resources': {
1592+
'get': {
1593+
'summary': 'Get resources',
1594+
'responses': {
1595+
'200': {
1596+
'description': 'Something',
1597+
'content': {
1598+
'application/json': {
1599+
'schema': {
1600+
'$ref':
1601+
'#/components/schemas/Data',
1602+
}
1603+
}
1604+
}
1605+
},
1606+
},
1607+
},
1608+
},
1609+
},
1610+
'components': {
1611+
'schemas': {
1612+
'Data': {
1613+
'type': 'object',
1614+
'additionalProperties': True,
1615+
'example': {
1616+
'prop1': "Sample 1",
1617+
}
1618+
}
1619+
}
1620+
}
1621+
}))
1622+
1623+
assert text == textwrap.dedent('''
1624+
.. http:get:: /resources
1625+
:synopsis: Get resources
1626+
1627+
**Get resources**
1628+
1629+
1630+
**Example request:**
1631+
1632+
.. sourcecode:: http
1633+
1634+
GET /resources HTTP/1.1
1635+
Host: example.com
1636+
1637+
:status 200:
1638+
Something
1639+
1640+
**Example response:**
1641+
1642+
.. sourcecode:: http
1643+
1644+
HTTP/1.1 200 OK
1645+
Content-Type: application/json
1646+
1647+
{
1648+
"prop1": "Sample 1"
1649+
}
1650+
1651+
''').lstrip()
1652+
15321653
def test_method_option(self):
15331654
spec = collections.defaultdict(collections.OrderedDict)
15341655
spec['paths']['/resource_a'] = {

0 commit comments

Comments
 (0)