Add support for additionalProperties when printing schema'd objects.
Reviewed in http://codereview.appspot.com/6348104/.
This commit is contained in:
@@ -244,9 +244,13 @@ class _SchemaToStruct(object):
|
|||||||
if stype == 'object':
|
if stype == 'object':
|
||||||
self.emitEnd('{', schema.get('description', ''))
|
self.emitEnd('{', schema.get('description', ''))
|
||||||
self.indent()
|
self.indent()
|
||||||
|
if 'properties' in schema:
|
||||||
for pname, pschema in schema.get('properties', {}).iteritems():
|
for pname, pschema in schema.get('properties', {}).iteritems():
|
||||||
self.emitBegin('"%s": ' % pname)
|
self.emitBegin('"%s": ' % pname)
|
||||||
self._to_str_impl(pschema)
|
self._to_str_impl(pschema)
|
||||||
|
elif 'additionalProperties' in schema:
|
||||||
|
self.emitBegin('"a_key": ')
|
||||||
|
self._to_str_impl(schema['additionalProperties'])
|
||||||
self.undent()
|
self.undent()
|
||||||
self.emit('},')
|
self.emit('},')
|
||||||
elif '$ref' in schema:
|
elif '$ref' in schema:
|
||||||
|
|||||||
@@ -122,6 +122,26 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"AnimalMap": {
|
||||||
|
"id": "AnimalMap",
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"etag": {
|
||||||
|
"type": "string"
|
||||||
|
},
|
||||||
|
"animals": {
|
||||||
|
"type": "object",
|
||||||
|
"description": "Map of animal id to animal data",
|
||||||
|
"additionalProperties": {
|
||||||
|
"$ref": "Animal"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"kind": {
|
||||||
|
"type": "string",
|
||||||
|
"default": "zoo#animalMap"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"LoadFeed": {
|
"LoadFeed": {
|
||||||
"id": "LoadFeed",
|
"id": "LoadFeed",
|
||||||
"type": "object",
|
"type": "object",
|
||||||
|
|||||||
@@ -126,6 +126,28 @@ class SchemasTest(unittest.TestCase):
|
|||||||
|
|
||||||
self.assertEqual(feed, eval(self.sc.prettyPrintByName('AnimalFeed')))
|
self.assertEqual(feed, eval(self.sc.prettyPrintByName('AnimalFeed')))
|
||||||
|
|
||||||
|
def test_additional_properties(self):
|
||||||
|
items = {
|
||||||
|
'animals': {
|
||||||
|
'a_key': {
|
||||||
|
'photo': {
|
||||||
|
'hash': 'A String',
|
||||||
|
'hashAlgorithm': 'A String',
|
||||||
|
'filename': 'A String',
|
||||||
|
'type': 'A String',
|
||||||
|
'size': 42
|
||||||
|
},
|
||||||
|
'kind': 'zoo#animal',
|
||||||
|
'etag': 'A String',
|
||||||
|
'name': 'A String'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
'kind': 'zoo#animalMap',
|
||||||
|
'etag': 'A String'
|
||||||
|
}
|
||||||
|
|
||||||
|
self.assertEqual(items, eval(self.sc.prettyPrintByName('AnimalMap')))
|
||||||
|
|
||||||
def test_unknown_name(self):
|
def test_unknown_name(self):
|
||||||
self.assertRaises(KeyError,
|
self.assertRaises(KeyError,
|
||||||
self.sc.prettyPrintByName, 'UknownSchemaThing')
|
self.sc.prettyPrintByName, 'UknownSchemaThing')
|
||||||
@@ -133,4 +155,3 @@ class SchemasTest(unittest.TestCase):
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user