blueprinter_schema 1.2.0 → 1.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 +4 -4
- data/README.md +18 -2
- data/lib/blueprinter_schema/generator.rb +9 -6
- data/lib/blueprinter_schema/version.rb +1 -1
- data/lib/blueprinter_schema.rb +4 -3
- 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: 0cafd732029c0e4f3cd09d13dd6809e3e68f599c194a24247774e7d18f6233ca
|
|
4
|
+
data.tar.gz: 1bf4791a3812e9f16bd3c8d7f8ed0bc2a91095130b91286fa75d3b72954b1482
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 02577f5a4da542def621be3544ff1c82d4509addfc2b8b7a1264a9a6e7d079943ae75c0574f160354f48ebf3c6b1481eeb09d899a35fb3ed83b169e25671a482
|
|
7
|
+
data.tar.gz: 6690022f62b1a2ad817a362ae2322300e0013c22494b1bb2efe195478c0a6036063c3c31ae422ab92b1745591cacdf4d6aba0356e386df32d1419f28c8ecb717
|
data/README.md
CHANGED
|
@@ -100,6 +100,7 @@ class UserSerializer < Blueprinter::Base
|
|
|
100
100
|
|
|
101
101
|
association :addresses, blueprint: AddressSerializer, collection: true
|
|
102
102
|
association :profile, blueprint: ProfileSerializer
|
|
103
|
+
association :account, blueprint: AccountSerializer, optional: true
|
|
103
104
|
end
|
|
104
105
|
|
|
105
106
|
class AddressSerializer < Blueprinter::Base
|
|
@@ -110,6 +111,10 @@ class ProfileSerializer < Blueprinter::Base
|
|
|
110
111
|
field :public, type: :boolean
|
|
111
112
|
end
|
|
112
113
|
|
|
114
|
+
class AccountSerializer < Blueprinter::Base
|
|
115
|
+
field :name, type: :string
|
|
116
|
+
end
|
|
117
|
+
|
|
113
118
|
BlueprinterSchema.generate(serializer: UserSerializer)
|
|
114
119
|
```
|
|
115
120
|
|
|
@@ -142,6 +147,16 @@ BlueprinterSchema.generate(serializer: UserSerializer)
|
|
|
142
147
|
},
|
|
143
148
|
"required" => ["public"],
|
|
144
149
|
"additionalProperties" => false
|
|
150
|
+
},
|
|
151
|
+
"account" => {
|
|
152
|
+
"type" => ["object", "null"],
|
|
153
|
+
"properties" => {
|
|
154
|
+
"name" => {
|
|
155
|
+
"type" => "string"
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
"required" => ["name"],
|
|
159
|
+
"additionalProperties" => false
|
|
145
160
|
}
|
|
146
161
|
},
|
|
147
162
|
"required" => ["email", "addresses", "profile"],
|
|
@@ -156,8 +171,9 @@ BlueprinterSchema.generate(
|
|
|
156
171
|
serializer:,
|
|
157
172
|
model: nil,
|
|
158
173
|
include_conditional_fields: true, # Whether or not to include conditional fields from the serializer
|
|
159
|
-
|
|
160
|
-
view: :default # The blueprint view to use
|
|
174
|
+
fallback_definition: {}, # Type when no DB column or type definition is found. E.g. { 'type' => 'object' }
|
|
175
|
+
view: :default, # The blueprint view to use
|
|
176
|
+
type: "object" # Root type
|
|
161
177
|
)
|
|
162
178
|
```
|
|
163
179
|
|
|
@@ -5,17 +5,18 @@ module BlueprinterSchema
|
|
|
5
5
|
|
|
6
6
|
# rubocop:disable Metrics/ClassLength
|
|
7
7
|
class Generator
|
|
8
|
-
def initialize(serializer:, model:, skip_conditional_fields:, fallback_definition:, view:)
|
|
8
|
+
def initialize(serializer:, model:, skip_conditional_fields:, fallback_definition:, view:, type:) # rubocop:disable Metrics/ParameterLists
|
|
9
9
|
@serializer = serializer
|
|
10
10
|
@model = model
|
|
11
11
|
@skip_conditional_fields = skip_conditional_fields
|
|
12
12
|
@fallback_definition = fallback_definition
|
|
13
13
|
@view = view
|
|
14
|
+
@type = type
|
|
14
15
|
end
|
|
15
16
|
|
|
16
17
|
def generate
|
|
17
18
|
schema = {
|
|
18
|
-
'type' =>
|
|
19
|
+
'type' => @type,
|
|
19
20
|
'properties' => build_properties,
|
|
20
21
|
'required' => build_required_fields,
|
|
21
22
|
'additionalProperties' => false
|
|
@@ -123,7 +124,7 @@ module BlueprinterSchema
|
|
|
123
124
|
type
|
|
124
125
|
end
|
|
125
126
|
|
|
126
|
-
def association_to_json_schema(association)
|
|
127
|
+
def association_to_json_schema(association) # rubocop:disable Metrics/CyclomaticComplexity
|
|
127
128
|
blueprint_class = association.options[:blueprint]
|
|
128
129
|
|
|
129
130
|
return { 'type' => 'object' } unless blueprint_class
|
|
@@ -132,18 +133,20 @@ module BlueprinterSchema
|
|
|
132
133
|
is_collection = ar_association ? ar_association.collection? : association.options[:collection]
|
|
133
134
|
|
|
134
135
|
view = association.options[:view] || :default
|
|
135
|
-
|
|
136
|
+
type = association.options[:optional] ? %w[object null] : 'object'
|
|
137
|
+
associated_schema = recursive_generate(blueprint_class, ar_association&.klass, view, type:)
|
|
136
138
|
|
|
137
139
|
is_collection ? { 'type' => 'array', 'items' => associated_schema } : associated_schema
|
|
138
140
|
end
|
|
139
141
|
|
|
140
|
-
def recursive_generate(serializer, model, view)
|
|
142
|
+
def recursive_generate(serializer, model, view, type:)
|
|
141
143
|
BlueprinterSchema.generate(
|
|
142
144
|
serializer:,
|
|
143
145
|
model:,
|
|
144
146
|
skip_conditional_fields: @skip_conditional_fields,
|
|
145
147
|
fallback_definition: @fallback_definition,
|
|
146
|
-
view
|
|
148
|
+
view:,
|
|
149
|
+
type:
|
|
147
150
|
)
|
|
148
151
|
end
|
|
149
152
|
end
|
data/lib/blueprinter_schema.rb
CHANGED
|
@@ -4,13 +4,14 @@ require_relative 'blueprinter_schema/version'
|
|
|
4
4
|
require_relative 'blueprinter_schema/generator'
|
|
5
5
|
|
|
6
6
|
module BlueprinterSchema
|
|
7
|
-
def self.generate(
|
|
7
|
+
def self.generate( # rubocop:disable Metrics/ParameterLists
|
|
8
8
|
serializer:,
|
|
9
9
|
model: nil,
|
|
10
10
|
skip_conditional_fields: false,
|
|
11
11
|
fallback_definition: {},
|
|
12
|
-
view: :default
|
|
12
|
+
view: :default,
|
|
13
|
+
type: 'object'
|
|
13
14
|
)
|
|
14
|
-
Generator.new(serializer:, model:, skip_conditional_fields:, fallback_definition:, view:).generate
|
|
15
|
+
Generator.new(serializer:, model:, skip_conditional_fields:, fallback_definition:, view:, type:).generate
|
|
15
16
|
end
|
|
16
17
|
end
|