jbuilder-schema 2.6.2 → 2.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8ca9a375b705e699e2fd70f4e1b78b034b3d261b2b154edbf27115e726d5063a
4
- data.tar.gz: 58b61b54f0fb57d79ec201d5fdae5e6f5fc55f34927e67bf11efa6fb0a5bc95b
3
+ metadata.gz: d3b0ea78dca91b88897944b81769f35798117c891702c585c6197a18c141736a
4
+ data.tar.gz: 668c8e606e9a52392114fb94dfe73f333ab7831dba7fd21c602fbcad1ee37c0c
5
5
  SHA512:
6
- metadata.gz: 9a206a642fc33750c2b58b1ab2108a407bed1806158760aea112f6f8ddd6792a2b4be31102cf10ca68eacdafabdcf02bb193e98e6cf81d4e725511a4cfeb70ac
7
- data.tar.gz: '078125afab38274a3bbaf13f9562f7f3be3e5497ef9fa26ebedf7c205ab77968f4a4c77b5f53e5f5aedee9f19f7dec6628708602875d2d7086f15c26d1f23e28'
6
+ metadata.gz: 7277d58ee426c5635adbbae4b1f8e2b68fc0b16aa6ab42653d1e7dc7ebb62c3f13e3917e767b57ee723f82d52043064db415690e18207820fa6036584b9b4faa
7
+ data.tar.gz: 1c8b45fcf22dd2a219fd22c3483c4d68fa05d0f519fa66eb5593a1476fe73e98bc2a0cb3f07635933890bb297d0183d5451c152d1e10a567b3797ad1deaff9f6
@@ -63,7 +63,7 @@ class Jbuilder::Schema
63
63
 
64
64
  # Rails 7.1 calls `to_s` on our `target!` (the return value from our templates).
65
65
  # To get around that and let our inner Hash through, we add this override.
66
- # `unwrap_target!` is added for backwardscompatibility so we get the inner Hash on Rails < 7.1.
66
+ # `unwrap_target!` is added for backwards compatibility so we get the inner Hash on Rails < 7.1.
67
67
  def to_s
68
68
  @object
69
69
  end
@@ -212,29 +212,63 @@ 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
+ {
261
+ type: type,
262
+ properties: _set_properties(key, value)
263
+ }
264
+ else
265
+ {type: type}
266
+ end
267
+ end
268
+
236
269
  def _primitive_type(value)
237
270
  case value
271
+ when ::Hash, ::Struct, ::OpenStruct, ::ActiveRecord::Base then :object
238
272
  when ::Array then :array
239
273
  when ::Float, ::BigDecimal then :number
240
274
  when true, false then :boolean
@@ -245,11 +279,22 @@ class Jbuilder::Schema
245
279
  end
246
280
 
247
281
  def _set_value(key, value)
282
+ value = _value(value)
248
283
  value = _schema(key, value) unless value.is_a?(::Hash) && (value.key?(:type) || value.key?(:allOf)) # rubocop:disable Style/UnlessLogicalOperators
249
284
  _set_description(key, value)
250
285
  super
251
286
  end
252
287
 
288
+ def _set_properties(key, value)
289
+ _value(value).each_with_object({}) do |(attr_name, attr_value), properties|
290
+ properties[attr_name] = _schema("#{key}.#{attr_name}", attr_value)
291
+ end
292
+ end
293
+
294
+ def _value(value)
295
+ value.respond_to?(:attributes) ? value.attributes : value
296
+ end
297
+
253
298
  def _required!(keys)
254
299
  presence_validated_attributes = @configuration.object&.class.try(:validators).to_a.flat_map { _1.attributes if _1.is_a?(::ActiveRecord::Validations::PresenceValidator) }
255
300
  keys & [_key(:id), *presence_validated_attributes.flat_map { [_key(_1), _key("#{_1}_id")] }]
@@ -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.4"
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.4
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-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jbuilder