jbuilder-schema 2.1.0 → 2.2.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: 8e69ae6e6726679110d135c34a461a2bfc176f967bfa1777b73989ae107fdabc
4
- data.tar.gz: 63fc234f84fe96195c6cc849bf26f4bea6d5e60808a0363e4fdddfe778497292
3
+ metadata.gz: 220162d597f3e157552d3ca783f9b4aba191f21ce2fb69172f24608d1439e840
4
+ data.tar.gz: 5ffd2a9d013ab15ff47d3d98dc4bcad5f79c735892f6733286d29032ee5d08f8
5
5
  SHA512:
6
- metadata.gz: 2fcc3260655ff07670c2a8be5175c801f29af27790e0675c680fc861bae3a2a48dcd30170cb5468e595ec647d58c9abe92d2177f7c5dc77acdb8569f8ab7997e
7
- data.tar.gz: ca344a595d3ed5c43c602e5d24ca154c4e0b9872388bd055422c957af93891be133618651e423f560ccc419b66c8b068828202f68929161d0790c4efbf71c8cb
6
+ metadata.gz: 86cedff5f98aa84fe8399361058ecff9cb29cbfe63f3b2b2d9894b238b87348692b7354effbef595e83e9b86c01ceeac7f0dd2499c3d20b66e6874653cfcec4f
7
+ data.tar.gz: a40c484ba860d6f50669c83535deb0047c908f47c82bb59f757752d4ec6e1d30f1872bcfa9cb4636cd8df6de6a5a73d6ac290397c9ca9c28c26df70a17d34725
data/Gemfile.lock CHANGED
@@ -1,95 +1,98 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jbuilder-schema (2.1.0)
4
+ jbuilder-schema (2.2.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.4.3)
12
+ actionpack (= 7.0.4.3)
13
+ activesupport (= 7.0.4.3)
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.4.3)
17
+ actionpack (= 7.0.4.3)
18
+ activejob (= 7.0.4.3)
19
+ activerecord (= 7.0.4.3)
20
+ activestorage (= 7.0.4.3)
21
+ activesupport (= 7.0.4.3)
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.4.3)
27
+ actionpack (= 7.0.4.3)
28
+ actionview (= 7.0.4.3)
29
+ activejob (= 7.0.4.3)
30
+ activesupport (= 7.0.4.3)
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)
36
+ actionpack (7.0.4.3)
37
+ actionview (= 7.0.4.3)
38
+ activesupport (= 7.0.4.3)
39
39
  rack (~> 2.0, >= 2.2.0)
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.4.3)
44
+ actionpack (= 7.0.4.3)
45
+ activerecord (= 7.0.4.3)
46
+ activestorage (= 7.0.4.3)
47
+ activesupport (= 7.0.4.3)
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.4.3)
51
+ activesupport (= 7.0.4.3)
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.4.3)
57
+ activesupport (= 7.0.4.3)
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.4.3)
60
+ activesupport (= 7.0.4.3)
61
+ activerecord (7.0.4.3)
62
+ activemodel (= 7.0.4.3)
63
+ activesupport (= 7.0.4.3)
64
+ activestorage (7.0.4.3)
65
+ actionpack (= 7.0.4.3)
66
+ activejob (= 7.0.4.3)
67
+ activerecord (= 7.0.4.3)
68
+ activesupport (= 7.0.4.3)
69
69
  marcel (~> 1.0)
70
70
  mini_mime (>= 1.1.0)
71
- activesupport (7.0.4)
71
+ activesupport (7.0.4.3)
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
77
  builder (3.2.4)
78
- concurrent-ruby (1.1.10)
78
+ concurrent-ruby (1.2.2)
79
79
  crass (1.0.6)
80
+ date (3.3.3)
80
81
  erubi (1.12.0)
81
- globalid (1.0.0)
82
+ globalid (1.1.0)
82
83
  activesupport (>= 5.0)
83
- i18n (1.12.0)
84
+ i18n (1.13.0)
84
85
  concurrent-ruby (~> 1.0)
85
86
  jbuilder (2.11.5)
86
87
  actionview (>= 5.0.0)
87
88
  activesupport (>= 5.0.0)
