jbuilder-schema 2.3.1 → 2.5.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: 7e54563767a015e7b3cb32a86f99155d08c90d317ad6c89f79d7634b3a428993
4
- data.tar.gz: d65a34bc554e7d1416b5e9cb0fd0d32b1c26454506ec5148ad7343f001c0bce4
3
+ metadata.gz: 7b91da7438556fc92910dbf0f50ac15c54f62593985f1baacb9d1cda87c09e2e
4
+ data.tar.gz: bd9e173ce9473d32ca6c3a62d0af33411e76782a927c6ec6b0542a23a8f340dc
5
5
  SHA512:
6
- metadata.gz: 11a516b42e9d28a515e504a59b5f51916d5e8ba74211dda0bad5eea11e0fae80af546e442e4b71274efce5bb555d2c5d6fd453ee342a966c03fa2f50f72e6fe0
7
- data.tar.gz: a1f2a4c564bdb28b6a8ef72b5fc345fcaac03885cc8819763b9a61139b2ab468472a9ec7494b7a3c2bcab311092a754e8f30521ebbcd2c1807bc515814d371f1
6
+ metadata.gz: d5be408235c5190215d78d4d5946f4bb3a28becd80ccbb3a727225efc5f9c48f48903b8ed719296b344bcbf648f9a0d9a2e93ccedf4e475e4e874ca7142b6fa0
7
+ data.tar.gz: 89775b8f6a5819561eb33c8e0af6847167db2de6c0f719981de6bf393e072234fd36baf763dace85e773c5cd761dc93781c81c2a7548f19811111cefa24114e6
data/Gemfile CHANGED
@@ -12,3 +12,13 @@ gem "sqlite3"
12
12
  gem "standard", "~> 1.3"
13
13
 
14
14
  gem "mocha"
15
+
16
+ rails_version = ENV.fetch("RAILS_VERSION", "7.0")
17
+
18
+ rails_constraint = if rails_version == "main"
19
+ {github: "rails/rails"}
20
+ else
21
+ "~> #{rails_version}.0"
22
+ end
23
+
24
+ gem "rails", rails_constraint
@@ -37,7 +37,9 @@ class Jbuilder::Schema::Renderer
37
37
  options[:locals].merge! @default_locals if @default_locals
38
38
  options[:locals][:__jbuilder_schema_options] = {json: json, object: object, title: title, description: description}
39
39
 
40
- @view_renderer.render(options)
40
+ @view_renderer.render(options).then do |result|
41
+ result.respond_to?(:unwrap_target!) ? result.unwrap_target! : result
42
+ end
41
43
  end
42
44
 
43
45
  # Thin wrapper around the regular Jbuilder JSON output render, which also parses it into a hash.
@@ -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,18 +53,28 @@ class Jbuilder::Schema
52
53
  @configuration = Configuration.new(**options)
53
54
  super(context)
54
55
  @ignore_nil = false
56
+ @within_block = false
57
+ end
58
+
59
+ class TargetWrapper
60
+ def initialize(object)
61
+ @object = object
62
+ end
63
+
64
+ # Rails 7.1 calls `to_s` on our `target!` (the return value from our templates).
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.
67
+ def to_s
68
+ @object
69
+ end
70
+ alias_method :unwrap_target!, :to_s
55
71
  end
56
72
 
57
73
  def target!
58
- schema!
74
+ TargetWrapper.new(schema!)
59
75
  end
60
76
 
61
77
  def schema!
62
- # TODO: Not sure why it was like that, when it was meant to be used,
63
- # but that seems to fix the problem with disappearance of root properties
64
- # https://github.com/bullet-train-co/jbuilder-schema/issues/46
65
- # if ([@attributes] + @attributes.each_value.grep(::Hash)).any? { _1[:type] == :array && _1.key?(:items) }
66
-
67
78
  if [@attributes, *@attributes.first].select { |a| a.is_a?(::Hash) && a[:type] == :array && a.key?(:items) }.any?
68
79
  @attributes
69
80
  else
@@ -73,6 +84,7 @@ class Jbuilder::Schema
73
84
 
