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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3dd0981a3be1b7ee216a5b81d3ea24069cb937e291e2c2c7bb590d98db2e698
4
- data.tar.gz: 00211a94280dd1dfd0f4cb5a93ef56d1ae77fc17e312cf7c5c11799a7fe4fc74
3
+ metadata.gz: 820f85b8d22780549e1913cd8fa26f219c425c14746296b466b7966efc58bb2b
4
+ data.tar.gz: c013ef3d11fe9c214a21e0400ba2f0c66aa2ca2be0fb62e9feafe0fed0b55e46
5
5
  SHA512:
6
- metadata.gz: 16674fa11cdd54d134cdf32b2366e5c4be600fc0c455d2fa6f30ca2bb9c80daf6ea4e6008ab92ad6ae0fe29308da3aff739ff052bdeb6e1679d0604f5cc7cc69
7
- data.tar.gz: fbe7899df7288fc0520f8103ec846439fd1cecf04ee36dd3387923cfd59879eee63c5c2bfec8e6cca4db313393829e369df5311adf0c5f2608cbcd39c8dee444
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
- fallback_type: {}, # Type when no DB column or type definition is found. E.g. { 'type' => 'object' }
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' => 'object',
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.map(&:to_s)
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
- associated_schema = recursive_generate(blueprint_class, ar_association&.klass, view)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlueprinterSchema
4
- VERSION = '1.2.0'
4
+ VERSION = '1.4.0'
5
5
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: blueprinter_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thisismydesign