From 7f371e13fc0dd1d9f19c1ea8de5f2e94c0038baa Mon Sep 17 00:00:00 2001 From: Joe Gregorio Date: Thu, 12 Jul 2012 16:04:33 -0400 Subject: [PATCH] Add support for additionalProperties when printing schema'd objects. Reviewed in http://codereview.appspot.com/6348104/. --- apiclient/schema.py | 10 +++++++--- tests/data/zoo.json | 20 ++++++++++++++++++++ tests/test_schema.py | 23 ++++++++++++++++++++++- 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/apiclient/schema.py b/apiclient/schema.py index ddcd670..cfed7de 100644 --- a/apiclient/schema.py +++ b/apiclient/schema.py @@ -244,9 +244,13 @@ class _SchemaToStruct(object): if stype == 'object': self.emitEnd('{', schema.get('description', '')) self.indent() - for pname, pschema in schema.get('properties', {}).iteritems(): - self.emitBegin('"%s": ' % pname) - self._to_str_impl(pschema) + 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: diff --git a/tests/data/zoo.json b/tests/data/zoo.json index eec8b36..0df7f7b 100644 --- a/tests/data/zoo.json +++ b/tests/data/zoo.json @@ -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", diff --git a/tests/test_schema.py b/tests/test_schema.py index 2cdb200..1d47790 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -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() -