74
85
  def set!(key, value = BLANK, *args, schema: nil, **options, &block)
75
86
  old_configuration, @configuration = @configuration, Configuration.build(**schema) if schema&.dig(:object)
87
+ @within_block = _within_block?(&block)
76
88
 
77
89
  _with_schema_overrides(key => schema) do
78
90
  keys = args.presence || _extract_possible_keys(value)
@@ -88,6 +100,7 @@ class Jbuilder::Schema
88
100
  end
89
101
  ensure
90
102
  @configuration = old_configuration if old_configuration
103
+ @within_block = false
91
104
  end
92
105
 
93
106
  alias_method :method_missing, :set! # TODO: Remove once Jbuilder passes keyword arguments along to `set!` in its `method_missing`.
@@ -96,10 +109,14 @@ class Jbuilder::Schema
96
109
  if _partial_options?(options)
97
110
  partial!(collection: collection, **options)
98
111
  else
112
+ @within_block = _within_block?(&block)
113
+
99
114
  _with_schema_overrides(schema) do
100
115
  _attributes.merge! type: :array, items: _scope { super(collection, *args, &block) }
101
116
  end
102
117
  end
118
+ ensure
119
+ @within_block = false
103
120
  end
104
121
 
105
122
  def extract!(object, *attributes, schema: nil)
@@ -114,7 +131,12 @@ class Jbuilder::Schema
114
131
  local = options.except(:partial, :as, :collection, :cached, :schema).first
115
132
  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)
116
133
 
117
- _set_ref(as&.to_s || partial || model, array: collection&.any?)
134
+ if @within_block || collection.present?
135
+ _set_ref(as&.to_s || partial || model, array: collection&.any?)
136
+ else
137
+ json = ::Jbuilder::Schema.renderer.original_render partial: model || partial, locals: options
138
+ json.each { |key, value| set!(key, value) }
139
+ end
118
140
  end
119
141
  end
120
142
 
@@ -240,5 +262,15 @@ class Jbuilder::Schema
240
262
  value = _object(value, _required!(value.keys)) unless value[:type] == :array || value.key?(:$ref)
241
263
  _merge_values(current_value, value)
242
264
  end
265
+
266
+ def _within_block?(&block)
267
+ block.present? && _one_line?(block.source)
268
+ end
269
+
270
+ def _one_line?(text)
271
+ text = text.gsub("{", " do\n").gsub("}", "\nend").tr(";", "\n")
272
+ lines = text.lines[1..-2].reject { |line| line.strip.empty? || line.strip.start_with?("#") }
273
+ lines.size == 1
274
+ end
243
275
  end
244
276
  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.3.1"
4
+ JBUILDER_SCHEMA_VERSION = "2.5.0"
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.3.1
4
+ version: 2.5.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-06 00:00:00.000000000 Z
11
+ date: 2023-10-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jbuilder
@@ -31,9 +31,6 @@ dependencies:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 5.0.0
34
- - - "<"
35
- - !ruby/object:Gem::Version
36
- version: 7.1.0
37
34
  type: :runtime
38
35
  prerelease: false
39
36
  version_requirements: !ruby/object:Gem::Requirement
@@ -41,9 +38,20 @@ dependencies:
41
38
  - - ">="
42
39
  - !ruby/object:Gem::Version
43
40
  version: 5.0.0
44
- - - "<"
41
+ - !ruby/object:Gem::Dependency
42
+ name: method_source
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
45
46
  - !ruby/object:Gem::Version
46
- version: 7.1.0
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'
47
55
  description: Generate JSON Schema from Jbuilder files
48
56
  email:
49
57
  - hey@yurisidorov.com
@@ -53,7 +61,6 @@ extra_rdoc_files: []
53
61
  files:
54
62
  - ".standard.yml"
55
63
  - Gemfile
56
- - Gemfile.lock
57
64
  - LICENSE.txt
58
65
  - README.md
59
66
  - Rakefile
