json-schema-serializer 1.7.1 → 2.0.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: 8520da63406ec92ce27f94214e6377eb0235fa22745c1dd9473fb597bfc2acdd
4
- data.tar.gz: 36de8e40ad358955d937e2b46141c0da13e9b7cff328a26194e6e52991c1b2cc
3
+ metadata.gz: 301107414cad9b1f2e8845d86633a36354e32b9ca7a45bb6611a1b42c4ece2ce
4
+ data.tar.gz: 7d2c5f7bb39e5d59278073a779abf70979d86870009c6195f764ee37da5c6c5f
5
5
  SHA512:
6
- metadata.gz: a6a9d1927d21adc4c7a499313b195e63465302018e4575a83c9095236c31df86eb1037e5d06618ad07fa0674c620c5cd2b58aad0aae2ae04884f3c0f9b6592c8
7
- data.tar.gz: f3d64406270a9b00f816106611cf1327ed5eab8bd9ec5472bb577fbfda8f0ef415404563fda4fd50008441388d03f655d82c0085f9dd8b5aba95438c1d50d9f7
6
+ metadata.gz: 4305c335b4a52abd9631bc11fb93b7268b7d0a235939f86be68dff2f3d8569df3b7908a33e4b9b45417c5f7b7b928a28b334d8feaad91e012079c3670ee512bd
7
+ data.tar.gz: f7a3ac83d661e4ddd546fae884600e61c1dce9119347ed0a93e3ee574c66c38bb4647a40f0c6f84e295804b3c620864e65c549e1b38e5f69429805fcc9a675a8
@@ -1,5 +1,10 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 2.0.0
4
+
5
+ - fix: Non-primitive (arrays, objects, etc.) "default" schemas and injections have strange results.
6
+ - If you were using a non-primitive (array, object, etc.) "default" schema with injection, it could be a breaking change!
7
+
3
8
  ## 1.7.0
4
9
 
5
10
  - add: `with_context!` api
@@ -11,7 +11,7 @@ module JSON
11
11
  end
12
12
 
13
13
  def serialize(data)
14
- Walker.walk(@schema, data, true, @options)
14
+ Walker.walk(@schema, data, true, false, @options)
15
15
  end
16
16
 
17
17
  DataWithContext = Struct.new(:data, :context, keyword_init: true)
@@ -34,11 +34,14 @@ module JSON
34
34
  class << self
35
35
  TimeWithZone = defined?(ActiveSupport::TimeWithZone) ? ActiveSupport::TimeWithZone : nil
36
36
 
37
- def walk(schema, obj, required, options)
37
+ def walk(schema, obj, required, using_default, options)
38
38
  type = try_hash(schema, :type)
39
39
  default = try_hash(schema, :default)
40
40
  format = try_hash(schema, :format)
41
- obj = default if obj.nil?
41
+ if obj.nil?
42
+ using_default = true
43
+ obj = default
44
+ end
42
45
 
43
46
  if options[:inject_key]
44
47
  inject_key = try_hash(schema, options[:inject_key])
@@ -46,8 +49,12 @@ module JSON
46
49
  if obj.instance_of?(JSON::Schema::Serializer::DataWithContext)
47
50
  options = options.merge(inject_context: obj.context)
48
51
  obj = obj.data
52
+ if obj.nil?
53
+ using_default = true
54
+ obj = default
55
+ end
49
56
  end
50
- if injector
57
+ if injector && !using_default
51
58
  if options[:inject_context]
52
59
  obj =
53
60
  if options[:inject_by_keyword]
@@ -65,7 +72,7 @@ module JSON
65
72
  end
66
73
  end
67
74
  end
68
- type_coerce(schema, detect_type(type, obj), format, obj, required, options)
75
+ type_coerce(schema, detect_type(type, obj), format, obj, required, using_default, options)
69
76
  end
70
77
 
71
78
  def detect_type(type, obj)
@@ -148,7 +155,7 @@ module JSON
148
155
  end
149
156
  end
150
157
 
151
- def type_coerce(schema, type, format, obj, required, options)
158
+ def type_coerce(schema, type, format, obj, required, using_default, options)
152
159
  return nil if !required && obj.nil?
153
160
 
154
161
  case type.to_s
@@ -217,7 +224,7 @@ module JSON
217
224
  return options[:null_through] ? nil : [] if obj.nil? || !obj.respond_to?(:map)
218
225
  return options[:null_through] ? nil : [] if options[:guard_primitive_in_structure] && is_primitive?(obj)
219
226
 
220
- obj.map { |item| walk(items_schema, item, true, options) }
227
+ obj.map { |item| walk(items_schema, item, true, using_default, options) }
221
228
  when "object"
222
229
  return nil if obj.nil? && options[:null_through]
223
230
  return options[:null_through] ? nil : {} if options[:guard_primitive_in_structure] && is_primitive?(obj)
@@ -231,7 +238,7 @@ module JSON
231
238
  properties_schema.map do |name, property_schema|
232
239
  input_key = input_key_transform ? input_key_transform.call(name.to_s) : name
233
240
  output_key = output_key_transform ? output_key_transform.call(name.to_s) : name.to_s
234
- [output_key, walk(property_schema, try_hash(obj, input_key), required_schema.include?(name.to_s), options)]
241
+ [output_key, walk(property_schema, try_hash(obj, input_key), required_schema.include?(name.to_s), using_default, options)]
235
242
  end.to_h
236
243
  if additional_properties_schema
237
244
  not_additional_keys_array = properties_schema.keys.map(&:to_s)
@@ -240,7 +247,7 @@ module JSON
240
247
  ret.merge(
241
248
  additional_keys.map do |name|
242
249
  output_key = output_key_transform ? output_key_transform.call(name.to_s) : name.to_s
243
- [output_key, walk(additional_properties_schema, try_hash(obj, name), false, options)]
250
+ [output_key, walk(additional_properties_schema, try_hash(obj, name), false, using_default, options)]
244
251
  end.to_h,
245
252
  )
246
253
  else
@@ -257,6 +264,9 @@ module JSON
257
264
  elsif obj.respond_to?(name)
258
265
  obj.send(name)
259
266
  end
267
+ rescue
268
+ p [name, obj]
269
+ raise
260
270
  end
261
271
 
262
272
  def is_primitive?(obj)
@@ -1,7 +1,7 @@
1
1
  module JSON
2
2
  class Schema
3
3
  class Serializer
4
- VERSION = "1.7.1".freeze
4
+ VERSION = "2.0.0".freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Narazaka
@@ -195,7 +195,7 @@ metadata:
195
195
  homepage_uri: https://github.com/Narazaka/json-schema-serializer
196
196
  source_code_uri: https://github.com/Narazaka/json-schema-serializer.git
197
197
  changelog_uri: https://github.com/Narazaka/json-schema-serializer/blob/master/CHANGELOG.md
198
- documentation_uri: https://www.rubydoc.info/gems/json-schema-serializer/1.7.1
198
+ documentation_uri: https://www.rubydoc.info/gems/json-schema-serializer/2.0.0
199
199
  post_install_message:
200
200
  rdoc_options: []
201
201
  require_paths: