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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3dd0981a3be1b7ee216a5b81d3ea24069cb937e291e2c2c7bb590d98db2e698
4
- data.tar.gz: 00211a94280dd1dfd0f4cb5a93ef56d1ae77fc17e312cf7c5c11799a7fe4fc74
3
+ metadata.gz: 0cafd732029c0e4f3cd09d13dd6809e3e68f599c194a24247774e7d18f6233ca
4
+ data.tar.gz: 1bf4791a3812e9f16bd3c8d7f8ed0bc2a91095130b91286fa75d3b72954b1482
5
5
  SHA512:
6
- metadata.gz: 16674fa11cdd54d134cdf32b2366e5c4be600fc0c455d2fa6f30ca2bb9c80daf6ea4e6008ab92ad6ae0fe29308da3aff739ff052bdeb6e1679d0604f5cc7cc69
7
- data.tar.gz: fbe7899df7288fc0520f8103ec846439fd1cecf04ee36dd3387923cfd59879eee63c5c2bfec8e6cca4db313393829e369df5311adf0c5f2608cbcd39c8dee444
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
- 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
@@ -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
- associated_schema = recursive_generate(blueprint_class, ar_association&.klass, view)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BlueprinterSchema
4
- VERSION = '1.2.0'
4
+ VERSION = '1.3.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.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - thisismydesign