jbuilder-schema 2.1.0 → 2.3.0

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: 8e69ae6e6726679110d135c34a461a2bfc176f967bfa1777b73989ae107fdabc
4
- data.tar.gz: 63fc234f84fe96195c6cc849bf26f4bea6d5e60808a0363e4fdddfe778497292
3
+ metadata.gz: f7d989f5d37de77b8e1a0dc0703619f334f3d5605ec3458b6508d2f1712241d9
4
+ data.tar.gz: 11c116736ae75e9c5e6cca37cd244a538ca68feb3d168d8b2c01861b26515cb4
5
5
  SHA512:
6
- metadata.gz: 2fcc3260655ff07670c2a8be5175c801f29af27790e0675c680fc861bae3a2a48dcd30170cb5468e595ec647d58c9abe92d2177f7c5dc77acdb8569f8ab7997e
7
- data.tar.gz: ca344a595d3ed5c43c602e5d24ca154c4e0b9872388bd055422c957af93891be133618651e423f560ccc419b66c8b068828202f68929161d0790c4efbf71c8cb
6
+ metadata.gz: c069b534a6f5f044193c9fb9432b104e66d7d6d158b2969bea0ceb33be4db85dc178b5d971b3efab75bec54c21d5c05050aa2eeebc8546a86a5f27aaf3ddd41e
7
+ data.tar.gz: 8738536e32a3db5a21037b32acf91a6dcc59b2e69745046ab8d68d03ab847979f36f80801f9be6927276eda7fe8ad1f2573838813575d4ac5802a8294b635097
data/Gemfile.lock CHANGED
@@ -1,184 +1,205 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jbuilder-schema (2.1.0)
4
+ jbuilder-schema (2.3.0)
5
5
  jbuilder
6
6
  rails (>= 5.0.0)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- actioncable (7.0.4)
12
- actionpack (= 7.0.4)
13
- activesupport (= 7.0.4)
11
+ actioncable (7.0.8)
12
+ actionpack (= 7.0.8)
13
+ activesupport (= 7.0.8)
14
14
  nio4r (~> 2.0)
15
15
  websocket-driver (>= 0.6.1)
16
- actionmailbox (7.0.4)
17
- actionpack (= 7.0.4)
18
- activejob (= 7.0.4)
19
- activerecord (= 7.0.4)
20
- activestorage (= 7.0.4)
21
- activesupport (= 7.0.4)
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
22
  mail (>= 2.7.1)
23
23
  net-imap
24
24
  net-pop
25
25
  net-smtp
26
- actionmailer (7.0.4)
27
- actionpack (= 7.0.4)
28
- actionview (= 7.0.4)
29
- activejob (= 7.0.4)
30
- activesupport (= 7.0.4)
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
31
  mail (~> 2.5, >= 2.5.4)
32
32
  net-imap
33
33
  net-pop
34
34
  net-smtp
35
35
  rails-dom-testing (~> 2.0)
