jbuilder-schema 2.6.2 → 2.6.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8ca9a375b705e699e2fd70f4e1b78b034b3d261b2b154edbf27115e726d5063a
4
- data.tar.gz: 58b61b54f0fb57d79ec201d5fdae5e6f5fc55f34927e67bf11efa6fb0a5bc95b
3
+ metadata.gz: 3ece2220807094bf6da825da24512a4dabd0ce985aa935a16e9247c0e5d4c70e
4
+ data.tar.gz: f04dc67915f12690c7eca1efc2357f915351584061a6b0ece6598326606b0bbf
5
5
  SHA512:
6
- metadata.gz: 9a206a642fc33750c2b58b1ab2108a407bed1806158760aea112f6f8ddd6792a2b4be31102cf10ca68eacdafabdcf02bb193e98e6cf81d4e725511a4cfeb70ac
7
- data.tar.gz: '078125afab38274a3bbaf13f9562f7f3be3e5497ef9fa26ebedf7c205ab77968f4a4c77b5f53e5f5aedee9f19f7dec6628708602875d2d7086f15c26d1f23e28'
6
+ metadata.gz: eb350992a61f92fbaee952b0167369caf119fd2d3f2695f647e1d774974e461e01e6a9cd1b7d5d4da2c19abe0658f8a74b601bedeff7d4e78de8365a6c61b02b
7
+ data.tar.gz: c9582ca4c80ba75106468c8b806cc443be3ee54a630afaa786524d249a4ef8d8bc5eeaef1f1beebb2b00c32e739cfe23be7734695cfff04a51f95de326a7fbd4
@@ -212,29 +212,70 @@ class Jbuilder::Schema
212
212
  FORMATS = {::DateTime => "date-time", ::ActiveSupport::TimeWithZone => "date-time", ::Date => "date", ::Time => "time"}
213
213
 
214
214
  def _schema(key, value, **options)
215
+ within_array = options.delete(:within_array)
215
216
  options = @schema_overrides&.dig(key).to_h if options.empty?
216
217
 
217
218
  unless options[:type]
218
219
  options[:type] = _primitive_type value
219
220
 
220
- if options[:type] == :array && (types = value.map { _primitive_type _1 }.uniq).any?
221
+ if options[:type] == :array && (types = value.map { _primitive_type _1 }).any?
221
222
  options[:minContains] = 0
222
- options[:contains] = {type: types.many? ? types : types.first}
223
+
224
+ # Merge all arrays in one so we have all possible array items in one place
225
+ if types.include?(:array) && types.count(:array) > 1
226
+ array_indices = types.each_index.select { |i| types[i] == :array }
227
+ merged_array = array_indices.each_with_object([]) { |i, arr| arr.concat(value[i]) }
228
+ array_indices.each { |i| value[i] = merged_array }
229
+ end
230
+
231
+ options[:contains] = if types.uniq { |type| (type == :object) ? type.object_id : type }.many?
232
+ any_of = types.map.with_index do |type, index|
233
+ _fill_contains(key, value[index], type)
234
+ end
235
+
236
+ {anyOf: any_of.uniq}
237
+ else
238
+ _fill_contains(key, value[0], types.first)
239
+ end
240
+ elsif options[:type] == :object
241
+ options[:properties] = _set_properties(key, value)
223
242
  end
224
243
 
225
- format = FORMATS[value.class] and options[:format] ||= format
244
+ (format = FORMATS[value.class]) and options[:format] ||= format
226
245
  end
227
246
 
228
247
  if (klass = @configuration.object&.class) && (defined_enum = klass.try(:defined_enums)&.dig(key.to_s))
229
248
  options[:enum] = defined_enum.keys
230
249
  end
231
250
 
232
- _set_description key, options
251
+ _set_description key, options unless within_array
233
252
  options
234
253
  end
235
254
 
255
+ def _fill_contains(key, value, type)
256
+ case type
257
+ when :array
258
+ _schema(key, value, within_array: true)
259
+ when :object
260
+ value = value.attributes if value.is_a?(::ActiveRecord::Base)
261
+ {
262
+ type: type,
263
+ properties: _set_properties(key, value)
264
+ }
265
+ else
266
+ {type: type}
267
+ end
268
+ end
269
+
270
+ def _set_properties(key, value)
271
+ value.each_with_object({}) do |(attr_name, attr_value), properties|
272
+ properties[attr_name] = _schema("#{key}.#{attr_name}", attr_value)
273
+ end
274
+ end
275
+
236
276
  def _primitive_type(value)
237
277
  case value
278
+ when ::Hash, ::Struct, ::OpenStruct, ::ActiveRecord::Base then :object
238
279
  when ::Array then :array
239
280
  when ::Float, ::BigDecimal then :number
240
281
  when true, false then :boolean
@@ -1,4 +1,4 @@
1
1
  # We can't use the standard `Jbuilder::Schema::VERSION =` because
2
2
  # `Jbuilder` isn't a regular module namespace, but a class …which also loads Active Support.
3
3
  # So we use trickery, and assign the proper version once `jbuilder/schema.rb` is loaded.
4
- JBUILDER_SCHEMA_VERSION = "2.6.2"
4
+ JBUILDER_SCHEMA_VERSION = "2.6.3"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jbuilder-schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.2
4
+ version: 2.6.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yuri Sidorov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-11-02 00:00:00.000000000 Z
11
+ date: 2023-11-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jbuilder