|
85 | 85 | private def self.find_and_cast_into_type(klass, data) |
86 | 86 | return if data.nil? |
87 | 87 |
|
88 | | - case klass.to_s |
89 | | - when "Boolean" |
90 | | - return data if data.instance_of?(TrueClass) || data.instance_of?(FalseClass) |
91 | | - when "Float" |
92 | | - return data if data.instance_of?(Float) |
93 | | - when "Integer" |
94 | | - return data if data.instance_of?(Integer) |
95 | | - when "Time" |
96 | | - return Time.parse(data) |
97 | | - when "Date" |
98 | | - return Date.parse(data) |
99 | | - when "String" |
100 | | - return data if data.instance_of?(String) |
101 | | - when "Object" # "type: object" |
102 | | - return data if data.instance_of?(Hash) |
103 | | - when /\AArray<(?<sub_type>.+)>\z/ # "type: array" |
104 | | - if data.instance_of?(Array) |
105 | | - sub_type = Regexp.last_match[:sub_type] |
106 | | - return data.map { |item| find_and_cast_into_type(sub_type, item) } |
107 | | - end |
108 | | - when /\AHash<String, (?<sub_type>.+)>\z/ # "type: object" with "additionalProperties: { ... }" |
109 | | - if data.instance_of?(Hash) && data.keys.all? { |k| k.instance_of?(Symbol) || k.instance_of?(String) } |
110 | | - sub_type = Regexp.last_match[:sub_type] |
111 | | - return data.each_with_object({} of String | Symbol => Bool | Float | Integer | Time | Date | String | Array | Hash) { |(k, v), hsh| hsh[k] = find_and_cast_into_type(sub_type, v) } |
112 | | - end |
113 | | - else # model |
114 | | - const = {{moduleName}}.const_get(klass) |
115 | | - if const |
116 | | - if const.respond_to?(:openapi_one_of) # nested oneOf model |
117 | | - model = const.build(data) |
118 | | - return model if model |
119 | | - else |
120 | | - # raise if data contains keys that are not known to the model |
121 | | - raise unless (data.keys - const.acceptable_attributes).empty? |
122 | | - model = const.build_from_hash(data) |
123 | | - return model if model && model.valid? |
| 88 | + begin |
| 89 | + case klass.to_s |
| 90 | + when "Boolean" |
| 91 | + return data if data.instance_of?(TrueClass) || data.instance_of?(FalseClass) |
| 92 | + when "Float" |
| 93 | + return data if data.instance_of?(Float) |
| 94 | + when "Integer" |
| 95 | + return data if data.instance_of?(Integer) |
| 96 | + when "Time" |
| 97 | + return Time.parse(data) |
| 98 | + when "Date" |
| 99 | + return Date.parse(data) |
| 100 | + when "String" |
| 101 | + return data if data.instance_of?(String) |
| 102 | + when "Object" # "type: object" |
| 103 | + return data if data.instance_of?(Hash) |
| 104 | + when /\AArray<(?<sub_type>.+)>\z/ # "type: array" |
| 105 | + if data.instance_of?(Array) |
| 106 | + sub_type = Regexp.last_match[:sub_type] |
| 107 | + return data.map { |item| find_and_cast_into_type(sub_type, item) } |
| 108 | + end |
| 109 | + when /\AHash<String, (?<sub_type>.+)>\z/ # "type: object" with "additionalProperties: { ... }" |
| 110 | + if data.instance_of?(Hash) && data.keys.all? { |k| k.instance_of?(Symbol) || k.instance_of?(String) } |
| 111 | + sub_type = Regexp.last_match[:sub_type] |
| 112 | + return data.each_with_object({} of String | Symbol => Bool | Float | Integer | Time | Date | String | Array | Hash) { |(k, v), hsh| hsh[k] = find_and_cast_into_type(sub_type, v) } |
| 113 | + end |
| 114 | + else # model |
| 115 | + const = {{moduleName}}.const_get(klass) |
| 116 | + if const |
| 117 | + if const.respond_to?(:openapi_one_of) # nested oneOf model |
| 118 | + model = const.build(data) |
| 119 | + return model if model |
| 120 | + else |
| 121 | + # raise if data contains keys that are not known to the model |
| 122 | + raise unless (data.keys - const.acceptable_attributes).empty? |
| 123 | + model = const.build_from_hash(data) |
| 124 | + return model if model && model.valid? |
| 125 | + end |
124 | 126 | end |
125 | 127 | end |
126 | | - end |
127 | 128 |
|
128 | | - raise # if no match by now, raise |
129 | | - rescue |
130 | | - raise SchemaMismatchError, "#{data} doesn't match the #{klass} type" |
| 129 | + raise # if no match by now, raise |
| 130 | + rescue |
| 131 | + raise SchemaMismatchError, "#{data} doesn't match the #{klass} type" |
| 132 | + end |
131 | 133 | end |
132 | 134 | {{/discriminator}} |
133 | 135 | end |
0 commit comments