data/Gemfile.lock DELETED
@@ -1,215 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- jbuilder-schema (2.3.1)
5
- jbuilder
6
- rails (>= 5.0.0, < 7.1.0)
7
-
8
- GEM
9
- remote: https://rubygems.org/
10
- specs:
11
- actioncable (7.0.8)
12
- actionpack (= 7.0.8)
13
- activesupport (= 7.0.8)
14
- nio4r (~> 2.0)
15
- websocket-driver (>= 0.6.1)
16
- actionmailbox (7.0.8)
17
- actionpack (= 7.0.8)
18
- activejob (= 7.0.8)
19
- activerecord (= 7.0.8)
20
- activestorage (= 7.0.8)
21
- activesupport (= 7.0.8)
22
- mail (>= 2.7.1)
23
- net-imap
24
- net-pop
25
- net-smtp
26
- actionmailer (7.0.8)
27
- actionpack (= 7.0.8)
28
- actionview (= 7.0.8)
29
- activejob (= 7.0.8)
30
- activesupport (= 7.0.8)
31
- mail (~> 2.5, >= 2.5.4)
32
- net-imap
33
- net-pop
34
- net-smtp
35
- rails-dom-testing (~> 2.0)
36
- actionpack (7.0.8)
37
- actionview (= 7.0.8)
38
- activesupport (= 7.0.8)
39
- rack (~> 2.0, >= 2.2.4)
40
- rack-test (>= 0.6.3)
41
- rails-dom-testing (~> 2.0)
42
- rails-html-sanitizer (~> 1.0, >= 1.2.0)
43
- actiontext (7.0.8)
44
- actionpack (= 7.0.8)
45
- activerecord (= 7.0.8)
46
- activestorage (= 7.0.8)
47
- activesupport (= 7.0.8)
48
- globalid (>= 0.6.0)
49
- nokogiri (>= 1.8.5)
50
- actionview (7.0.8)
51
- activesupport (= 7.0.8)
52
- builder (~> 3.1)
53
- erubi (~> 1.4)
54
- rails-dom-testing (~> 2.0)
55
- rails-html-sanitizer (~> 1.1, >= 1.2.0)
56
- activejob (7.0.8)
57
- activesupport (= 7.0.8)
58
- globalid (>= 0.3.6)
59
- activemodel (7.0.8)
60
- activesupport (= 7.0.8)
61
- activerecord (7.0.8)
62
- activemodel (= 7.0.8)
63
- activesupport (= 7.0.8)
64
- activestorage (7.0.8)
65
- actionpack (= 7.0.8)
66
- activejob (= 7.0.8)
67
- activerecord (= 7.0.8)
68
- activesupport (= 7.0.8)
69
- marcel (~> 1.0)
70
- mini_mime (>= 1.1.0)
71
- activesupport (7.0.8)
72
- concurrent-ruby (~> 1.0, >= 1.0.2)
73
- i18n (>= 1.6, < 2)
74
- minitest (>= 5.1)
75
- tzinfo (~> 2.0)
76
- ast (2.4.2)
77
- base64 (0.1.1)
78
- builder (3.2.4)
79
- concurrent-ruby (1.2.2)
80
- crass (1.0.6)
81
- date (3.3.3)
82
- erubi (1.12.0)
83
- globalid (1.2.1)
84
- activesupport (>= 6.1)
85
- i18n (1.14.1)
86
- concurrent-ruby (~> 1.0)
87
- jbuilder (2.11.5)
88
- actionview (>= 5.0.0)
89
- activesupport (>= 5.0.0)
90
- json (2.6.3)
91
- language_server-protocol (3.17.0.3)
92
- lint_roller (1.1.0)
93
- loofah (2.21.3)
94
- crass (~> 1.0.2)
95
- nokogiri (>= 1.12.0)
96
- mail (2.8.1)
97
- mini_mime (>= 0.1.1)
98
- net-imap
99
- net-pop
100
- net-smtp
101
- marcel (1.0.2)
102
- method_source (1.0.0)
103
- mini_mime (1.1.5)
104
- mini_portile2 (2.8.4)
105
- minitest (5.20.0)
106
- mocha (2.1.0)
107
- ruby2_keywords (>= 0.0.5)
108
- net-imap (0.4.0)
109
- date
110
- net-protocol
111
- net-pop (0.1.2)
112
- net-protocol
113
- net-protocol (0.2.1)
114
- timeout
115
- net-smtp (0.4.0)
116
- net-protocol
117
- nio4r (2.5.9)
118
- nokogiri (1.15.4)
119
- mini_portile2 (~> 2.8.2)
120
- racc (~> 1.4)
121
- parallel (1.23.0)
122
- parser (3.2.2.4)
123
- ast (~> 2.4.1)
124
- racc
125
- racc (1.7.1)
126
- rack (2.2.8)
127
- rack-test (2.1.0)
128
- rack (>= 1.3)
129
- rails (7.0.8)
130
- actioncable (= 7.0.8)
131
- actionmailbox (= 7.0.8)
132
- actionmailer (= 7.0.8)
133
- actionpack (= 7.0.8)
134
- actiontext (= 7.0.8)
135
- actionview (= 7.0.8)
136
- activejob (= 7.0.8)
137
- activemodel (= 7.0.8)
138
- activerecord (= 7.0.8)
139
- activestorage (= 7.0.8)
140
- activesupport (= 7.0.8)
141
- bundler (>= 1.15.0)
142
- railties (= 7.0.8)
143
- rails-dom-testing (2.2.0)
144
- activesupport (>= 5.0.0)
145
- minitest
146
- nokogiri (>= 1.6)
147
- rails-html-sanitizer (1.6.0)
148
- loofah (~> 2.21)
149
- nokogiri (~> 1.14)
150
- railties (7.0.8)
151
- actionpack (= 7.0.8)
152
- activesupport (= 7.0.8)
153
- method_source
154
- rake (>= 12.2)
155
- thor (~> 1.0)
156
- zeitwerk (~> 2.5)
157
- rainbow (3.1.1)
158
- rake (13.0.6)
159
- regexp_parser (2.8.1)
160
- rexml (3.2.6)
161
- rubocop (1.56.4)
162
- base64 (~> 0.1.1)
163
- json (~> 2.3)
164
- language_server-protocol (>= 3.17.0)
165
- parallel (~> 1.10)
166
- parser (>= 3.2.2.3)
167
- rainbow (>= 2.2.2, < 4.0)
168
- regexp_parser (>= 1.8, < 3.0)
169
- rexml (>= 3.2.5, < 4.0)
170
- rubocop-ast (>= 1.28.1, < 2.0)
171
- ruby-progressbar (~> 1.7)
172
- unicode-display_width (>= 2.4.0, < 3.0)
173
- rubocop-ast (1.29.0)
174
- parser (>= 3.2.1.0)
175
- rubocop-performance (1.19.1)
176
- rubocop (>= 1.7.0, < 2.0)
177
- rubocop-ast (>= 0.4.0)
178
- ruby-progressbar (1.13.0)
179
- ruby2_keywords (0.0.5)
180
- sqlite3 (1.6.6)
181
- mini_portile2 (~> 2.8.0)
182
- standard (1.31.1)
183
- language_server-protocol (~> 3.17.0.2)
184
- lint_roller (~> 1.0)
185
- rubocop (~> 1.56.2)
186
- standard-custom (~> 1.0.0)
187
- standard-performance (~> 1.2)
188
- standard-custom (1.0.2)
189
- lint_roller (~> 1.0)
190
- rubocop (~> 1.50)
191
- standard-performance (1.2.0)
192
- lint_roller (~> 1.1)
193
- rubocop-performance (~> 1.19.0)
194
- thor (1.2.2)
195
- timeout (0.4.0)
196
- tzinfo (2.0.6)
197
- concurrent-ruby (~> 1.0)
198
- unicode-display_width (2.5.0)
199
- websocket-driver (0.7.6)
200
- websocket-extensions (>= 0.1.0)
201
- websocket-extensions (0.1.5)
202
- zeitwerk (2.6.12)
203
-
204
- PLATFORMS
205
- ruby
206
-
207
- DEPENDENCIES
208
- jbuilder-schema!
209
- mocha
210
- rake (~> 13.0)
211
- sqlite3
212
- standard (~> 1.3)
213
-
214
- BUNDLED WITH
215
- 2.4.10