jbuilder-schema 2.6.7 → 2.6.8
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 +4 -4
- data/README.md +221 -115
- data/lib/jbuilder/schema/template.rb +32 -21
- data/lib/jbuilder/schema/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3f75cc13caa4465745a822b1bd1d7e66899fa1bb2387457d1e638ba1df9e360a
|
|
4
|
+
data.tar.gz: 6ec03326e02493379e9e97e2c9d50eac1f1bfb4a4cc340c1e3941774cddde6c6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9704440704b61701c9b3852a97b86b4dbed7e4456e8fbf37934cab3978690d41fe4fd9c7fa786ef1479da9c3b8be53e67cc0a05516073481f21eaabeb864921a
|
|
7
|
+
data.tar.gz: b875004bb14f11ba6eed4a8f1ca77675bb7b2484e5c97676943fb21fe1023aedb8b263476191517b45f89ab7c15bd2c2f1a1e97e1a2082bc002f7734454ed310
|
data/README.md
CHANGED
|
@@ -1,62 +1,58 @@
|
|
|
1
1
|
# Jbuilder::Schema
|
|
2
2
|
|
|
3
|
-
Generate JSON
|
|
3
|
+
Easily Generate JSON Schemas from Jbuilder Templates for OpenAPI 3.1
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Quick Start
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
### Installation
|
|
8
|
+
|
|
9
|
+
Add this to your Gemfile:
|
|
8
10
|
|
|
9
11
|
gem "jbuilder"
|
|
10
12
|
gem "jbuilder-schema"
|
|
11
13
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
$ bundle
|
|
15
|
-
|
|
16
|
-
Or install it yourself as:
|
|
14
|
+
Then, run `bundle` or install it manually using `gem install jbuilder-schema`.
|
|
17
15
|
|
|
18
|
-
|
|
16
|
+
### Generating Schemas
|
|
19
17
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
Wherever you want to generate schemas, call `Jbuilder::Schema.yaml` or `Jbuilder::Schema.json`:
|
|
18
|
+
Use `Jbuilder::Schema.yaml` or `Jbuilder::Schema.json` to create schemas. For example:
|
|
23
19
|
|
|
24
20
|
```ruby
|
|
25
21
|
Jbuilder::Schema.yaml(@article, title: 'Article', description: 'Article in the blog', locals: { current_user: @user })
|
|
26
22
|
```
|
|
27
23
|
|
|
28
|
-
|
|
24
|
+
This will render a Jbuilder template (e.g., `articles/_article.json.jbuilder`) and make `@article` available in the partial. You can also pass additional locals.
|
|
29
25
|
|
|
30
|
-
|
|
26
|
+
## Contents
|
|
31
27
|
|
|
32
|
-
|
|
28
|
+
- [Advanced Usage](#advanced-usage)
|
|
29
|
+
- [Rendering Specific Directories](#rendering-specific-directories)
|
|
30
|
+
- [Rendering Templates](#rendering-templates)
|
|
31
|
+
- [Output](#output)
|
|
32
|
+
- [Handling Arrays and Objects](#handling-arrays-and-objects)
|
|
33
|
+
- [Nested Partials and Arrays](#nested-partials-and-arrays)
|
|
34
|
+
- [Customization](#customization)
|
|
35
|
+
- [Titles & Descriptions](#titles--descriptions)
|
|
36
|
+
- [Configuration](#configuration)
|
|
37
|
+
- [Integration with RSwag](#integration-with-rswag)
|
|
38
|
+
- [Contributing](#contributing)
|
|
39
|
+
- [License](#license)
|
|
40
|
+
- [Sponsor](#open-source-development-sponsored-by)
|
|
33
41
|
|
|
34
|
-
|
|
42
|
+
### Advanced Usage
|
|
35
43
|
|
|
36
|
-
|
|
44
|
+
#### Rendering Specific Directories
|
|
37
45
|
|
|
38
|
-
If
|
|
46
|
+
If your Jbuilder templates are in a specific directory, use `Jbuilder::Schema.renderer`:
|
|
39
47
|
|
|
40
48
|
```ruby
|
|
41
49
|
jbuilder = Jbuilder::Schema.renderer('app/views/api/v1', locals: { current_user: @user })
|
|
42
50
|
jbuilder.yaml @article, title: 'Article', description: 'Article in the blog'
|
|
43
51
|
```
|
|
44
52
|
|
|
45
|
-
|
|
53
|
+
#### Rendering Templates
|
|
46
54
|
|
|
47
|
-
|
|
48
|
-
Jbuilder::Schema.yaml(partial: 'api/v1/articles/article', locals: { article: @article, current_user: @user }, title: 'Article', description: 'Article in the blog')
|
|
49
|
-
```
|
|
50
|
-
|
|
51
|
-
### Rendering a template
|
|
52
|
-
|
|
53
|
-
If you're rendering a template like `app/views/articles/index.jbuilder`:
|
|
54
|
-
|
|
55
|
-
```ruby
|
|
56
|
-
json.articles @articles, :id, :title
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
You'll need to pass the relative template path in `template:` and any needed instance variables in `assigns:` like so:
|
|
55
|
+
For templates like `app/views/articles/index.jbuilder`, specify the template path and variables:
|
|
60
56
|
|
|
61
57
|
```ruby
|
|
62
58
|
Jbuilder::Schema.yaml(template: "articles/index", assigns: { articles: Article.first(3) })
|
|
@@ -64,15 +60,16 @@ Jbuilder::Schema.yaml(template: "articles/index", assigns: { articles: Article.f
|
|
|
64
60
|
|
|
65
61
|
### Output
|
|
66
62
|
|
|
67
|
-
Jbuilder::Schema automatically sets `description`, `type`, and `required`
|
|
63
|
+
Jbuilder::Schema automatically sets `description`, `type`, and `required` fields in the JSON Schema. You can *[customize](#customization)* these using the `schema:` hash.
|
|
68
64
|
|
|
69
|
-
|
|
65
|
+
#### Example
|
|
70
66
|
|
|
71
67
|
```ruby
|
|
68
|
+
# _article.json.jbuilder
|
|
72
69
|
json.extract! article, :id, :title, :body, :created_at
|
|
73
70
|
```
|
|
74
71
|
|
|
75
|
-
|
|
72
|
+
#### Result
|
|
76
73
|
|
|
77
74
|
```yaml
|
|
78
75
|
type: object
|
|
@@ -84,136 +81,245 @@ required:
|
|
|
84
81
|
- body
|
|
85
82
|
properties:
|
|
86
83
|
id:
|
|
87
|
-
description: ID of an article
|
|
88
84
|
type: integer
|
|
85
|
+
description: Article ID
|
|
89
86
|
title:
|
|
90
|
-
description: Title of an article
|
|
91
87
|
type: string
|
|
88
|
+
description: Article Title
|
|
92
89
|
body:
|
|
93
|
-
description: Contents of an article
|
|
94
90
|
type: string
|
|
91
|
+
description: Article Contents
|
|
95
92
|
created_at:
|
|
96
|
-
|
|
97
|
-
|
|
93
|
+
type:
|
|
94
|
+
- string
|
|
95
|
+
- "null"
|
|
98
96
|
format: date-time
|
|
97
|
+
description: Timestamp when article was created
|
|
99
98
|
```
|
|
100
99
|
|
|
101
|
-
###
|
|
100
|
+
### Handling Arrays and Objects
|
|
101
|
+
|
|
102
|
+
The gem efficiently handles arrays and objects, including nested structures. Arrays with a single element type are straightforwardly represented, while arrays with mixed types use the `anyOf` keyword for versatility.
|
|
102
103
|
|
|
103
|
-
|
|
104
|
+
Support of various object types like `Hash`, `Struct`, `OpenStruct`, and `ActiveRecord::Base` is also integrated. It simplifies object schemas by setting only `type` and `properties`.
|
|
104
105
|
|
|
105
|
-
|
|
106
|
+
#### Example
|
|
106
107
|
|
|
107
108
|
```ruby
|
|
108
|
-
json.
|
|
109
|
-
json.title article.title, schema: { minLength: 5, maxLength: 20 }
|
|
110
|
-
json.body article.body, schema: { type: :text, maxLength: 500 }
|
|
111
|
-
json.created_at article.created_at.strftime('%d/%m/%Y'), schema: { format: :date, pattern: /^(3[01]|[12][0-9]|0[1-9])\/(1[0-2]|0[1-9])\/[0-9]{4}$/ }
|
|
109
|
+
json.custom_array [1, article.user, 2, "Text", [3.14, 25.44], 5.33, [3, "Another text", {a: 4, b: "One more text"}], {c: 5, d: "And another"}, {e: 6, f: {g: 7, h: "Last Text"}}]
|
|
112
110
|
```
|
|
113
111
|
|
|
114
|
-
|
|
112
|
+
#### Result
|
|
115
113
|
|
|
116
114
|
```yaml
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
115
|
+
properties:
|
|
116
|
+
custom_array:
|
|
117
|
+
type:
|
|
118
|
+
- array
|
|
119
|
+
- "null"
|
|
120
|
+
minContains: 0
|
|
121
|
+
contains:
|
|
122
|
+
anyOf:
|
|
123
|
+
- type: integer
|
|
124
|
+
- type: object
|
|
125
|
+
# ... ActiveRecord object properties ...
|
|
126
|
+
- type: string
|
|
127
|
+
- type: array
|
|
128
|
+
# All arrays are merged in one so all possible values of arrays are in one place
|
|
129
|
+
minContains: 0
|
|
130
|
+
contains:
|
|
131
|
+
anyOf:
|
|
132
|
+
- type: number
|
|
133
|
+
- type: integer
|
|
134
|
+
- type: string
|
|
135
|
+
- type: object
|
|
136
|
+
properties:
|
|
137
|
+
a:
|
|
138
|
+
type: integer
|
|
139
|
+
# ... description ...
|
|
140
|
+
b:
|
|
141
|
+
type: integer
|
|
142
|
+
# ... description ...
|
|
143
|
+
- type: number
|
|
144
|
+
- type: object
|
|
145
|
+
properties:
|
|
146
|
+
c:
|
|
147
|
+
type: integer
|
|
148
|
+
# ... description ...
|
|
149
|
+
d:
|
|
150
|
+
type: integer
|
|
151
|
+
# ... description ...
|
|
152
|
+
- type: object
|
|
153
|
+
properties:
|
|
154
|
+
e:
|
|
155
|
+
type: integer
|
|
156
|
+
# ... description ...
|
|
157
|
+
f:
|
|
158
|
+
type: object
|
|
159
|
+
properties:
|
|
160
|
+
h:
|
|
161
|
+
type: integer
|
|
162
|
+
# ... description ...
|
|
163
|
+
g:
|
|
164
|
+
type: string
|
|
165
|
+
# ... description ...
|
|
166
|
+
description: Very weird custom array
|
|
136
167
|
```
|
|
137
168
|
|
|
138
|
-
|
|
169
|
+
Each schema is unique, ensuring no duplication. Description fields are nested under parent field names for clarity.
|
|
139
170
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
```ruby
|
|
143
|
-
json.extract! user, :id, :name, :email, schema: {id: {type: :string}, email: {type: :email, pattern: /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/}}
|
|
144
|
-
```
|
|
171
|
+
### Nested Partials and Arrays
|
|
145
172
|
|
|
146
|
-
|
|
173
|
+
Nested partials and arrays will most commonly produce reference to the related schema component.
|
|
174
|
+
Only if block with partial includes other fields, the inline object will be generated.
|
|
147
175
|
|
|
148
|
-
|
|
176
|
+
#### Example
|
|
149
177
|
|
|
150
178
|
```ruby
|
|
151
|
-
json.
|
|
152
|
-
json.
|
|
153
|
-
|
|
179
|
+
json.author do
|
|
180
|
+
json.partial! "api/v1/users/user", user: article.user
|
|
181
|
+
end
|
|
182
|
+
json.comments do
|
|
183
|
+
json.array! article.comments, partial: "api/v1/articles/comments/comment", as: :article_comment
|
|
184
|
+
end
|
|
185
|
+
json.ratings do
|
|
186
|
+
json.array! article.ratings, schema: {object: article.ratings.first, title: "Rating", description: "Article Rating"} do |rating|
|
|
187
|
+
json.partial! "api/v1/shared/id", resource: rating
|
|
188
|
+
json.extract! rating, :value
|
|
189
|
+
end
|
|
154
190
|
end
|
|
155
191
|
```
|
|
156
192
|
|
|
157
|
-
|
|
193
|
+
#### Result
|
|
158
194
|
|
|
159
|
-
|
|
195
|
+
```yaml
|
|
196
|
+
# ... object description ...
|
|
197
|
+
properties:
|
|
198
|
+
author:
|
|
199
|
+
type: object
|
|
200
|
+
allOf:
|
|
201
|
+
- "$ref": "#/components/schemas/User"
|
|
202
|
+
description: User
|
|
203
|
+
comments:
|
|
204
|
+
type: array
|
|
205
|
+
items:
|
|
206
|
+
- "$ref": "#/components/schemas/Comment"
|
|
207
|
+
description: Comments
|
|
208
|
+
ratings:
|
|
209
|
+
type: array
|
|
210
|
+
items:
|
|
211
|
+
type: object
|
|
212
|
+
title: Rating
|
|
213
|
+
description: Article Rating
|
|
214
|
+
required:
|
|
215
|
+
- id
|
|
216
|
+
- value
|
|
217
|
+
properties:
|
|
218
|
+
id:
|
|
219
|
+
type: integer
|
|
220
|
+
description: Rating ID
|
|
221
|
+
public_id:
|
|
222
|
+
type:
|
|
223
|
+
- string
|
|
224
|
+
- "null"
|
|
225
|
+
description: Rating Public ID
|
|
226
|
+
value:
|
|
227
|
+
type: integer
|
|
228
|
+
description: Rating Value
|
|
229
|
+
description: Article Ratings
|
|
230
|
+
```
|
|
160
231
|
|
|
161
|
-
|
|
232
|
+
Reference names are taken from `:as` option or first of the `locals:`.
|
|
162
233
|
|
|
163
|
-
|
|
234
|
+
The path to component schemas can be configured with `components_path` variable, which defaults to `components/schemas`. See *[Configuration](#configuration)* for more info.
|
|
235
|
+
|
|
236
|
+
### Customization
|
|
237
|
+
|
|
238
|
+
Customize individual or multiple fields at once using the `schema:` attribute.
|
|
239
|
+
For nested objects and collections, use the `schema: {object: <nested_object>}` format.
|
|
240
|
+
|
|
241
|
+
#### Example
|
|
164
242
|
|
|
165
243
|
```ruby
|
|
166
|
-
json.
|
|
167
|
-
|
|
168
|
-
|
|
244
|
+
json.id article.id, schema: { type: :number, description: "Custom ID description" }
|
|
245
|
+
json.title article.title, schema: { minLength: 5, maxLength: 20 }
|
|
246
|
+
json.contents article.body, schema: { type: :text, maxLength: 500, required: true }
|
|
247
|
+
json.created_at article.created_at.strftime('%d/%m/%Y'), schema: { format: :date, pattern: /^(3[01]|[12][0-9]|0[1-9])\/(1[0-2]|0[1-9])\/[0-9]{4}$/ }
|
|
169
248
|
|
|
170
|
-
json.
|
|
171
|
-
json.
|
|
249
|
+
json.author schema: {object: article.user, title: "Article Author", description: "The person who wrote the article", required: true} do
|
|
250
|
+
json.extract! article.user, :id, :name, :email, schema: {id: {type: :string}, email: {type: :email, pattern: /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/}}
|
|
172
251
|
end
|
|
173
252
|
```
|
|
174
253
|
|
|
175
|
-
|
|
254
|
+
#### Result
|
|
176
255
|
|
|
177
256
|
```yaml
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
257
|
+
type: object
|
|
258
|
+
title: Article
|
|
259
|
+
description: Article in the blog
|
|
260
|
+
required:
|
|
261
|
+
- id
|
|
262
|
+
- title
|
|
263
|
+
- contents
|
|
264
|
+
- author
|
|
265
|
+
properties:
|
|
266
|
+
id:
|
|
267
|
+
type: number
|
|
268
|
+
description: Custom ID description
|
|
269
|
+
title:
|
|
270
|
+
type: string
|
|
271
|
+
minLength: 5
|
|
272
|
+
maxLength: 20
|
|
273
|
+
description: Title of an article
|
|
274
|
+
contents:
|
|
275
|
+
type: string
|
|
276
|
+
maxLength: 500
|
|
277
|
+
description: Contents of an article
|
|
278
|
+
created_at:
|
|
279
|
+
type:
|
|
280
|
+
- string
|
|
281
|
+
- "null"
|
|
282
|
+
format: date
|
|
283
|
+
pattern: "^(3[01]|[12][0-9]|0[1-9])\/(1[0-2]|0[1-9])\/[0-9]{4}$"
|
|
284
|
+
description: Timestamp when article was created
|
|
285
|
+
author:
|
|
286
|
+
type: object
|
|
287
|
+
title: Article Author
|
|
288
|
+
description: The person who wrote the article
|
|
289
|
+
required:
|
|
290
|
+
- id
|
|
291
|
+
- name
|
|
292
|
+
- email
|
|
293
|
+
properties:
|
|
294
|
+
id:
|
|
295
|
+
type: string
|
|
296
|
+
description: User ID
|
|
297
|
+
name:
|
|
298
|
+
type: string
|
|
299
|
+
description: User Name
|
|
300
|
+
email:
|
|
301
|
+
type: email
|
|
302
|
+
pattern: "^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"
|
|
303
|
+
description: User Email
|
|
185
304
|
```
|
|
186
305
|
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
### Titles & Descriptions
|
|
306
|
+
#### Titles & Descriptions
|
|
190
307
|
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
Titles and descriptions for the models are supposed to be found in locale files under `<underscored_plural_model_name>.<title_name>` and `<underscored_plural_model_name>.<description_name>`, for example:
|
|
308
|
+
Set custom titles and descriptions directly or through locale files. For models, use `<underscored_plural_model_name>.<title_name>` and for fields, use `<underscored_plural_model_name>.fields.<field_name>.<description_name>` in locale files:
|
|
194
309
|
|
|
195
310
|
```yaml
|
|
196
311
|
en:
|
|
197
312
|
articles:
|
|
198
313
|
title: Article
|
|
199
314
|
description: The main object on the blog
|
|
200
|
-
```
|
|
201
|
-
|
|
202
|
-
Descriptions for the fields are supposed to be found in locale files under `<underscored_plural_model_name>.fields.<field_name>.<description_name>`, for example:
|
|
203
|
-
|
|
204
|
-
```yaml
|
|
205
|
-
en:
|
|
206
|
-
articles:
|
|
207
315
|
fields:
|
|
208
316
|
title:
|
|
209
317
|
description: The title of an article
|
|
210
318
|
```
|
|
211
319
|
|
|
212
|
-
`<title_name>` and `<description_name>` can be configured (see *[Configuration](#configuration)*), it defaults to `title` and `description`.
|
|
213
|
-
|
|
214
320
|
### Configuration
|
|
215
321
|
|
|
216
|
-
|
|
322
|
+
Configure Jbuilder::Schema in `config/initializers/jbuilder_schema.rb`:
|
|
217
323
|
|
|
218
324
|
```ruby
|
|
219
325
|
Jbuilder::Schema.configure do |config|
|
|
@@ -223,9 +329,9 @@ Jbuilder::Schema.configure do |config|
|
|
|
223
329
|
end
|
|
224
330
|
```
|
|
225
331
|
|
|
226
|
-
### RSwag
|
|
332
|
+
### Integration with RSwag
|
|
227
333
|
|
|
228
|
-
|
|
334
|
+
Use `yaml`/`json` methods in your `swagger_helper.rb` for Swagger documentation:
|
|
229
335
|
|
|
230
336
|
```ruby
|
|
231
337
|
RSpec.configure do |config|
|
|
@@ -246,11 +352,11 @@ end
|
|
|
246
352
|
|
|
247
353
|
## Contributing
|
|
248
354
|
|
|
249
|
-
|
|
355
|
+
Contributions are welcome! Report bugs and submit pull requests on [GitHub](https://github.com/bullet-train-co/jbuilder-schema).
|
|
250
356
|
|
|
251
357
|
## License
|
|
252
358
|
|
|
253
|
-
|
|
359
|
+
This gem is open source under the [MIT License](https://opensource.org/licenses/MIT).
|
|
254
360
|
|
|
255
361
|
## Open-source development sponsored by:
|
|
256
362
|
|
|
@@ -37,7 +37,11 @@ class Jbuilder::Schema
|
|
|
37
37
|
super || translate(Jbuilder::Schema.description_name)
|
|
38
38
|
end
|
|
39
39
|
|
|
40
|
-
def
|
|
40
|
+
def translate_title(key)
|
|
41
|
+
translate("fields.#{key}.#{Jbuilder::Schema.title_name}")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def translate_description(key)
|
|
41
45
|
translate("fields.#{key}.#{Jbuilder::Schema.description_name}")
|
|
42
46
|
end
|
|
43
47
|
|
|
@@ -84,6 +88,7 @@ class Jbuilder::Schema
|
|
|
84
88
|
|
|
85
89
|
def set!(key, value = BLANK, *args, schema: nil, **options, &block)
|
|
86
90
|
old_configuration, @configuration = @configuration, Configuration.build(**schema) if schema&.dig(:object)
|
|
91
|
+
_required << key if schema&.delete(:required) == true
|
|
87
92
|
@within_block = _within_block?(&block)
|
|
88
93
|
|
|
89
94
|
_with_schema_overrides(key => schema) do
|
|
@@ -188,16 +193,18 @@ class Jbuilder::Schema
|
|
|
188
193
|
}
|
|
189
194
|
end
|
|
190
195
|
|
|
191
|
-
def
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
196
|
+
def _set_title_and_description(key, value)
|
|
197
|
+
overrides = @schema_overrides&.dig(key)&.to_h || {}
|
|
198
|
+
return unless overrides.any? || @configuration.object
|
|
199
|
+
|
|
200
|
+
value[:title] ||= overrides[:title] if overrides&.key?(:title)
|
|
201
|
+
value[:description] ||= overrides[:description] || @configuration.translate_description(key)
|
|
195
202
|
end
|
|
196
203
|
|
|
197
|
-
def _set_ref(
|
|
198
|
-
ref = {"$ref": "#/#{::Jbuilder::Schema.components_path}/#{
|
|
204
|
+
def _set_ref(object, **options)
|
|
205
|
+
ref = {"$ref": "#/#{::Jbuilder::Schema.components_path}/#{object.split("/").last}"}
|
|
199
206
|
|
|
200
|
-
if array
|
|
207
|
+
if options[:array]
|
|
201
208
|
_attributes.merge! type: :array, items: ref
|
|
202
209
|
else
|
|
203
210
|
_attributes.merge! type: :object, allOf: [ref]
|
|
@@ -209,6 +216,10 @@ class Jbuilder::Schema
|
|
|
209
216
|
@attributes
|
|
210
217
|
end
|
|
211
218
|
|
|
219
|
+
def _required
|
|
220
|
+
@required_keys ||= []
|
|
221
|
+
end
|
|
222
|
+
|
|
212
223
|
FORMATS = {::DateTime => "date-time", ::ActiveSupport::TimeWithZone => "date-time", ::Date => "date", ::Time => "time"}
|
|
213
224
|
|
|
214
225
|
def _schema(key, value, **options)
|
|
@@ -248,7 +259,7 @@ class Jbuilder::Schema
|
|
|
248
259
|
options[:enum] = defined_enum.keys
|
|
249
260
|
end
|
|
250
261
|
|
|
251
|
-
|
|
262
|
+
_set_title_and_description key, options unless within_array
|
|
252
263
|
options
|
|
253
264
|
end
|
|
254
265
|
|
|
@@ -281,7 +292,7 @@ class Jbuilder::Schema
|
|
|
281
292
|
def _set_value(key, value)
|
|
282
293
|
value = _value(value)
|
|
283
294
|
value = _schema(key, value) unless value.is_a?(::Hash) && (value.key?(:type) || value.key?(:allOf)) # rubocop:disable Style/UnlessLogicalOperators
|
|
284
|
-
|
|
295
|
+
_set_title_and_description(key, value)
|
|
285
296
|
super
|
|
286
297
|
end
|
|
287
298
|
|
|
@@ -296,10 +307,20 @@ class Jbuilder::Schema
|
|
|
296
307
|
end
|
|
297
308
|
|
|
298
309
|
def _required!(keys)
|
|
299
|
-
presence_validated_attributes = @configuration.object&.class.try(:validators).to_a.flat_map { _1.attributes if _1.is_a?(::ActiveRecord::Validations::PresenceValidator) }
|
|
310
|
+
presence_validated_attributes = @configuration.object&.class.try(:validators).to_a.flat_map { _1.attributes if _1.is_a?(::ActiveRecord::Validations::PresenceValidator) } + _required
|
|
300
311
|
keys & [_key(:id), *presence_validated_attributes.flat_map { [_key(_1), _key("#{_1}_id")] }]
|
|
301
312
|
end
|
|
302
313
|
|
|
314
|
+
def _within_block?(&block)
|
|
315
|
+
block.present? && _one_line?(block.source)
|
|
316
|
+
end
|
|
317
|
+
|
|
318
|
+
def _one_line?(text)
|
|
319
|
+
text = text.gsub("{", " do\n").gsub("}", "\nend").tr(";", "\n")
|
|
320
|
+
lines = text.lines[1..-2].reject { |line| line.strip.empty? || !line.strip.start_with?("json.") }
|
|
321
|
+
lines.size == 1
|
|
322
|
+
end
|
|
323
|
+
|
|
303
324
|
###
|
|
304
325
|
# Jbuilder methods
|
|
305
326
|
###
|
|
@@ -317,15 +338,5 @@ class Jbuilder::Schema
|
|
|
317
338
|
|
|
318
339
|
_merge_values(current_value, value)
|
|
319
340
|
end
|
|
320
|
-
|
|
321
|
-
def _within_block?(&block)
|
|
322
|
-
block.present? && _one_line?(block.source)
|
|
323
|
-
end
|
|
324
|
-
|
|
325
|
-
def _one_line?(text)
|
|
326
|
-
text = text.gsub("{", " do\n").gsub("}", "\nend").tr(";", "\n")
|
|
327
|
-
lines = text.lines[1..-2].reject { |line| line.strip.empty? || line.strip.start_with?("#") }
|
|
328
|
-
lines.size == 1
|
|
329
|
-
end
|
|
330
341
|
end
|
|
331
342
|
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.6.
|
|
4
|
+
JBUILDER_SCHEMA_VERSION = "2.6.8"
|
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.
|
|
4
|
+
version: 2.6.8
|
|
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-12-
|
|
11
|
+
date: 2023-12-06 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: jbuilder
|