blueprinter_schema 1.6.1 → 1.8.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 +4 -4
- data/README.md +37 -8
- data/lib/blueprinter_schema/generator.rb +16 -4
- data/lib/blueprinter_schema/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 51982caa98d5ae3d364c33024d21cd2acbe0a9350ffc9696ef3ff42ba0177427
|
|
4
|
+
data.tar.gz: 671bab5263c4c03a5ece305359a457da839b1363b547679de678159938a87bc4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b7dc67b5ebbf4e3b6aca4d8ece274d96f66cda67caff677fbfa4352de73a3374c6485f2cf0a8ad48d879b46f43e92151ccf6ae6b836d72f90c3d4c6a18bf0a59
|
|
7
|
+
data.tar.gz: dc3b0317a497218a9d4047031426e7c89714a9d55d15c195c490a843e4fe16c22b4af17455477f28939b184cad32b8fb9a4a9756168481061e25da644ed68668
|
data/README.md
CHANGED
|
@@ -20,6 +20,7 @@ class UserSerializer < Blueprinter::Base
|
|
|
20
20
|
field :email, type: :string, format: :email
|
|
21
21
|
field :role, type: :string, enum: ["user", "admin"]
|
|
22
22
|
field :tags, type: :array, items: { type: :string }
|
|
23
|
+
field :legacy_name, type: :string, deprecated: true
|
|
23
24
|
end
|
|
24
25
|
|
|
25
26
|
BlueprinterSchema.generate(serializer: UserSerializer)
|
|
@@ -50,9 +51,13 @@ BlueprinterSchema.generate(serializer: UserSerializer)
|
|
|
50
51
|
"tags" => {
|
|
51
52
|
"type" => "array",
|
|
52
53
|
"items" => { "type" => "string" }
|
|
54
|
+
},
|
|
55
|
+
"legacy_name" => {
|
|
56
|
+
"type" => "string",
|
|
57
|
+
"deprecated" => true
|
|
53
58
|
}
|
|
54
59
|
},
|
|
55
|
-
"required" => ["first_name", "last_name", "full_name", "email", "role", "tags"],
|
|
60
|
+
"required" => ["first_name", "last_name", "full_name", "email", "role", "tags", "legacy_name"],
|
|
56
61
|
"additionalProperties" => false
|
|
57
62
|
}
|
|
58
63
|
```
|
|
@@ -195,22 +200,46 @@ class RestrictionsSerializer < Blueprinter::Base
|
|
|
195
200
|
field :max_units
|
|
196
201
|
end
|
|
197
202
|
|
|
198
|
-
|
|
203
|
+
class Product
|
|
204
|
+
include ActiveModel::Model
|
|
205
|
+
include ActiveModel::Attributes
|
|
206
|
+
|
|
207
|
+
attribute :reference, :string
|
|
208
|
+
attribute :restrictions
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
class ProductSerializer < Blueprinter::Base
|
|
212
|
+
field :reference
|
|
213
|
+
association :restrictions, blueprint: RestrictionsSerializer, model: Restrictions
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
BlueprinterSchema.generate(serializer: ProductSerializer, model: Product)
|
|
199
217
|
```
|
|
200
218
|
|
|
201
219
|
```rb
|
|
202
220
|
{
|
|
203
221
|
"type" => "object",
|
|
204
|
-
"title" => "
|
|
222
|
+
"title" => "Product",
|
|
205
223
|
"properties" => {
|
|
206
|
-
"
|
|
207
|
-
"type" => "
|
|
224
|
+
"reference" => {
|
|
225
|
+
"type" => ["string", "null"]
|
|
208
226
|
},
|
|
209
|
-
"
|
|
210
|
-
"type" =>
|
|
227
|
+
"restrictions" => {
|
|
228
|
+
"type" => "object",
|
|
229
|
+
"title" => "Restrictions",
|
|
230
|
+
"properties" => {
|
|
231
|
+
"min_units" => {
|
|
232
|
+
"type" => "integer"
|
|
233
|
+
},
|
|
234
|
+
"max_units" => {
|
|
235
|
+
"type" => ["integer", "null"]
|
|
236
|
+
}
|
|
237
|
+
},
|
|
238
|
+
"required" => ["max_units", "min_units"],
|
|
239
|
+
"additionalProperties" => false
|
|
211
240
|
}
|
|
212
241
|
},
|
|
213
|
-
"required" => ["
|
|
242
|
+
"required" => ["reference", "restrictions"],
|
|
214
243
|
"additionalProperties" => false
|
|
215
244
|
}
|
|
216
245
|
```
|
|
@@ -100,6 +100,7 @@ module BlueprinterSchema
|
|
|
100
100
|
type_definition['items'] = options[:items].deep_stringify_keys if options[:items]
|
|
101
101
|
type_definition['format'] = options[:format] if options[:format]
|
|
102
102
|
type_definition['description'] = options[:description] if options[:description]
|
|
103
|
+
type_definition['deprecated'] = true if options[:deprecated]
|
|
103
104
|
type_definition
|
|
104
105
|
end
|
|
105
106
|
|
|
@@ -150,13 +151,24 @@ module BlueprinterSchema
|
|
|
150
151
|
return { 'type' => 'object' } unless blueprint_class
|
|
151
152
|
|
|
152
153
|
ar_association = @model.try(:reflect_on_association, association.name)
|
|
153
|
-
is_collection = ar_association ? ar_association.collection? : association.options[:collection]
|
|
154
|
-
|
|
155
154
|
view = association.options[:view] || :default
|
|
156
155
|
type = association.options[:optional] ? %w[object null] : 'object'
|
|
157
|
-
|
|
156
|
+
model = association_model(association, ar_association)
|
|
157
|
+
associated_schema = recursive_generate(blueprint_class, model, view, type:)
|
|
158
|
+
|
|
159
|
+
wrap_collection(associated_schema, association_collection?(association, ar_association))
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def association_model(association, ar_association)
|
|
163
|
+
ar_association&.klass || association.options[:model]
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
def association_collection?(association, ar_association)
|
|
167
|
+
ar_association ? ar_association.collection? : association.options[:collection]
|
|
168
|
+
end
|
|
158
169
|
|
|
159
|
-
|
|
170
|
+
def wrap_collection(schema, is_collection)
|
|
171
|
+
is_collection ? { 'type' => 'array', 'items' => schema } : schema
|
|
160
172
|
end
|
|
161
173
|
|
|
162
174
|
def recursive_generate(serializer, model, view, type:)
|