88
- json (2.6.2)
89
- loofah (2.19.1)
89
+ json (2.6.3)
90
+ language_server-protocol (3.17.0.3)
91
+ lint_roller (1.0.0)
92
+ loofah (2.20.0)
90
93
  crass (~> 1.0.2)
91
94
  nokogiri (>= 1.5.9)
92
- mail (2.8.0)
95
+ mail (2.8.1)
93
96
  mini_mime (>= 0.1.1)
94
97
  net-imap
95
98
  net-pop
@@ -97,10 +100,12 @@ GEM
97
100
  marcel (1.0.2)
98
101
  method_source (1.0.0)
99
102
  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_portile2 (2.8.2)
104
+ minitest (5.18.0)
105
+ mocha (2.0.2)
106
+ ruby2_keywords (>= 0.0.5)
107
+ net-imap (0.3.4)
108
+ date
104
109
  net-protocol
105
110
  net-pop (0.1.2)
106
111
  net-protocol
@@ -108,77 +113,86 @@ GEM
108
113
  timeout
109
114
  net-smtp (0.3.3)
110
115
  net-protocol
111
- nio4r (2.5.8)
112
- nokogiri (1.13.9)
116
+ nio4r (2.5.9)
117
+ nokogiri (1.14.3)
113
118
  mini_portile2 (~> 2.8.0)
114
119
  racc (~> 1.4)
115
- parallel (1.22.1)
116
- parser (3.1.2.0)
120
+ parallel (1.23.0)
121
+ parser (3.2.2.1)
117
122
  ast (~> 2.4.1)
118
123
  racc (1.6.2)
119
- rack (2.2.5)
120
- rack-test (2.0.2)
124
+ rack (2.2.7)
125
+ rack-test (2.1.0)
121
126
  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)
127
+ rails (7.0.4.3)
128
+ actioncable (= 7.0.4.3)
129
+ actionmailbox (= 7.0.4.3)
130
+ actionmailer (= 7.0.4.3)
131
+ actionpack (= 7.0.4.3)
132
+ actiontext (= 7.0.4.3)
133
+ actionview (= 7.0.4.3)
134
+ activejob (= 7.0.4.3)
135
+ activemodel (= 7.0.4.3)
136
+ activerecord (= 7.0.4.3)
137
+ activestorage (= 7.0.4.3)
138
+ activesupport (= 7.0.4.3)
134
139
  bundler (>= 1.15.0)
135
- railties (= 7.0.4)
140
+ railties (= 7.0.4.3)
136
141
  rails-dom-testing (2.0.3)
137
142
  activesupport (>= 4.2.0)
138
143
  nokogiri (>= 1.6)
139
- rails-html-sanitizer (1.4.4)
144
+ rails-html-sanitizer (1.5.0)
140
145
  loofah (~> 2.19, >= 2.19.1)
141
- railties (7.0.4)
142
- actionpack (= 7.0.4)
143
- activesupport (= 7.0.4)
146
+ railties (7.0.4.3)
147
+ actionpack (= 7.0.4.3)
148
+ activesupport (= 7.0.4.3)
144
149
  method_source
145
150
  rake (>= 12.2)
146
151
  thor (~> 1.0)
147
152
  zeitwerk (~> 2.5)
148
153
  rainbow (3.1.1)
149
154
  rake (13.0.6)
150
- regexp_parser (2.5.0)
155
+ regexp_parser (2.8.0)
151
156
  rexml (3.2.5)
152
- rubocop (1.32.0)
157
+ rubocop (1.50.2)
153
158
  json (~> 2.3)
154
159
  parallel (~> 1.10)
155
- parser (>= 3.1.0.0)
160
+ parser (>= 3.2.0.0)
156
161
  rainbow (>= 2.2.2, < 4.0)
157
162
  regexp_parser (>= 1.8, < 3.0)
158
163
  rexml (>= 3.2.5, < 4.0)
159
- rubocop-ast (>= 1.19.1, < 2.0)
164
+ rubocop-ast (>= 1.28.0, < 2.0)
160
165
  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)
166
+ unicode-display_width (>= 2.4.0, < 3.0)
167
+ rubocop-ast (1.28.1)
168
+ parser (>= 3.2.1.0)
169
+ rubocop-performance (1.16.0)
165
170
  rubocop (>= 1.7.0, < 2.0)
166
171
  rubocop-ast (>= 0.4.0)
