31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
|
# File 'lib/json_schema/faker/util.rb', line 31
def take_logical_and_of_schema(a, b, a_position: nil, b_position: nil)
return a || b if a.nil? || b.nil?
a = ::JsonSchema::Schema.new.tap {|s| s.copy_from(a) }
if a.multiple_of && b.multiple_of
::JsonSchema::Faker::Configuration.logger.warn "not support merging multipleOf" if ::JsonSchema::Faker::Configuration.logger
end
if b.max
if !a.max || a.max > b.max
a.max = b.max
end
a.max_exclusive = b.max_exclusive if b.max_exclusive
end
if b.min
if !a.min || a.min < b.min
a.min = b.min
end
a.min_exclusive = b.min_exclusive if b.min_exclusive
end
if b.max_length
if !a.max_length || a.max_length > b.max_length
a.max_length = b.max_length
end
end
if b.min_length
if !a.min_length || a.min_length < b.min_length
a.min_length = b.min_length
end
end
if b.pattern
if a.pattern
::JsonSchema::Faker::Configuration.logger.warn "not support merging pattern" if ::JsonSchema::Faker::Configuration.logger
else
a.pattern = b.pattern
end
end
if b.items
if a.items
if a.items.is_a?(Array) && b.items.is_a?(Array)
original_a_items = a.items
a.items = [] [ original_a_items.size, b.items.size ].max.times do |i|
ai, bi = original_a_items[i], b.items[i]
if ai && bi
a.items[i] = take_logical_and_of_schema(ai, bi)
else
a.items[i] = ai || bi
end
end
elsif a.items.is_a?(::JsonSchema::Schema) && b.items.is_a?(::JsonSchema::Schema)
a.items = take_logical_and_of_schema(a.items, b.items)
end
else
a.items = b.items
end
end
if a.additional_items === false || b.additional_items === false
a.additonal_items = false
else
if b.additional_items.is_a?(::JsonSchema::Schema)
if a.additional_items.is_a?(::JsonSchem::Schema)
::JsonSchema::Faker::Configuration.logger.warn "not support merging additionalItems" if ::JsonSchema::Faker::Configuration.logger
else
a.additional_items = b.additional_items
end
end
end
if b.max_items
if !a.max_items || a.max_items > b.max_items
a.max_items = b.max_items
end
end
if b.min_items
if !a.min_items || a.min_items < b.min_items
a.min_items = b.min_items
end
end
if a.unique_items || b.unique_items
a.unique_items = true
end
if b.max_properties
if !a.max_properties || a.max_properties > b.max_properties
a.max_properties = b.max_properties
end
end
if b.min_properties
if !a.min_properties || a.min_properties < b.min_properties
a.min_properties = b.min_properties
end
end
if b.required
a.required = a.required ? (a.required + b.required).uniq : b.required
end
if !b.properties.empty?
original_a_properties = a.properties
a.properties = {} ( original_a_properties.keys + b.properties.keys ).uniq.each do |key|
a.properties[key] = take_logical_and_of_schema(original_a_properties[key], b.properties[key])
end
end
if a.additional_properties === false || b.additional_properties === false
a.additional_properties = false
else
if b.additional_properties.is_a?(::JsonSchema::Schema)
if a.additional_properties.is_a?(::JsonSchem::Schema)
::JsonSchema::Faker::Configuration.logger.warn "not support merging additionalProperties" if ::JsonSchema::Faker::Configuration.logger
else
a.additional_properties = b.additional_properties
end
end
end
if !b.pattern_properties.empty?
if !a.pattern_properties.empty?
::JsonSchema::Faker::Configuration.logger.warn "not support merging patternProperties" if ::JsonSchema::Faker::Configuration.logger
else
a.pattern_properties = b.pattern_properties
end
end
if !b.dependencies.empty?
if !a.dependencies.empty?
::JsonSchema::Faker::Configuration.logger.warn "not support merging dependencies" if ::JsonSchema::Faker::Configuration.logger
else
a.dependencies = b.dependencies
end
end
if b.enum
if a.enum
a.enum = a.enum & b.enum
else
a.enum = b.enum
end
end
if !b.type.empty?
if !a.type.empty?
a.type = a.type & b.type
else
a.type = b.type
end
end
if !b.all_of.empty?
if !a.all_of.empty?
a.all_of = [
[ *a.all_of, *b.all_of ].inject(nil) {|res, c| res ? take_logical_and_of_schema(res, c) : c }
]
else
a.all_of = b.all_of
end
end
if !b.any_of.empty?
if !a.any_of.empty?
a.any_of = take_unique_items(a.any_of, b.any_of)
else
a.any_of = b.any_of
end
end
if !b.one_of.empty?
if !a.one_of.empty?
a.one_of = take_unique_items(a.one_of, b.one_of)
else
a.one_of = b.one_of
end
end
if b.not
if a.not
::JsonSchema::Faker::Configuration.logger.warn "not support merging not" if ::JsonSchema::Faker::Configuration.logger
else
a.not = b.not
end
end
if b.format
if a.format
::JsonSchema::Faker::Configuration.logger.warn "not support merging format" if ::JsonSchema::Faker::Configuration.logger
else
a.format = b.format
end
end
a
end
|