36
- actionpack (7.0.4)
37
- actionview (= 7.0.4)
38
- activesupport (= 7.0.4)
39
- rack (~> 2.0, >= 2.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
40
  rack-test (>= 0.6.3)
41
41
  rails-dom-testing (~> 2.0)
42
42
  rails-html-sanitizer (~> 1.0, >= 1.2.0)
43
- actiontext (7.0.4)
44
- actionpack (= 7.0.4)
45
- activerecord (= 7.0.4)
46
- activestorage (= 7.0.4)
47
- activesupport (= 7.0.4)
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
48
  globalid (>= 0.6.0)
49
49
  nokogiri (>= 1.8.5)
50
- actionview (7.0.4)
51
- activesupport (= 7.0.4)
50
+ actionview (7.0.8)
51
+ activesupport (= 7.0.8)
52
52
  builder (~> 3.1)
53
53
  erubi (~> 1.4)
54
54
  rails-dom-testing (~> 2.0)
55
55
  rails-html-sanitizer (~> 1.1, >= 1.2.0)
56
- activejob (7.0.4)
57
- activesupport (= 7.0.4)
56
+ activejob (7.0.8)
57
+ activesupport (= 7.0.8)
58
58
  globalid (>= 0.3.6)
59
- activemodel (7.0.4)
60
- activesupport (= 7.0.4)
61
- activerecord (7.0.4)
62
- activemodel (= 7.0.4)
63
- activesupport (= 7.0.4)
64
- activestorage (7.0.4)
65
- actionpack (= 7.0.4)
66
- activejob (= 7.0.4)
67
- activerecord (= 7.0.4)
68
- activesupport (= 7.0.4)
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
69
  marcel (~> 1.0)
70
70
  mini_mime (>= 1.1.0)
71
- activesupport (7.0.4)
71
+ activesupport (7.0.8)
72
72
  concurrent-ruby (~> 1.0, >= 1.0.2)
73
73
  i18n (>= 1.6, < 2)
74
74
  minitest (>= 5.1)
75
75
  tzinfo (~> 2.0)
76
76
  ast (2.4.2)
77
+ base64 (0.1.1)
77
78
  builder (3.2.4)
78
- concurrent-ruby (1.1.10)
79
+ concurrent-ruby (1.2.2)
79
80
  crass (1.0.6)
81
+ date (3.3.3)
80
82
  erubi (1.12.0)
81
- globalid (1.0.0)
82
- activesupport (>= 5.0)
83
- i18n (1.12.0)
83
+ globalid (1.2.1)
84
+ activesupport (>= 6.1)
85
+ i18n (1.14.1)
84
86
  concurrent-ruby (~> 1.0)
85
87
  jbuilder (2.11.5)
86
88
  actionview (>= 5.0.0)
87
89
  activesupport (>= 5.0.0)
88
- json (2.6.2)
89
- loofah (2.19.1)
90
+ json (2.6.3)
91
+ language_server-protocol (3.17.0.3)
92
+ lint_roller (1.1.0)
93
+ loofah (2.21.3)
90
94
  crass (~> 1.0.2)
91
- nokogiri (>= 1.5.9)
92
- mail (2.8.0)
95
+ nokogiri (>= 1.12.0)
96
+ mail (2.8.1)
93
97
  mini_mime (>= 0.1.1)
94
98
  net-imap
95
99
  net-pop
96
100
  net-smtp
97
101
  marcel (1.0.2)
98
102
  method_source (1.0.0)
99
- mini_mime (1.1.2)
100
- mini_portile2 (2.8.0)
101
- minitest (5.17.0)
102
- mocha (1.14.0)
103
- net-imap (0.3.1)
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.3.7)
109
+ date
104
110
  net-protocol
105
111
  net-pop (0.1.2)
106
112
  net-protocol
107
113
  net-protocol (0.2.1)
108
114
  timeout
109
- net-smtp (0.3.3)
115
+ net-smtp (0.4.0)
110
116
  net-protocol
111
- nio4r (2.5.8)
112
- nokogiri (1.13.9)
113
- mini_portile2 (~> 2.8.0)
117
+ nio4r (2.5.9)
118
+ nokogiri (1.15.4)
119
+ mini_portile2 (~> 2.8.2)
114
120
  racc (~> 1.4)
115
- parallel (1.22.1)
116
- parser (3.1.2.0)
121
+ parallel (1.23.0)
122
+ parser (3.2.2.3)
117
123
  ast (~> 2.4.1)
118
- racc (1.6.2)
119
- rack (2.2.5)
120
- rack-test (2.0.2)
124
+ racc
125
+ racc (1.7.1)
126
+ rack (2.2.8)
127
+ rack-test (2.1.0)
121
128
  rack (>= 1.3)
122
- rails (7.0.4)
123
- actioncable (= 7.0.4)
124
- actionmailbox (= 7.0.4)
125
- actionmailer (= 7.0.4)
126
- actionpack (= 7.0.4)
127
- actiontext (= 7.0.4)
128
- actionview (= 7.0.4)
129
- activejob (= 7.0.4)
130
- activemodel (= 7.0.4)
131
- activerecord (= 7.0.4)
132
- activestorage (= 7.0.4)
133
- activesupport (= 7.0.4)
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)
134
141
  bundler (>= 1.15.0)
135
- railties (= 7.0.4)
136
- rails-dom-testing (2.0.3)
137
- activesupport (>= 4.2.0)
142
+ railties (= 7.0.8)
143
+ rails-dom-testing (2.2.0)
144
+ activesupport (>= 5.0.0)
145
+ minitest
138
146
  nokogiri (>= 1.6)
139
- rails-html-sanitizer (1.4.4)
140
- loofah (~> 2.19, >= 2.19.1)
141
- railties (7.0.4)
142
- actionpack (= 7.0.4)
143
- activesupport (= 7.0.4)
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)
144
153
  method_source
145
154
  rake (>= 12.2)
146
155
  thor (~> 1.0)
147
156
  zeitwerk (~> 2.5)
148
157
  rainbow (3.1.1)
149
158
  rake (13.0.6)