167
- ruby-progressbar (1.11.0)
168
- sqlite3 (1.5.3)
172
+ ruby-progressbar (1.13.0)
173
+ ruby2_keywords (0.0.5)
174
+ sqlite3 (1.6.2)
169
175
  mini_portile2 (~> 2.8.0)
170
- standard (1.14.0)
171
- rubocop (= 1.32.0)
172
- rubocop-performance (= 1.14.3)
176
+ standard (1.28.2)
177
+ language_server-protocol (~> 3.17.0.2)
178
+ lint_roller (~> 1.0)
179
+ rubocop (~> 1.50.2)
180
+ standard-custom (~> 1.0.0)
181
+ standard-performance (~> 1.0.1)
182
+ standard-custom (1.0.0)
183
+ lint_roller (~> 1.0)
184
+ standard-performance (1.0.1)
185
+ lint_roller (~> 1.0)
186
+ rubocop-performance (~> 1.16.0)
173
187
  thor (1.2.1)
174
- timeout (0.3.1)
175
- tzinfo (2.0.5)
188
+ timeout (0.3.2)
189
+ tzinfo (2.0.6)
176
190
  concurrent-ruby (~> 1.0)
177
- unicode-display_width (2.2.0)
191
+ unicode-display_width (2.4.2)
178
192
  websocket-driver (0.7.5)
179
193
  websocket-extensions (>= 0.1.0)
180
194
  websocket-extensions (0.1.5)
181
- zeitwerk (2.6.6)
195
+ zeitwerk (2.6.8)
182
196
 
183
197
  PLATFORMS
184
198
  ruby
@@ -191,4 +205,4 @@ DEPENDENCIES
191
205
  standard (~> 1.3)
192
206
 
193
207
  BUNDLED WITH
194
- 2.3.16
208
+ 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
@@ -61,7 +62,7 @@ class Jbuilder::Schema
61
62
  if ([@attributes] + @attributes.each_value.grep(::Hash)).any? { _1[:type] == :array && _1.key?(:items) }
62
63
  @attributes
63
64
  else
64
- _object(@attributes)
65
+ _object(@attributes, _required!(@attributes.keys))
65
66
  end.merge(example: @json).compact
66
67
  end
67
68
 
@@ -128,16 +129,20 @@ class Jbuilder::Schema
128
129
  @schema_overrides = old_schema_overrides if overrides
129
130
  end
130
131
 
131
- def _object(attributes)
132
+ def _object(attributes, required)
132
133
  {
133
134
  type: :object,
134
135
  title: @configuration.title,
135
136
  description: @configuration.description,
136
- required: _required!(attributes.keys),
137
- properties: attributes
137
+ required: required,
138
+ properties: _nullify_non_required_types(attributes, required)
138
139
  }
139
140
  end
140
141
 
142
+ def _nullify_non_required_types(attributes, required)
143
+ attributes.transform_values! { _1[:type] = [_1[:type], "null"] unless required.include?(attributes.key(_1)); _1 }
144
+ end
145
+
141
146
  def _set_description(key, value)
142
147
  if !value.key?(:description) && @configuration.object
143
148
  value[:description] = @configuration.translate_field(key)
@@ -183,8 +188,8 @@ class Jbuilder::Schema
183
188
  options
184
189
  end
185
190
 
186
- def _primitive_type(type)
187
- case type
191
+ def _primitive_type(value)
192
+ case value
188
193
  when ::Array then :array
189
194
  when ::Float, ::BigDecimal then :number
190
195
  when true, false then :boolean
@@ -202,7 +207,7 @@ class Jbuilder::Schema
202
207
 
203
208
  def _required!(keys)
204
209
  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 }]
210
+ keys & [_key(:id), *presence_validated_attributes.flat_map { [_key(_1), _key("#{_1}_id")] }]
206
211
  end
207
212
 
208
213
  ###
@@ -218,7 +223,7 @@ class Jbuilder::Schema
218
223
  raise NullError.build(key) if current_value.nil?
219
224
 
220
225
  value = _scope { yield self }
221
- value = _object(value) unless value[:type] == :array || value.key?(:$ref)
226
+ value = _object(value, _required!(value.keys)) unless value[:type] == :array || value.key?(:$ref)
222
227
  _merge_values(current_value, value)
223
228
  end
224
229
  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.2.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.2.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-05-05 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