Skip to content

Commit 2ba6032

Browse files
chore: validate request option coercion correctness
1 parent eca7722 commit 2ba6032

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

lib/orb/internal/type/base_model.rb

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -390,15 +390,7 @@ def to_yaml(*a) = Orb::Internal::Type::Converter.dump(self.class, self).to_yaml(
390390
# Create a new instance of a model.
391391
#
392392
# @param data [Hash{Symbol=>Object}, self]
393-
def initialize(data = {})
394-
case Orb::Internal::Util.coerce_hash(data)
395-
in Hash => coerced
396-
@data = coerced
397-
else
398-
message = "Expected a #{Hash} or #{Orb::Internal::Type::BaseModel}, got #{data.inspect}"
399-
raise ArgumentError.new(message)
400-
end
401-
end
393+
def initialize(data = {}) = (@data = Orb::Internal::Util.coerce_hash!(data).to_h)
402394

403395
class << self
404396
# @api private

lib/orb/internal/type/request_parameters.rb

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,8 @@ def dump_request(params)
2828
state = {can_retry: true}
2929
case (dumped = dump(params, state: state))
3030
in Hash
31-
options = Orb::Internal::Util.coerce_hash(dumped[:request_options])
32-
request_options =
33-
case [options, state.fetch(:can_retry)]
34-
in [Hash | nil, false]
35-
{**options.to_h, max_retries: 0}
36-
else
37-
options
38-
end
31+
options = Orb::Internal::Util.coerce_hash!(dumped[:request_options]).to_h
32+
request_options = state.fetch(:can_retry) ? options : {**options, max_retries: 0}
3933
[dumped.except(:request_options), request_options]
4034
else
4135
[dumped, nil]

lib/orb/internal/util.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,22 @@ def coerce_hash(input)
128128
input.respond_to?(:to_h) ? input.to_h : input
129129
end
130130
end
131+
132+
# @api private
133+
#
134+
# @param input [Object]
135+
#
136+
# @raise [ArgumentError]
137+
# @return [Hash{Object=>Object}, nil]
138+
def coerce_hash!(input)
139+
case coerce_hash(input)
140+
in Hash | nil => coerced
141+
coerced
142+
else
143+
message = "Expected a #{Hash} or #{Orb::Internal::Type::BaseModel}, got #{data.inspect}"
144+
raise ArgumentError.new(message)
145+
end
146+
end
131147
end
132148

133149
class << self

rbi/orb/internal/util.rbi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ module Orb
4242
# @api private
4343
sig { params(input: T.anything).returns(T.any(T::Hash[T.anything, T.anything], T.anything)) }
4444
def coerce_hash(input); end
45+
46+
# @api private
47+
sig { params(input: T.anything).returns(T.nilable(T::Hash[T.anything, T.anything])) }
48+
def coerce_hash!(input); end
4549
end
4650

4751
class << self

sig/orb/internal/util.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ module Orb
1919

2020
def self?.coerce_hash: (top input) -> (::Hash[top, top] | top)
2121

22+
def self?.coerce_hash!: (top input) -> ::Hash[top, top]?
23+
2224
def self?.deep_merge_lr: (top lhs, top rhs, ?concat: bool) -> top
2325

2426
def self?.deep_merge: (

0 commit comments

Comments
 (0)