Skip to content

Commit 42f3975

Browse files
committed
Ensure definitions with both and description (or other property) output is valid (using )
1 parent e088161 commit 42f3975

File tree

3 files changed

+59
-7
lines changed

3 files changed

+59
-7
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Current
77
-------
88

99
- Fix missing changelog inprevious release
10+
- Ensure definitions with both `$ref` and description (or other property) output is valid (using `allOf`)
1011

1112
0.12.0 (2018-09-27)
1213
-------------------

flask_restplus/fields.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,13 @@ def schema(self):
227227
if self.as_list:
228228
schema['type'] = 'array'
229229
schema['items'] = {'$ref': ref}
230+
elif any(schema.values()):
231+
# There is already some properties in the schema
232+
allOf = schema.get('allOf', [])
233+
allOf.append({'$ref': ref})
234+
schema['allOf'] = allOf
230235
else:
231236
schema['$ref'] = ref
232-
# if not self.allow_null and not self.readonly:
233-
# schema['required'] = True
234-
235237
return schema
236238

237239
def clone(self, mask=None):

tests/test_swagger.py

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1517,9 +1517,6 @@ def test_model_as_nested_dict(self, api, client):
15171517
})
15181518

15191519
fields = api.model('Person', {
1520-
'name': restplus.fields.String,
1521-
'age': restplus.fields.Integer,
1522-
'birthdate': restplus.fields.DateTime,
15231520
'address': restplus.fields.Nested(address_fields)
15241521
})
15251522

@@ -1537,8 +1534,16 @@ def post(self):
15371534

15381535
assert 'definitions' in data
15391536
assert 'Person' in data['definitions']
1537+
assert data['definitions']['Person'] == {
1538+
'properties': {
1539+
'address': {
1540+
'$ref': '#/definitions/Address'
1541+
},
1542+
},
1543+
'type': 'object'
1544+
}
15401545

1541-
assert 'Address' in data['definitions'].keys()
1546+
assert 'Address' in data['definitions']
15421547
assert data['definitions']['Address'] == {
15431548
'properties': {
15441549
'road': {
@@ -1552,6 +1557,50 @@ def post(self):
15521557
assert path['get']['responses']['200']['schema']['$ref'] == '#/definitions/Person'
15531558
assert path['post']['responses']['200']['schema']['$ref'] == '#/definitions/Person'
15541559

1560+
def test_model_as_nested_dict_with_details(self, api, client):
1561+
address_fields = api.model('Address', {
1562+
'road': restplus.fields.String,
1563+
})
1564+
1565+
fields = api.model('Person', {
1566+
'address': restplus.fields.Nested(address_fields, description='description', readonly=True)
1567+
})
1568+
1569+
@api.route('/model-as-dict/')
1570+
class ModelAsDict(restplus.Resource):
1571+
@api.doc(model=fields)
1572+
def get(self):
1573+
return {}
1574+
1575+
@api.doc(model='Person')
1576+
def post(self):
1577+
return {}
1578+
1579+
data = client.get_specs()
1580+
1581+
assert 'definitions' in data
1582+
assert 'Person' in data['definitions']
1583+
assert data['definitions']['Person'] == {
1584+
'properties': {
1585+
'address': {
1586+
'description': 'description',
1587+
'readOnly': True,
1588+
'allOf': [{'$ref': '#/definitions/Address'}]
1589+
},
1590+
},
1591+
'type': 'object'
1592+
}
1593+
1594+
assert 'Address' in data['definitions']
1595+
assert data['definitions']['Address'] == {
1596+
'properties': {
1597+
'road': {
1598+
'type': 'string'
1599+
},
1600+
},
1601+
'type': 'object'
1602+
}
1603+
15551604
def test_model_as_flat_dict_with_marchal_decorator(self, api, client):
15561605
fields = api.model('Person', {
15571606
'name': restplus.fields.String,

0 commit comments

Comments
 (0)