lib/algolia/models/search/highlight_result.rb in algolia-3.0.0.beta.8 vs lib/algolia/models/search/highlight_result.rb in algolia-3.0.0.beta.9

- old
+ new

@@ -1,20 +1,20 @@ # Code generated by OpenAPI Generator (https://openapi-generator.tech), manual changes will be lost - read more on https://github.com/algolia/api-clients-automation. DO NOT EDIT. -require 'date' -require 'time' +require "date" +require "time" module Algolia module Search module HighlightResult class << self - # List of class defined in oneOf (OpenAPI v3) + # List of class defined in oneOf (OpenAPI v3) def openapi_one_of [ - :'Array<HighlightResultOption>', - :'Hash<String, HighlightResultOption>', - :HighlightResultOption + :"Array<HighlightResultOption>", + :"Hash<String, HighlightResultOption>", + :"HighlightResultOption" ] end # Builds the object # @param [Mixed] Data to be matched against the list of oneOf items @@ -26,72 +26,81 @@ # - No advanced validation of types in some cases (e.g. "x: { type: string }" will happily match { x: 123 }) # due to the way the deserialization is made in the base_object template (it just casts without verifying). # - TODO: scalar values are de facto behaving as if they were nullable. # - TODO: logging when debugging is set. openapi_one_of.each do |klass| - next if klass == :AnyType # "nullable: true" - - typed_data = find_and_cast_into_type(klass, data) - return typed_data if typed_data - rescue # rescue all errors so we keep iterating even if the current item lookup raises + begin + # "nullable: true" + next if klass == :AnyType + typed_data = find_and_cast_into_type(klass, data) + return typed_data if typed_data + # rescue all errors so we keep iterating even if the current item lookup raises + rescue + end end openapi_one_of.include?(:AnyType) ? data : nil end private SchemaMismatchError = Class.new(StandardError) - # NOTE: 'File' is missing here because in the regular case we get the data _after_ a call to JSON.parse. + # Note: 'File' is missing here because in the regular case we get the data _after_ a call to JSON.parse. def find_and_cast_into_type(klass, data) return if data.nil? case klass.to_s - when 'Boolean' + when "Boolean" return data if data.instance_of?(TrueClass) || data.instance_of?(FalseClass) - when 'Float' + when "Float" return data if data.instance_of?(Float) - when 'Integer' + when "Integer" return data if data.instance_of?(Integer) - when 'Time' + when "Time" return Time.parse(data) - when 'Date' + when "Date" return Date.parse(data) - when 'String' + when "String" return data if data.instance_of?(String) - when 'Object' # "type: object" + # "type: object" + when "Object" return data if data.instance_of?(Hash) - when /\AArray<(?<sub_type>.+)>\z/ # "type: array" + # "type: array" + when /\AArray<(?<sub_type>.+)>\z/ if data.instance_of?(Array) sub_type = Regexp.last_match[:sub_type] return data.map { |item| find_and_cast_into_type(sub_type, item) } end - when /\AHash<String, (?<sub_type>.+)>\z/ # "type: object" with "additionalProperties: { ... }" + # "type: object" with "additionalProperties: { ... }" + when /\AHash<String, (?<sub_type>.+)>\z/ if data.instance_of?(Hash) && data.keys.all? { |k| k.instance_of?(Symbol) || k.instance_of?(String) } sub_type = Regexp.last_match[:sub_type] return data.each_with_object({}) { |(k, v), hsh| hsh[k] = find_and_cast_into_type(sub_type, v) } end - else # model + # model + else const = Algolia::Search.const_get(klass) if const - if const.respond_to?(:openapi_one_of) # nested oneOf model + # nested oneOf model + if const.respond_to?(:openapi_one_of) model = const.build(data) else # raise if data contains keys that are not known to the model raise unless (data.keys - const.acceptable_attributes).empty? - model = const.build_from_hash(data) end return model if model end end - raise # if no match by now, raise + # if no match by now, raise + raise rescue raise SchemaMismatchError, "#{data} doesn't match the #{klass} type" end end end + end end