150
- regexp_parser (2.5.0)
151
- rexml (3.2.5)
152
- rubocop (1.32.0)
159
+ regexp_parser (2.8.1)
160
+ rexml (3.2.6)
161
+ rubocop (1.56.3)
162
+ base64 (~> 0.1.1)
153
163
  json (~> 2.3)
164
+ language_server-protocol (>= 3.17.0)
154
165
  parallel (~> 1.10)
155
- parser (>= 3.1.0.0)
166
+ parser (>= 3.2.2.3)
156
167
  rainbow (>= 2.2.2, < 4.0)
157
168
  regexp_parser (>= 1.8, < 3.0)
158
169
  rexml (>= 3.2.5, < 4.0)
159
- rubocop-ast (>= 1.19.1, < 2.0)
170
+ rubocop-ast (>= 1.28.1, < 2.0)
160
171
  ruby-progressbar (~> 1.7)
161
- unicode-display_width (>= 1.4.0, < 3.0)
162
- rubocop-ast (1.19.1)
163
- parser (>= 3.1.1.0)
164
- rubocop-performance (1.14.3)
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)
165
176
  rubocop (>= 1.7.0, < 2.0)
166
177
  rubocop-ast (>= 0.4.0)
167
- ruby-progressbar (1.11.0)
168
- sqlite3 (1.5.3)
178
+ ruby-progressbar (1.13.0)
179
+ ruby2_keywords (0.0.5)
180
+ sqlite3 (1.6.6)
169
181
  mini_portile2 (~> 2.8.0)
170
- standard (1.14.0)
171
- rubocop (= 1.32.0)
172
- rubocop-performance (= 1.14.3)
173
- thor (1.2.1)
174
- timeout (0.3.1)
175
- tzinfo (2.0.5)
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)
176
197
  concurrent-ruby (~> 1.0)
177
- unicode-display_width (2.2.0)
178
- websocket-driver (0.7.5)
198
+ unicode-display_width (2.4.2)
199
+ websocket-driver (0.7.6)
179
200
  websocket-extensions (>= 0.1.0)
180
201
  websocket-extensions (0.1.5)
181
- zeitwerk (2.6.6)
202
+ zeitwerk (2.6.11)
182
203
 
183
204
  PLATFORMS
184
205
  ruby
@@ -191,4 +212,4 @@ DEPENDENCIES
191
212
  standard (~> 1.3)
192
213
 
193
214
  BUNDLED WITH
194
- 2.3.16
215
+ 2.4.10
data/README.md CHANGED
@@ -2,9 +2,6 @@
2
2
 
3
3
  Generate JSON Schema compatible with OpenAPI 3 specs from Jbuilder files
4
4
 
