80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/datacaster/json_schema_result.rb', line 80
def apply(other, schema_attributes = {})
return self if other.nil? || other.empty?
return JsonSchemaResult.new(other) if empty?
if @focus && !@focus.empty?
return with_updated_target(JsonSchemaResult.new(@target).apply(other))
end
self_type = self['type']
other_type = other['type']
if (self_type == 'object' || self_type == 'array') && (other_type != 'object' && other_type != 'array')
return JsonSchemaResult.new(self)
end
result = self.class.new({})
if self['required'] || other['required']
result['required'] = (
(self['required'] || []).to_set | (other['required'] || []).to_set
).to_a
end
nested =
if self['properties'] && (other['items'] || self['items']) ||
self['items'] && (self['properties'] || other['properties']) ||
other['items'] && other['properties']
raise RuntimeError, "can't merge json schemas due to wrong items/properties combination " \
"for #{self.inspect} and #{other.inspect}", caller
elsif self['properties'] || other['properties']
'properties'
elsif self['items'] || other['items']
'items'
else
nil
end
if nested
result[nested] = {}
keys = (self[nested] || {}).keys + (other[nested] || {}).keys
keys = keys.to_set
keys.each do |k|
one_k = self[nested] && self[nested][k] || {}
two_k = other[nested] && other[nested][k] || {}
if !one_k.is_a?(Hash) || !two_k.is_a?(Hash)
if one_k.empty? && !two_k.is_a?(Hash)
result[nested][k] = two_k
elsif two_k.empty? && !one_k.is_a?(Hash)
result[nested][k] = one_k
elsif one_k == two_k
result[nested][k] = one_k
else
raise RuntimeError, "can't merge json schemas due to wrong items/properties combination " \
"for #{self.inspect} and #{other.inspect}", caller
end
elsif one_k.is_a?(Hash) && two_k.is_a?(Hash)
result[nested][k] = self.class.new(one_k).apply(two_k)
else
raise RuntimeError, "can't merge json schemas due to wrong items/properties combination " \
"for #{self.inspect} and #{other.inspect}", caller
end
end
end
if self['description'] || other['description']
result['description'] = other['description'] || self['description']
end
(self.keys + other.keys - %w(required properties items description)).to_set.each do |k|
if schema_attributes[:extendable]
case k
in 'oneOf'
self_one_of = self[k]
other_one_of = other[k]
result_objects = other_one_of.map do |other_obj|
other_obj_properties = other_obj['properties'].to_a
max_same = -1
merge_candidate = self_one_of.max_by do |self_obj|
next -1 if self_obj.empty?
self_obj_properties = self_obj['properties'].to_a
max_same = (self_obj_properties & other_obj_properties).size
max_same
end
next other_obj if max_same < 1
Datacaster::Utils.deep_merge(other_obj, merge_candidate)
end
next result[k] = result_objects
else
raise RuntimeError, "can't merge json schemas due to conflicting field #{k} for " \
"#{inspect} and #{other.inspect}", caller
end
else
if self[k] && other[k] && self[k] != other[k]
raise RuntimeError, "can't merge json schemas due to conflicting field #{k} for " \
"#{inspect} and #{other.inspect}", caller
end
end
result[k] = other[k] || self[k]
end
result
end
|