From abe44df1ec2adc4b7e3d5afbc0ef8553d19bf9b6 Mon Sep 17 00:00:00 2001 From: xhh Date: Tue, 3 Nov 2015 16:39:54 +0800 Subject: [PATCH] Ruby: support map/hash in model deserialization with additionalProperties For example, the "scoreMap" and "cateMap" properties below: "definitions": { "User": { "properties": { "scoreMap": { "type": "object", "additionalProperties": { "type": "integer", "format": "int32", } }, "cateMap": { "type": "object", "additionalProperties": { "$ref": "#/definitions/Category" } } } } } --- .../main/resources/ruby/base_object.mustache | 25 ++++++++++++++----- .../ruby/lib/petstore/models/base_object.rb | 25 ++++++++++++++----- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache b/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache index ccf6db4a4fd..ddb68998e9e 100644 --- a/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache +++ b/modules/swagger-codegen/src/main/resources/ruby/base_object.mustache @@ -42,6 +42,17 @@ module {{moduleName}} else false end + when /\AArray<(?.+)>\z/ + inner_type = Regexp.last_match[:inner_type] + value.map { |v| _deserialize(inner_type, v) } + when /\AHash<(?.+), (?.+)>\z/ + k_type = Regexp.last_match[:k_type] + v_type = Regexp.last_match[:v_type] + {}.tap do |hash| + value.each do |k, v| + hash[_deserialize(k_type, k)] = _deserialize(v_type, v) + end + end else # model _model = {{moduleName}}.const_get(type).new _model.build_from_hash(value) @@ -63,11 +74,7 @@ module {{moduleName}} self.class.attribute_map.each_pair do |attr, param| value = self.send(attr) next if value.nil? - if value.is_a?(Array) - hash[param] = value.compact.map{ |v| _to_hash(v) } - else - hash[param] = _to_hash(value) - end + hash[param] = _to_hash(value) end hash end @@ -75,7 +82,13 @@ module {{moduleName}} # Method to output non-array value in the form of hash # For object, use to_hash. Otherwise, just return the value def _to_hash(value) - if value.respond_to? :to_hash + if value.is_a?(Array) + value.compact.map{ |v| _to_hash(v) } + elsif value.is_a?(Hash) + {}.tap do |hash| + value.each { |k, v| hash[k] = _to_hash(v) } + end + elsif value.respond_to? :to_hash value.to_hash else value diff --git a/samples/client/petstore/ruby/lib/petstore/models/base_object.rb b/samples/client/petstore/ruby/lib/petstore/models/base_object.rb index 4a9a781ae64..af0ec63e79f 100644 --- a/samples/client/petstore/ruby/lib/petstore/models/base_object.rb +++ b/samples/client/petstore/ruby/lib/petstore/models/base_object.rb @@ -42,6 +42,17 @@ module Petstore else false end + when /\AArray<(?.+)>\z/ + inner_type = Regexp.last_match[:inner_type] + value.map { |v| _deserialize(inner_type, v) } + when /\AHash<(?.+), (?.+)>\z/ + k_type = Regexp.last_match[:k_type] + v_type = Regexp.last_match[:v_type] + {}.tap do |hash| + value.each do |k, v| + hash[_deserialize(k_type, k)] = _deserialize(v_type, v) + end + end else # model _model = Petstore.const_get(type).new _model.build_from_hash(value) @@ -63,11 +74,7 @@ module Petstore self.class.attribute_map.each_pair do |attr, param| value = self.send(attr) next if value.nil? - if value.is_a?(Array) - hash[param] = value.compact.map{ |v| _to_hash(v) } - else - hash[param] = _to_hash(value) - end + hash[param] = _to_hash(value) end hash end @@ -75,7 +82,13 @@ module Petstore # Method to output non-array value in the form of hash # For object, use to_hash. Otherwise, just return the value def _to_hash(value) - if value.respond_to? :to_hash + if value.is_a?(Array) + value.compact.map{ |v| _to_hash(v) } + elsif value.is_a?(Hash) + {}.tap do |hash| + value.each { |k, v| hash[k] = _to_hash(v) } + end + elsif value.respond_to? :to_hash value.to_hash else value