[Ruby][client] Handle enums (and other scalars) in oneOf and anyOf schemas (#17515)

* Handle enums in oneOf and anyOf schemas

* Update specs
This commit is contained in:
Armand Mégrot
2024-01-05 08:01:08 +01:00
committed by GitHub
parent 5c571b0e1f
commit 07a9257ee9
25 changed files with 578 additions and 10 deletions

View File

@@ -28,6 +28,7 @@ require 'petstore/models/capitalization'
require 'petstore/models/category'
require 'petstore/models/class_model'
require 'petstore/models/client'
require 'petstore/models/cow'
require 'petstore/models/deprecated_object'
require 'petstore/models/enum_arrays'
require 'petstore/models/enum_class'
@@ -41,6 +42,7 @@ require 'petstore/models/format_test'
require 'petstore/models/has_only_read_only'
require 'petstore/models/health_check_result'
require 'petstore/models/list'
require 'petstore/models/mamal_with_enum'
require 'petstore/models/mammal'
require 'petstore/models/mammal_anyof'
require 'petstore/models/mammal_without_discriminator'

View File

@@ -0,0 +1,40 @@
=begin
#OpenAPI Petstore
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
The version of the OpenAPI document: 1.0.0
Generated by: https://openapi-generator.tech
OpenAPI Generator version: 7.3.0-SNAPSHOT
=end
require 'date'
require 'time'
module Petstore
class Cow
BLACK_AND_WHITE_COW = "BlackAndWhiteCow".freeze
BROWN_COW = "BrownCow".freeze
def self.all_vars
@all_vars ||= [BLACK_AND_WHITE_COW, BROWN_COW].freeze
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def self.build_from_hash(value)
new.build_from_hash(value)
end
# Builds the enum from string
# @param [String] The enum value in the form of the string
# @return [String] The enum value
def build_from_hash(value)
return value if Cow.all_vars.include?(value)
raise "Invalid ENUM value #{value} for class #Cow"
end
end
end

View File

@@ -0,0 +1,104 @@
=begin
#OpenAPI Petstore
#This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
The version of the OpenAPI document: 1.0.0
Generated by: https://openapi-generator.tech
OpenAPI Generator version: 7.3.0-SNAPSHOT
=end
require 'date'
require 'time'
module Petstore
module MamalWithEnum
class << self
# List of class defined in oneOf (OpenAPI v3)
def openapi_one_of
[
:'Cow'
]
end
# Builds the object
# @param [Mixed] Data to be matched against the list of oneOf items
# @return [Object] Returns the model or the data itself
def build(data)
# Go through the list of oneOf items and attempt to identify the appropriate one.
# Note:
# - We do not attempt to check whether exactly one item matches.
# - 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|
begin
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
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.
def find_and_cast_into_type(klass, data)
return if data.nil?
case klass.to_s
when 'Boolean'
return data if data.instance_of?(TrueClass) || data.instance_of?(FalseClass)
when 'Float'
return data if data.instance_of?(Float)
when 'Integer'
return data if data.instance_of?(Integer)
when 'Time'
return Time.parse(data)
when 'Date'
return Date.parse(data)
when 'String'
return data if data.instance_of?(String)
when 'Object' # "type: object"
return data if data.instance_of?(Hash)
when /\AArray<(?<sub_type>.+)>\z/ # "type: array"
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: { ... }"
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
const = Petstore.const_get(klass)
if const
if const.respond_to?(:openapi_one_of) # nested oneOf model
model = const.build(data)
return model if model
else
# raise if data contains keys that are not known to the model
raise if const.respond_to?(:acceptable_attributes) && !(data.keys - const.acceptable_attributes).empty?
model = const.build_from_hash(data)
return model if model
end
end
end
raise # if no match by now, raise
rescue
raise SchemaMismatchError, "#{data} doesn't match the #{klass} type"
end
end
end
end

View File

@@ -87,7 +87,7 @@ module Petstore
return model if model
else
# raise if data contains keys that are not known to the model
raise unless (data.keys - const.acceptable_attributes).empty?
raise if const.respond_to?(:acceptable_attributes) && !(data.keys - const.acceptable_attributes).empty?
model = const.build_from_hash(data)
return model if model
end

View File

@@ -88,7 +88,7 @@ module Petstore
return model if model
else
# raise if data contains keys that are not known to the model
raise unless (data.keys - const.acceptable_attributes).empty?
raise if const.respond_to?(:acceptable_attributes) && !(data.keys - const.acceptable_attributes).empty?
model = const.build_from_hash(data)
return model if model
end