Class: JSON::Schema::Serializer::Walker

Inherits:
Object
  • Object
show all
Defined in:
lib/json/schema/serializer.rb

Constant Summary collapse

TimeWithZone =
defined?(ActiveSupport::TimeWithZone) ? ActiveSupport::TimeWithZone : nil

Class Method Summary collapse

Class Method Details

.detect_type(type, obj) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/json/schema/serializer.rb', line 41

def detect_type(type, obj)
  return type unless type.is_a?(Array)
  type = Set.new(type.map(&:to_s))

  case obj
  when nil
    case
    when type.include?("null")
      "null"
    else
      type.first
    end
  when DateTime, Date, Time, TimeWithZone
    case
    when type.include?("string")
      "string"
    when type.include?("number")
      "number"
    when type.include?("integer")
      "integer"
    else
      type.first
    end
  when String
    case
    when type.include?("string")
      "string"
    else
      type.first
    end
  when Integer
    case
    when type.include?("integer")
      "integer"
    when type.include?("number")
      "number"
    when type.include?("string")
      "string"
    when type.include?("boolean")
      "boolean"
    else
      type.first
    end
  when Float
    case
    when type.include?("number")
      "number"
    when type.include?("string")
      "string"
    when type.include?("integer")
      "integer"
    when type.include?("boolean")
      "boolean"
    else
      type.first
    end
  when true, false
    case
    when type.include?("boolean")
      "boolean"
    else
      type.first
    end
  when Array
    case
    when type.include?("array")
      "array"
    else
      type.first
    end
  else
    case
    when type.include?("object")
      "object"
    else
      type.first
    end
  end
end

.type_coerce(schema, type, format, obj, required, options) ⇒ Object



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/json/schema/serializer.rb', line 121

def type_coerce(schema, type, format, obj, required, options)
  return nil if !required && obj.nil?

  case type.to_s
  when "null"
    nil
  when "string"
    case obj
    when nil
      options[:null_through] ? nil : ""
    when DateTime, Date, Time, TimeWithZone
      case format.to_s
      when "date-time"
        obj.strftime("%FT%T%:z")
      when "date"
        obj.strftime("%F")
      when "time"
        obj.strftime("%T%:z")
      else
        obj.to_s
      end
    when Regexp
      obj.inspect.gsub(%r{^/|/[a-z]*$}, "")
    else
      obj.to_s
    end
  when "integer"
    case obj
    when true
      1
    when false
      0
    when nil
      options[:null_through] ? nil : 0
    when ""
      options[:empty_string_number_coerce_null] ? nil : 0
    else
      obj.to_i
    end
  when "number"
    case obj
    when true
      1.0
    when false
      0.0
    when nil
      options[:null_through] ? nil : 0.0
    when ""
      options[:empty_string_number_coerce_null] ? nil : 0.0
    else
      obj.to_f
    end
  when "boolean"
    if obj.nil? && options[:null_through]
      nil
    elsif options[:empty_string_boolean_coerce_null] && obj == ""
      nil
    elsif options[:false_values]
      !options[:false_values].include?(obj)
    elsif options[:no_boolean_coerce]
      obj == true
    else
      obj ? true : false
    end
  when "array"
    items_schema = try_hash(schema, :items)
    return options[:null_through] ? nil : [] if obj.nil? || !obj.respond_to?(:map)
    return options[:null_through] ? nil : [] if options[:guard_primitive_in_structure] && is_primitive?(obj)

    obj.map { |item| walk(items_schema, item, true, options) }
  when "object"
    return nil if obj.nil? && options[:null_through]
    return options[:null_through] ? nil : {} if options[:guard_primitive_in_structure] && is_primitive?(obj)

    properties_schema = try_hash(schema, :properties)
    additional_properties_schema = try_hash(schema, :additionalProperties)
    required_schema = Set.new(try_hash(schema, :required)&.map(&:to_s))
    input_key_transform = options[:schema_key_transform_for_input] # schema key -> input obj key
    output_key_transform = options[:schema_key_transform_for_output] # schema key -> out
    ret =
      properties_schema.map do |name, property_schema|
        input_key = input_key_transform ? input_key_transform.call(name.to_s) : name
        output_key = output_key_transform ? output_key_transform.call(name.to_s) : name.to_s
        [output_key, walk(property_schema, try_hash(obj, input_key), required_schema.include?(name.to_s), options)]
      end.to_h
    if additional_properties_schema
      not_additional_keys_array = properties_schema.keys.map(&:to_s)
      not_additional_keys = Set.new(input_key_transform ? not_additional_keys_array.map { |k| input_key_transform.call(k) } : not_additional_keys_array)
      additional_keys = obj.keys.reject { |key| not_additional_keys.include?(key.to_s) }
      ret.merge(
        additional_keys.map do |name|
          output_key = output_key_transform ? output_key_transform.call(name.to_s) : name.to_s
          [output_key, walk(additional_properties_schema, try_hash(obj, name), false, options)]
        end.to_h,
      )
    else
      ret
    end
  end
end

.walk(schema, obj, required, options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/json/schema/serializer.rb', line 21

def walk(schema, obj, required, options)
  type = try_hash(schema, :type)
  default = try_hash(schema, :default)
  format = try_hash(schema, :format)
  obj = default if obj.nil?

  if options[:inject_key]
    inject_key = try_hash(schema, options[:inject_key])
    injector = try_hash(options[:injectors], inject_key) if inject_key
    if injector
      if options[:inject_context]
        obj = injector.new(obj, options[:inject_context])
      else
        obj = injector.new(obj)
      end
    end
  end
  type_coerce(schema, detect_type(type, obj), format, obj, required, options)
end