Add support for additionalProperties when printing schema'd objects.
Reviewed in http://codereview.appspot.com/6348104/.
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 @@
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 @@
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 @@
if __name__ == '__main__':
unittest.main()
-