Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not fail when case insentive enabled while having a complex context object #191

Merged
merged 7 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
11 changes: 9 additions & 2 deletions lib/unleash/constraint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class Constraint
FALLBACK_VALIDATOR: ->(_context_v, _constraint_v){ false }
}.freeze

STRING_OPERATORS = [:STR_STARTS_WITH, :STR_ENDS_WITH, :STR_CONTAINS].freeze

LIST_OPERATORS = [:IN, :NOT_IN, :STR_STARTS_WITH, :STR_ENDS_WITH, :STR_CONTAINS].freeze

def initialize(context_name, operator, value = [], inverted: false, case_insensitive: false)
Expand Down Expand Up @@ -106,8 +108,13 @@ def matches_constraint?(context)
v = self.value.dup
context_value = context.get_by_name(self.context_name)
gastonfournier marked this conversation as resolved.
Show resolved Hide resolved

v.map!(&:upcase) if self.case_insensitive
context_value = context_value.upcase if self.case_insensitive
# always return false, if we are comparing a non string with a string operator:
return false if !context_value.is_a?(String) && STRING_OPERATORS.include?(self.operator)

if self.case_insensitive
Copy link
Member

Choose a reason for hiding this comment

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

The magic is now in the line above isn't it? This can change can get rolled back?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#192 maybe this simplifies the if? It's needed but it's also having side effects on the values which I'd like to prevent

v.map!(&:upcase)
context_value.upcase!
end

OPERATORS[self.operator].call(context_value, v)
end
Expand Down
8 changes: 7 additions & 1 deletion spec/unleash/constraint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,10 @@
session_id: 'verylongsesssionid',
remote_address: '127.0.0.1',
properties: {
env: 'development'
env: 'development',
complex: {
type: 'development'
}
}
}
context = Unleash::Context.new(context_params)
Expand All @@ -382,6 +385,9 @@

constraint = Unleash::Constraint.new('env', 'STR_CONTAINS', ['LOP'], case_insensitive: true)
expect(constraint.matches_context?(context)).to be true

constraint = Unleash::Constraint.new('complex', 'STR_CONTAINS', ['development'], case_insensitive: true)
expect(constraint.matches_context?(context)).to be false
end

it 'matches based on case insensitive property when context is uppercased' do
Expand Down
Loading