Add support for additionalProperties when printing schema'd objects.

Reviewed in http://codereview.appspot.com/6348104/.
This commit is contained in:
Joe Gregorio
2012-07-12 16:04:33 -04:00
parent 4a2c29f521
commit 7f371e13fc
3 changed files with 49 additions and 4 deletions

View File

@@ -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:

View File

@@ -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",

View File

@@ -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()