|
2 | 2 | # {{{.}}}
|
3 | 3 | {{/description}}
|
4 | 4 | module {{classname}}
|
5 |
| - class << self |
6 |
| - {{#oneOf}} |
7 |
| - {{#-first}} |
8 |
| - # List of class defined in oneOf (OpenAPI v3) |
9 |
| - def openapi_one_of |
10 |
| - [ |
11 |
| - {{/-first}} |
12 |
| - :'{{{.}}}'{{^-last}},{{/-last}} |
13 |
| - {{#-last}} |
14 |
| - ] |
15 |
| - end |
| 5 | + {{#oneOf}} |
| 6 | + {{#-first}} |
| 7 | + # List of class defined in oneOf (OpenAPI v3) |
| 8 | + def self.openapi_one_of |
| 9 | + [ |
| 10 | + {{/-first}} |
| 11 | + :"{{{.}}}"{{^-last}},{{/-last}} |
| 12 | + {{#-last}} |
| 13 | + ] |
| 14 | + end |
16 | 15 |
|
17 |
| - {{/-last}} |
18 |
| - {{/oneOf}} |
19 |
| - {{#discriminator}} |
20 |
| - {{#propertyName}} |
21 |
| - # Discriminator's property name (OpenAPI v3) |
22 |
| - def openapi_discriminator_name |
23 |
| - :'{{{.}}}' |
24 |
| - end |
| 16 | + {{/-last}} |
| 17 | + {{/oneOf}} |
| 18 | + {{#discriminator}} |
| 19 | + {{#propertyName}} |
| 20 | + # Discriminator's property name (OpenAPI v3) |
| 21 | + def self.openapi_discriminator_name |
| 22 | + :"{{{.}}}" |
| 23 | + end |
25 | 24 |
|
26 |
| - {{/propertyName}} |
27 |
| - {{#mappedModels}} |
28 |
| - {{#-first}} |
29 |
| - # Discriminator's mapping (OpenAPI v3) |
30 |
| - def openapi_discriminator_mapping |
31 |
| - { |
32 |
| - {{/-first}} |
33 |
| - :'{{{mappingName}}}' => :'{{{modelName}}}'{{^-last}},{{/-last}} |
34 |
| - {{#-last}} |
35 |
| - } |
36 |
| - end |
| 25 | + {{/propertyName}} |
| 26 | + {{#mappedModels}} |
| 27 | + {{#-first}} |
| 28 | + # Discriminator's mapping (OpenAPI v3) |
| 29 | + def self.openapi_discriminator_mapping |
| 30 | + { |
| 31 | + {{/-first}} |
| 32 | + :"{{{mappingName}}}" => :"{{{modelName}}}"{{^-last}},{{/-last}} |
| 33 | + {{#-last}} |
| 34 | + } |
| 35 | + end |
37 | 36 |
|
38 |
| - {{/-last}} |
39 |
| - {{/mappedModels}} |
40 |
| - {{/discriminator}} |
41 |
| - # Builds the object |
42 |
| - # @param [Mixed] Data to be matched against the list of oneOf items |
43 |
| - # @return [Object] Returns the model or the data itself |
44 |
| - def build(data) |
45 |
| - {{#discriminator}} |
46 |
| - discriminator_value = data[openapi_discriminator_name] |
47 |
| - return nil unless discriminator_value |
48 |
| - {{#mappedModels}} |
49 |
| - {{#-first}} |
| 37 | + {{/-last}} |
| 38 | + {{/mappedModels}} |
| 39 | + {{/discriminator}} |
| 40 | + # Builds the object |
| 41 | + # @param [Mixed] Data to be matched against the list of oneOf items |
| 42 | + # @return [Object] Returns the model or the data itself |
| 43 | + def self.build(data) |
| 44 | + {{#discriminator}} |
| 45 | + discriminator_value = data[openapi_discriminator_name] |
| 46 | + return nil unless discriminator_value |
| 47 | + {{#mappedModels}} |
| 48 | + {{#-first}} |
50 | 49 |
|
51 |
| - klass = openapi_discriminator_mapping[discriminator_value.to_sym] |
52 |
| - return nil unless klass |
| 50 | + klass = openapi_discriminator_mapping[discriminator_value.to_sym] |
| 51 | + return nil unless klass |
53 | 52 |
|
54 |
| - {{moduleName}}.const_get(klass).build_from_hash(data) |
55 |
| - {{/-first}} |
56 |
| - {{/mappedModels}} |
57 |
| - {{^mappedModels}} |
58 |
| - {{moduleName}}.const_get(discriminator_value).build_from_hash(data) |
59 |
| - {{/mappedModels}} |
60 |
| - {{/discriminator}} |
61 |
| - {{^discriminator}} |
62 |
| - # Go through the list of oneOf items and attempt to identify the appropriate one. |
63 |
| - # Note: |
64 |
| - # - We do not attempt to check whether exactly one item matches. |
65 |
| - # - No advanced validation of types in some cases (e.g. "x: { type: string }" will happily match { x: 123 }) |
66 |
| - # due to the way the deserialization is made in the base_object template (it just casts without verifying). |
67 |
| - # - TODO: scalar values are de facto behaving as if they were nullable. |
68 |
| - # - TODO: logging when debugging is set. |
69 |
| - openapi_one_of.each do |klass| |
70 |
| - begin |
71 |
| - next if klass == :AnyType # "nullable: true" |
72 |
| - typed_data = find_and_cast_into_type(klass, data) |
73 |
| - return typed_data if typed_data |
74 |
| - rescue # rescue all errors so we keep iterating even if the current item lookup raises |
75 |
| - end |
| 53 | + {{moduleName}}.const_get(klass).build_from_hash(data) |
| 54 | + {{/-first}} |
| 55 | + {{/mappedModels}} |
| 56 | + {{^mappedModels}} |
| 57 | + {{moduleName}}.const_get(discriminator_value).build_from_hash(data) |
| 58 | + {{/mappedModels}} |
| 59 | + {{/discriminator}} |
| 60 | + {{^discriminator}} |
| 61 | + # Go through the list of oneOf items and attempt to identify the appropriate one. |
| 62 | + # Note: |
| 63 | + # - We do not attempt to check whether exactly one item matches. |
| 64 | + # - No advanced validation of types in some cases (e.g. "x: { type: string }" will happily match { x: 123 }) |
| 65 | + # due to the way the deserialization is made in the base_object template (it just casts without verifying). |
| 66 | + # - TODO: scalar values are de facto behaving as if they were nullable. |
| 67 | + # - TODO: logging when debugging is set. |
| 68 | + openapi_one_of.each do |klass| |
| 69 | + begin |
| 70 | + next if klass == :AnyType # "nullable: true" |
| 71 | + typed_data = find_and_cast_into_type(klass, data) |
| 72 | + return typed_data if typed_data |
| 73 | + rescue # rescue all errors so we keep iterating even if the current item lookup raises |
76 | 74 | end
|
77 |
| - |
78 |
| - openapi_one_of.includes?(:AnyType) ? data : nil |
79 |
| - {{/discriminator}} |
80 | 75 | end
|
81 |
| - {{^discriminator}} |
82 | 76 |
|
83 |
| - private |
| 77 | + openapi_one_of.includes?(:AnyType) ? data : nil |
| 78 | + {{/discriminator}} |
| 79 | + end |
| 80 | + {{^discriminator}} |
84 | 81 |
|
85 |
| - SchemaMismatchError = Class.new(StandardError) |
| 82 | + SchemaMismatchError = Class.new(StandardError) |
86 | 83 |
|
87 |
| - # Note: 'File' is missing here because in the regular case we get the data _after_ a call to JSON.parse. |
88 |
| - def find_and_cast_into_type(klass, data) |
89 |
| - return if data.nil? |
| 84 | + # Note: 'File' is missing here because in the regular case we get the data _after_ a call to JSON.parse. |
| 85 | + private def self.find_and_cast_into_type(klass, data) |
| 86 | + return if data.nil? |
90 | 87 |
|
| 88 | + begin |
91 | 89 | case klass.to_s
|
92 |
| - when 'Boolean' |
| 90 | + when "Boolean" |
93 | 91 | return data if data.instance_of?(TrueClass) || data.instance_of?(FalseClass)
|
94 |
| - when 'Float' |
| 92 | + when "Float" |
95 | 93 | return data if data.instance_of?(Float)
|
96 |
| - when 'Integer' |
| 94 | + when "Integer" |
97 | 95 | return data if data.instance_of?(Integer)
|
98 |
| - when 'Time' |
| 96 | + when "Time" |
99 | 97 | return Time.parse(data)
|
100 |
| - when 'Date' |
| 98 | + when "Date" |
101 | 99 | return Date.parse(data)
|
102 |
| - when 'String' |
| 100 | + when "String" |
103 | 101 | return data if data.instance_of?(String)
|
104 |
| - when 'Object' # "type: object" |
| 102 | + when "Object" # "type: object" |
105 | 103 | return data if data.instance_of?(Hash)
|
106 | 104 | when /\AArray<(?<sub_type>.+)>\z/ # "type: array"
|
107 | 105 | if data.instance_of?(Array)
|
|
111 | 109 | when /\AHash<String, (?<sub_type>.+)>\z/ # "type: object" with "additionalProperties: { ... }"
|
112 | 110 | if data.instance_of?(Hash) && data.keys.all? { |k| k.instance_of?(Symbol) || k.instance_of?(String) }
|
113 | 111 | sub_type = Regexp.last_match[:sub_type]
|
114 |
| - return data.each_with_object({}) { |(k, v), hsh| hsh[k] = find_and_cast_into_type(sub_type, v) } |
| 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) } |
115 | 113 | end
|
116 | 114 | else # model
|
117 | 115 | const = {{moduleName}}.const_get(klass)
|
|
132 | 130 | rescue
|
133 | 131 | raise SchemaMismatchError, "#{data} doesn't match the #{klass} type"
|
134 | 132 | end
|
135 |
| - {{/discriminator}} |
136 | 133 | end
|
| 134 | + {{/discriminator}} |
137 | 135 | end
|
0 commit comments