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':
|
||||
self.emitEnd('{', schema.get('description', ''))
|
||||
self.indent()
|
||||
if 'properties' in schema:
|
||||
for pname, pschema in schema.get('properties', {}).iteritems():
|
||||
self.emitBegin('"%s": ' % pname)
|
||||
self._to_str_impl(pschema)
|
||||
elif 'additionalProperties' in schema:
|
||||
self.emitBegin('"a_key": ')
|
||||
self._to_str_impl(schema['additionalProperties'])
|
||||
self.undent()
|
||||
self.emit('},')
|
||||
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": {
|
||||
"id": "LoadFeed",
|
||||
"type": "object",
|
||||
|
||||
@@ -126,6 +126,28 @@ class SchemasTest(unittest.TestCase):
|
||||
|
||||
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):
|
||||
self.assertRaises(KeyError,
|
||||
self.sc.prettyPrintByName, 'UknownSchemaThing')
|
||||
@@ -133,4 +155,3 @@ class SchemasTest(unittest.TestCase):
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user