Handle recursive hashes (#621)

This fixes the generator for cases where one of the entries of the
"schemas" object returned by the Discovery service is a recursively
defined map.

Fixes #608
This commit is contained in:
bantini 2017-10-12 10:57:57 -07:00 committed by Sai Cheemalapati
parent f4e89a556a
commit 6e2276cb80
3 changed files with 56 additions and 4 deletions

View File

@ -55,8 +55,14 @@ module Google
return 'Fixnum' if format == 'uint64' return 'Fixnum' if format == 'uint64'
return TYPE_MAP[type] return TYPE_MAP[type]
when 'array' when 'array'
if items == self
return sprintf('Array<%s>', qualified_name)
end
return sprintf('Array<%s>', items.generated_type) return sprintf('Array<%s>', items.generated_type)
when 'hash' when 'hash'
if additional_properties == self
return sprintf('Hash<String,%s>', qualified_name)
end
return sprintf('Hash<String,%s>', additional_properties.generated_type) return sprintf('Hash<String,%s>', additional_properties.generated_type)
when 'object' when 'object'
return qualified_name return qualified_name
@ -81,16 +87,16 @@ module Google
return [] if parameters.nil? return [] if parameters.nil?
parameters.values.select { |param| param.location == 'query' } parameters.values.select { |param| param.location == 'query' }
end end
def required_parameters def required_parameters
return [] if parameter_order.nil? || parameters.nil? return [] if parameter_order.nil? || parameters.nil?
parameter_order.map { |name| parameters[name] }.select { |param| param.location == 'path' || param.required } parameter_order.map { |name| parameters[name] }.select { |param| param.location == 'path' || param.required }
end end
def optional_query_parameters def optional_query_parameters
query_parameters.select { |param| param.required != true } query_parameters.select { |param| param.required != true }
end end
end end
class RestResource class RestResource

View File

@ -1,4 +1,4 @@
<% if cls.type == 'object' -%> <% if cls.type == 'object' || (cls.type == 'hash' && cls.properties.length > 0) -%>
# <%= block_comment(cls.description, 0, 1) %> # <%= block_comment(cls.description, 0, 1) %>
class <%= cls.generated_class_name %><% if cls.base_ref %> < <%= cls.base_ref.generated_type %><% end %> class <%= cls.generated_class_name %><% if cls.base_ref %> < <%= cls.base_ref.generated_type %><% end %>

View File

@ -90,6 +90,9 @@
"hat": { "hat": {
"$ref": "Hat" "$ref": "Hat"
}, },
"recursiveHat": {
"$ref": "RecursiveHat"
},
"properties": { "properties": {
"$ref": "HashLikeThing" "$ref": "HashLikeThing"
}, },
@ -131,6 +134,49 @@
] ]
} }
}, },
"RecursiveHat": {
"id": "RecursiveHat",
"type": "object",
"properties": {
"bool": {
"type": "boolean",
"description": "The bool value."
},
"double": {
"type": "number",
"description": "The double value.",
"format": "double"
},
"image": {
"$ref": "Image",
"description": "The image value."
},
"int": {
"type": "integer",
"description": "The int value.",
"format": "int32"
},
"kind": {
"type": "string",
"description": "The kind, fixed to \"test#recursiveHat\".",
"default": "test#recursiveHat"
},
"list": {
"type": "array",
"description": "The list of typed values.",
"items": {
"$ref": "RecursiveHat"
}
},
"string": {
"type": "string",
"description": "The string value."
}
},
"additionalProperties": {
"$ref": "RecursiveHat"
}
},
"TopHat": { "TopHat": {
"type": "object", "type": "object",
"properties": { "properties": {