jbuilder-schema 2.4.0 → 2.6.0

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: d21f87d24f37c97ea05dfcdef1688249098d973c62bfec84a98495ae6c57976f
4
- data.tar.gz: c13ab2aeb505785a8915547ce0cf84c7987e0dc0def4e2b20ce5a47be92749d8
3
+ metadata.gz: ee4548cc207982dd75132d63f96e9396757af0b812b8649fb648cf978de0c4a7
4
+ data.tar.gz: fa0b95d49b9b0cb1cde94381bc0f217b08f89900d1b7f2bac72e9e3e2cd6ab41
5
5
  SHA512:
6
- metadata.gz: 2aef6970a39b78310a23e6dbbe441ef64ddb03133968cedec87e0e97ecf8b8b83a1a1842d27ff29aa378f8d751963d041233794a1579bc6ecb722e218f0e3cf2
7
- data.tar.gz: '028fb5e6a97b3a714365c1bcdf94b1f584710839685e81f7c4dd2bc514a68fcccbbcf046ddcc890559573f65696407c02023e8321f642861df3ae9d6b6419b91'
6
+ metadata.gz: e7d4fd3b56407c18a94877387f92f7c53f8632f0c610a7595bb567ec380454b72de0be3c68be5f17d9ece12dc78785aabb965a9a44d078b78b2d25e043c8b93c
7
+ data.tar.gz: 33ed871b094e1bb4103c7e07f4ab949ca9e595fa959433f449aed6e1f81a6b6f85d29da22b4185baee392a9385fdbd20035d0d8b045801844c9d1751675b371e
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "jbuilder/jbuilder_template"
4
4
  require "active_support/inflections"
5
+ require "method_source"
5
6
 
6
7
  class Jbuilder::Schema
7
8
  class Template < ::JbuilderTemplate
@@ -52,6 +53,7 @@ class Jbuilder::Schema
52
53
  @configuration = Configuration.new(**options)
53
54
  super(context)
54
55
  @ignore_nil = false
56
+ @within_block = false
55
57
  end
56
58
 
57
59
  class TargetWrapper
@@ -65,7 +67,7 @@ class Jbuilder::Schema
65
67
  def to_s
66
68
  @object
67
69
  end
68
- alias unwrap_target! to_s
70
+ alias_method :unwrap_target!, :to_s
69
71
  end
70
72
 
71
73
  def target!
@@ -73,11 +75,6 @@ class Jbuilder::Schema
73
75
  end
74
76
 
75
77
  def schema!
76
- # TODO: Not sure why it was like that, when it was meant to be used,
77
- # but that seems to fix the problem with disappearance of root properties
78
- # https://github.com/bullet-train-co/jbuilder-schema/issues/46
79
- # if ([@attributes] + @attributes.each_value.grep(::Hash)).any? { _1[:type] == :array && _1.key?(:items) }
80
-
81
78
  if [@attributes, *@attributes.first].select { |a| a.is_a?(::Hash) && a[:type] == :array && a.key?(:items) }.any?
82
79
  @attributes
83
80
  else
@@ -87,6 +84,7 @@ class Jbuilder::Schema
87
84
 
88
85
  def set!(key, value = BLANK, *args, schema: nil, **options, &block)
89
86
  old_configuration, @configuration = @configuration, Configuration.build(**schema) if schema&.dig(:object)
87
+ @within_block = _within_block?(&block)
90
88
 
91
89
  _with_schema_overrides(key => schema) do
92
90
  keys = args.presence || _extract_possible_keys(value)
@@ -102,6 +100,7 @@ class Jbuilder::Schema
102
100
  end
103
101
  ensure
104
102
  @configuration = old_configuration if old_configuration
103
+ @within_block = false
105
104
  end
106
105
 
107
106
  alias_method :method_missing, :set! # TODO: Remove once Jbuilder passes keyword arguments along to `set!` in its `method_missing`.
@@ -110,10 +109,20 @@ class Jbuilder::Schema
110
109
  if _partial_options?(options)
111
110
  partial!(collection: collection, **options)
112
111
  else
112
+ @within_block = _within_block?(&block)
113
+
113
114
  _with_schema_overrides(schema) do