5
- [![Tests](https://github.com/bullet-train-co/jbuilder-schema/actions/workflows/tests.yml/badge.svg)](https://github.com/bullet-train-co/jbuilder-schema/actions)
6
- [![Standard](https://github.com/bullet-train-co/jbuilder-schema/actions/workflows/standard.yml/badge.svg)](https://github.com/bullet-train-co/jbuilder-schema/actions)
7
-
8
5
  ## Installation
9
6
 
10
7
  In your Gemfile, put `gem "jbuilder-schema"` after Jbuilder:
@@ -21,14 +21,21 @@ class Jbuilder::Schema::Renderer
21
21
  def render(object = nil, title: nil, description: nil, assigns: nil, **options)
22
22
  @view_renderer.assign assigns if assigns
23
23
 
24
- json = original_render(object || options.dup, options.dup)
25
-
26
24
  partial_path = %i[to_partial_path_for_jbuilder_schema to_partial_path].map { object.public_send(_1) if object.respond_to?(_1) }.compact.first
27
- options.merge! partial: partial_path, object: object if partial_path
25
+ if partial_path
26
+ options[:partial] = partial_path
27
+ options[:object] = object
28
+ end
29
+
30
+ json = if partial_path
31
+ original_render(options.dup, options.dup)
32
+ else
33
+ original_render(object || options.dup, options.dup)
34
+ end
28
35
 
29
36
  options[:locals] ||= {}
30
37
  options[:locals].merge! @default_locals if @default_locals
31
- options[:locals][:__jbuilder_schema_options] = { json: json, object: object, title: title, description: description }
38
+ options[:locals][:__jbuilder_schema_options] = {json: json, object: object, title: title, description: description}
32
39
 
33
40
  @view_renderer.render(options)
34
41
  end
@@ -41,6 +41,7 @@ class Jbuilder::Schema
41
41
  end
42
42
 
43
43
  private
44
+
44
45
  def translate(key)
45
46
  I18n.t(key, scope: @scope ||= object&.class&.name&.underscore&.pluralize)
46
47
  end
@@ -58,10 +59,15 @@ class Jbuilder::Schema
58
59
  end
59
60
 
60
61
  def schema!
61
- if ([@attributes] + @attributes.each_value.grep(::Hash)).any? { _1[:type] == :array && _1.key?(:items) }
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
+ if [@attributes, *@attributes.first].select { |a| a.is_a?(::Hash) && a[:type] == :array && a.key?(:items) }.any?
62
68
  @attributes
63
69
  else
64
- _object(@attributes)
70
+ _object(@attributes, _required!(@attributes.keys))
65
71
  end.merge(example: @json).compact
66
72
  end
67
73
 
@@ -74,6 +80,8 @@ class Jbuilder::Schema
74
80
  # Detect `json.articles user.articles` to override Jbuilder's logic, which wouldn't hit `array!` and set a `type: :array, items: {"$ref": "#/components/schemas/article"}` ref.
75
81
  if block.nil? && keys.blank? && _is_collection?(value) && (value.empty? || value.all? { _is_active_model?(_1) })
76
82
  _set_value(key, _scope { _set_ref(key.to_s.singularize, array: true) })
83
+ elsif _partial_options?(options)
84
+ _set_value(key, _scope { _set_ref(options[:as].to_s, array: _is_collection?(value)) })
77
85
  else
78
86
  super(key, value, *keys, **options, &block)
79
87
  end
@@ -81,6 +89,7 @@ class Jbuilder::Schema
81
89
  ensure
82
90
  @configuration = old_configuration if old_configuration
83
91
  end
92
+
84
93
  alias_method :method_missing, :set! # TODO: Remove once Jbuilder passes keyword arguments along to `set!` in its `method_missing`.
85
94
 
86
95
  def array!(collection = [], *args, schema: nil, **options, &block)
@@ -128,13 +137,20 @@ class Jbuilder::Schema
128
137
  @schema_overrides = old_schema_overrides if overrides
129
138
  end
130
139
 
131
- def _object(attributes)
140
+ def _object(attributes, required)
132
141
  {
133
142
  type: :object,
134
143
  title: @configuration.title,
135
144
  description: @configuration.description,
136
- required: _required!(attributes.keys),
137
- properties: attributes
145
+ required: required,
146
+ properties: _nullify_non_required_types(attributes, required)
147
+ }
148
+ end
149
+
150
+ def _nullify_non_required_types(attributes, required)
151
+ attributes.transform_values! {
152
+ _1[:type] = [_1[:type], "null"] unless required.include?(attributes.key(_1))
153
+ _1
138
154
  }
139
155
  end
140
156
 
@@ -183,8 +199,8 @@ class Jbuilder::Schema
183
199
  options
184
200
  end
185
201
 
186
- def _primitive_type(type)
187
- case type
202
+ def _primitive_type(value)
203
+ case value
188
204
  when ::Array then :array
189
205
  when ::Float, ::BigDecimal then :number
190
206
  when true, false then :boolean
@@ -202,7 +218,7 @@ class Jbuilder::Schema
202
218
 
203
219
  def _required!(keys)
204
220
  presence_validated_attributes = @configuration.object&.class.try(:validators).to_a.flat_map { _1.attributes if _1.is_a?(::ActiveRecord::Validations::PresenceValidator) }
205
- keys & [_key(:id), *presence_validated_attributes.map { _key _1 }]
221
+ keys & [_key(:id), *presence_validated_attributes.flat_map { [_key(_1), _key("#{_1}_id")] }]
206
222
  end
207
223
 
208
224
  ###
@@ -218,7 +234,7 @@ class Jbuilder::Schema
218
234
  raise NullError.build(key) if current_value.nil?
219
235
 
220
236
  value = _scope { yield self }
221
- value = _object(value) unless value[:type] == :array || value.key?(:$ref)
237
+ value = _object(value, _required!(value.keys)) unless value[:type] == :array || value.key?(:$ref)
222
238
  _merge_values(current_value, value)
223
239
  end
224
240
  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.1.0"
4
+ JBUILDER_SCHEMA_VERSION = "2.3.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.1.0
4
+ version: 2.3.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-01-12 00:00:00.000000000 Z
11
+ date: 2023-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jbuilder
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  requirements: []
79
- rubygems_version: 3.4.1
79
+ rubygems_version: 3.4.10
80
80
  signing_key:
81
81
  specification_version: 4
82
82
  summary: Generate JSON Schema from Jbuilder files