blueprinter_schema 1.2.0 → 1.4.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 +19 -3
- data/lib/blueprinter_schema/generator.rb +21 -7
- 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: 820f85b8d22780549e1913cd8fa26f219c425c14746296b466b7966efc58bb2b
|
|
4
|
+
data.tar.gz: c013ef3d11fe9c214a21e0400ba2f0c66aa2ca2be0fb62e9feafe0fed0b55e46
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6075da5a95d77f0f0c2142f7d1a9f94e067c863f4f1aa6c042945e5793367633147005824bbd12fff807e10568c653ebd734cebcb9b67d261710178053f61410
|
|
7
|
+
data.tar.gz: 7b89616467a6d82202bc2b105f4e948392a4c1fd841c56a6dcb4c66130897ad49d94b2140362576297ca59d615cb37b1eaf2f340eabf01b2702a05790f3808a2
|
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,9 +147,19 @@ 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
|
-
"required" => ["email", "addresses", "profile"],
|
|
162
|
+
"required" => ["email", "addresses", "profile", "account"],
|
|
148
163
|
"additionalProperties" => false
|
|
149
164
|
}
|
|
150
165
|
```
|
|
@@ -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
|
|
@@ -57,10 +58,21 @@ module BlueprinterSchema
|
|
|
57
58
|
end
|
|
58
59
|
|
|
59
60
|
def build_required_fields
|
|
61
|
+
(required_field_names + required_association_names).map(&:to_s)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def required_field_names
|
|
60
65
|
fields
|
|
61
66
|
.reject { |_, field| field.options[:exclude_if_nil] }
|
|
62
67
|
.reject { |_, field| skip_field?(field) }
|
|
63
|
-
.keys
|
|
68
|
+
.keys
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def required_association_names
|
|
72
|
+
associations
|
|
73
|
+
.reject { |_, association| association.options[:exclude_if_nil] }
|
|
74
|
+
.reject { |_, association| skip_field?(association) }
|
|
75
|
+
.keys
|
|
64
76
|
end
|
|
65
77
|
|
|
66
78
|
def field_to_json_schema(field)
|
|
@@ -123,7 +135,7 @@ module BlueprinterSchema
|
|
|
123
135
|
type
|
|
124
136
|
end
|
|
125
137
|
|
|
126
|
-
def association_to_json_schema(association)
|
|
138
|
+
def association_to_json_schema(association) # rubocop:disable Metrics/CyclomaticComplexity
|
|
127
139
|
blueprint_class = association.options[:blueprint]
|
|
128
140
|
|
|
129
141
|
return { 'type' => 'object' } unless blueprint_class
|
|
@@ -132,18 +144,20 @@ module BlueprinterSchema
|
|
|
132
144
|
is_collection = ar_association ? ar_association.collection? : association.options[:collection]
|
|
133
145
|
|
|
134
146
|
view = association.options[:view] || :default
|
|
135
|
-
|
|
147
|
+
type = association.options[:optional] ? %w[object null] : 'object'
|
|
148
|
+
associated_schema = recursive_generate(blueprint_class, ar_association&.klass, view, type:)
|
|
136
149
|
|
|
137
150
|
is_collection ? { 'type' => 'array', 'items' => associated_schema } : associated_schema
|
|
138
151
|
end
|
|
139
152
|
|
|
140
|
-
def recursive_generate(serializer, model, view)
|
|
153
|
+
def recursive_generate(serializer, model, view, type:)
|
|
141
154
|
BlueprinterSchema.generate(
|
|
142
155
|
serializer:,
|
|
143
156
|
model:,
|
|
144
157
|
skip_conditional_fields: @skip_conditional_fields,
|
|
145
158
|
fallback_definition: @fallback_definition,
|
|
146
|
-
view
|
|
159
|
+
view:,
|
|
160
|
+
type:
|
|
147
161
|
)
|
|
148
162
|
end
|
|
149
163
|
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
|