Skip to content

Commit

Permalink
Support multiple data types for Swagger Model attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
tapan-sh committed Jan 17, 2025
1 parent 51d02ff commit 297fa4e
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion lib/class_kit/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ def attr_accessor_type(
name,
type: nil,
collection_type: nil,
one_of: nil,
allow_nil: true,
default: nil,
auto_init: false,
Expand All @@ -17,6 +18,7 @@ def attr_accessor_type(
attributes[name] = {
name: name,
type: type,
one_of: one_of,
collection_type: collection_type,
allow_nil: allow_nil,
default: default,
Expand Down Expand Up @@ -54,7 +56,16 @@ def attr_accessor_type(
raise ClassKit::Exceptions::InvalidAttributeValueError, "Attribute: #{name}, must not be nil."
end

# check if the value being set is not of the specified type and should attempt to parse the value
if !cka[:one_of].nil? && !value.nil? && ([Hash, String].any? { |t| value.is_a?(t) })
type = cka[:one_of].detect { |t| value.is_a?(t) }
value = ClassKit::ValueHelper.instance.parse(type: type, value: value)
end

puts '-' * 50
puts "#{cka}"
puts "Value: #{value}"
puts '-' * 50

if !cka[:type].nil? && !value.nil? && (cka[:type] == :bool || !value.is_a?(cka[:type]))
begin
value = ClassKit::ValueHelper.instance.parse(type: cka[:type], value: value)
Expand Down

0 comments on commit 297fa4e

Please sign in to comment.