114
- _attributes.merge! type: :array, items: _scope { super(collection, *args, &block) }
115
+ # TODO: Find a better solution
116
+ # Here we basically remove allOf key from items, because it's redundant, although valid.
117
+ # Better would be not to set it if it's not needed, but I couldn't figure how,
118
+ # as we have array of separate object partials hare, so each one of them would legally have allOf key.
119
+ items = _scope { super(collection, *args, &block) }
120
+ items = items[:allOf].first if items.key?(:allOf)
121
+ _attributes.merge! type: :array, items: items
115
122
  end
116
123
  end
124
+ ensure
125
+ @within_block = false
117
126
  end
118
127
 
119
128
  def extract!(object, *attributes, schema: nil)
@@ -128,7 +137,12 @@ class Jbuilder::Schema
128
137
  local = options.except(:partial, :as, :collection, :cached, :schema).first
129
138
  as = options[:as] || ((local.is_a?(::Array) && local.size == 2 && local.first.is_a?(::Symbol) && local.last.is_a?(::Object)) ? local.first.to_s : nil)
130
139
 
131
- _set_ref(as&.to_s || partial || model, array: collection&.any?)
140
+ if @within_block || collection.present?
141
+ _set_ref(as&.to_s || partial || model, array: collection&.any?)
142
+ else
143
+ json = ::Jbuilder::Schema.renderer.original_render partial: model || partial, locals: options
144
+ json.each { |key, value| set!(key, value) }
145
+ end
132
146
  end
133
147
  end
134
148
 
@@ -166,7 +180,7 @@ class Jbuilder::Schema
166
180
 
167
181
  def _nullify_non_required_types(attributes, required)
168
182
  attributes.transform_values! {
169
- _1[:type] = [_1[:type], "null"] unless required.include?(attributes.key(_1))
183
+ _1[:nullable] = true unless required.include?(attributes.key(_1))
170
184
  _1
171
185
  }
172
186
  end
@@ -183,7 +197,7 @@ class Jbuilder::Schema
183
197
  if array
184
198
  _attributes.merge! type: :array, items: ref
185
199
  else
186
- _attributes.merge! type: :object, **ref
200
+ _attributes.merge! allOf: [ref]
187
201
  end
188
202
  end
189
203
 
@@ -228,7 +242,7 @@ class Jbuilder::Schema
228
242
  end
229
243
 
230
244
  def _set_value(key, value)
231
- value = _schema(key, value) unless value.is_a?(::Hash) && value.key?(:type)
245
+ value = _schema(key, value) unless value.is_a?(::Hash) && (value.key?(:type) || value.key?(:allOf)) # rubocop:disable Style/UnlessLogicalOperators
232
246
  _set_description(key, value)
233
247
  super
234
248
  end
@@ -251,8 +265,19 @@ class Jbuilder::Schema
251
265
  raise NullError.build(key) if current_value.nil?
252
266
 
253
267
  value = _scope { yield self }
254
- value = _object(value, _required!(value.keys)) unless value[:type] == :array || value.key?(:$ref)
268
+ value = _object(value, _required!(value.keys)) unless value[:type] == :array || value.key?(:allOf)
269
+
255
270
  _merge_values(current_value, value)
256
271
  end
272
+
273
+ def _within_block?(&block)
274
+ block.present? && _one_line?(block.source)
275
+ end
276
+
277
+ def _one_line?(text)
278
+ text = text.gsub("{", " do\n").gsub("}", "\nend").tr(";", "\n")
279
+ lines = text.lines[1..-2].reject { |line| line.strip.empty? || line.strip.start_with?("#") }
280
+ lines.size == 1
281
+ end
257
282
  end
258
283
  end
@@ -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.4.0"
4
+ JBUILDER_SCHEMA_VERSION = "2.6.0"
@@ -37,7 +37,7 @@ class Jbuilder::Schema
37
37
  if paths.nil? && locals.nil?
38
38
  @renderer ||= Renderer.new("app/views")
39
39
  else
40
- Renderer.new(paths, locals)
40
+ @renderer = Renderer.new(paths, locals)
41
41
  end
42
42
  end
43
43
  singleton_class.delegate :yaml, :json, :render, to: :renderer
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.4.0
4
+ version: 2.6.0
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-10-09 00:00:00.000000000 Z
11
+ date: 2023-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jbuilder
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 5.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: method_source
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: Generate JSON Schema from Jbuilder files
42
56
  email:
43
57
  - hey@yurisidorov.com