Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/open_feature/sdk/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ def evaluate_flag(type:, flag_key:, default_value:, evaluation_context:)
)

if TYPE_CLASS_MAP[type].none? { |klass| resolution_details.value.is_a?(klass) }
resolution_details.value = default_value
resolution_details.error_code = Provider::ErrorCode::TYPE_MISMATCH
resolution_details.reason = Provider::Reason::ERROR
resolution_details.variant = nil
resolution_details = Provider::ResolutionDetails.new(
value: default_value,
error_code: Provider::ErrorCode::TYPE_MISMATCH,
reason: Provider::Reason::ERROR
)
Comment on lines +158 to +162
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

While creating a new ResolutionDetails object correctly avoids mutating the provider's response, this is also a good opportunity to provide a descriptive error_message. According to the OpenFeature specification, the evaluation details SHOULD contain an intelligible explanation of why an error occurred. Additionally, following repository guidelines, complex expressions like method chains should be extracted from string interpolations into local variables to improve readability.

          actual_type = resolution_details.value.class
          resolution_details = Provider::ResolutionDetails.new(
            value: default_value,
            error_code: Provider::ErrorCode::TYPE_MISMATCH,
            reason: Provider::Reason::ERROR,
            error_message: "Expected #{type}, but got #{actual_type}"
          )
References
  1. OpenFeature Specification Requirement 1.4.7: The evaluation details structure's error message field SHOULD contain a string containing an intelligible explanation of why an error occurred, if applicable.
  2. For readability, extract complex expressions like ternary operators from string interpolations into local variables.

end

EvaluationDetails.new(flag_key: flag_key, resolution_details: resolution_details)
Expand Down
16 changes: 16 additions & 0 deletions spec/open_feature/sdk/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,22 @@
expect(fetched.reason).to eq(OpenFeature::SDK::Provider::Reason::ERROR)
end
end

it "does not mutate the provider's ResolutionDetails on type mismatch" do
original = OpenFeature::SDK::Provider::ResolutionDetails.new(
value: "not_a_boolean",
reason: OpenFeature::SDK::Provider::Reason::STATIC
)
caching_provider = instance_double(OpenFeature::SDK::Provider::NoOpProvider)
allow(caching_provider).to receive(:fetch_boolean_value).and_return(original)

caching_client = described_class.new(provider: caching_provider)
caching_client.fetch_boolean_details(flag_key: "flag", default_value: false)

expect(original.value).to eq("not_a_boolean")
expect(original.error_code).to be_nil
expect(original.reason).to eq(OpenFeature::SDK::Provider::Reason::STATIC)
end
end
end

Expand Down
Loading