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

Conversation

gastonfournier
Copy link
Contributor

@gastonfournier gastonfournier commented Aug 28, 2024

About the changes

Closes #189

@rarruda
Copy link
Collaborator

rarruda commented Aug 28, 2024

For the sake of preventing regressions, would it be possible to implement the failing test in rspec?

@gastonfournier
Copy link
Contributor Author

For the sake of preventing regressions, would it be possible to implement the failing test in rspec?

Absolutely, I didn't know we had proper unit tests in this project, I was trying to find them and I was unaware they were under spec folder. Just asked a colleague and they point me in the right direction. I was expecting a test folder or files prefixed with test.

@rarruda
Copy link
Collaborator

rarruda commented Aug 28, 2024

I totally understand! No sweat. rspec uses spec, and i think minitest uses test by default. Just trying to nudge good practices here. 🙂

Unfortunately I'm too time constrained to be of more hands-on help at the moment. 🙁

@coveralls
Copy link

coveralls commented Aug 28, 2024

Pull Request Test Coverage Report for Build 10612694617

Details

  • 5 of 5 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.002%) to 97.156%

Totals Coverage Status
Change from base Build 10179193838: 0.002%
Covered Lines: 2562
Relevant Lines: 2637

💛 - Coveralls

@gastonfournier gastonfournier force-pushed the validate-string-before-upcase branch from fa51677 to 0076e2d Compare August 28, 2024 10:55
@gastonfournier gastonfournier self-assigned this Aug 28, 2024
Copy link
Contributor

@gardleopard gardleopard left a comment

Choose a reason for hiding this comment

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

LGTM

lib/unleash/constraint.rb Outdated Show resolved Hide resolved
@gastonfournier
Copy link
Contributor Author

We can validate what other SDKs are doing, we're aiming for consistency over correctness at the moment. I'll take some time to collect that data, because that idea also crossed my mind, and I believe the correct thing would be to return false if not a string...

This is what I found:

  1. JS client assumes it's a string and applies toString: https://github.com/Unleash/unleash-client-node/blob/929f14839d573aa6a61aff9074a80eb6167fd4c5/src/helpers.ts#L22 if you send an object this results in this string "[object Object]", it may cause some weird cases if you're constraint checks for the word object
  2. Java client does not allow non string properties: https://github.com/Unleash/unleash-client-java/blob/4188c4b723a995587510b9cd0e8fd7567d55e3db/src/main/java/io/getunleash/UnleashContext.java#L19
  3. PHP also supports string types only https://github.com/Unleash/unleash-client-php/blob/7353c74e1efde40634379282711bdb224df9a857/src/Configuration/UnleashContext.php#L65-L70

So, based on JS client which is the one accepting complex objects I think we can follow your suggestion and return false

@gastonfournier gastonfournier requested a review from rarruda August 28, 2024 14:14
# 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

Copy link
Member

@sighphyre sighphyre left a comment

Choose a reason for hiding this comment

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

Nice, LGTM

@sighphyre
Copy link
Member

We can validate what other SDKs are doing, we're aiming for consistency over correctness at the moment. I'll take some time to collect that data, because that idea also crossed my mind, and I believe the correct thing would be to return false if not a string...

This is what I found:

1. JS client assumes it's a string and applies toString: https://github.com/Unleash/unleash-client-node/blob/929f14839d573aa6a61aff9074a80eb6167fd4c5/src/helpers.ts#L22 if you send an object this results in this string `"[object Object]"`, it may cause some weird cases if you're constraint checks for the word `object`

2. Java client does not allow non string properties: https://github.com/Unleash/unleash-client-java/blob/4188c4b723a995587510b9cd0e8fd7567d55e3db/src/main/java/io/getunleash/UnleashContext.java#L19

3. PHP also supports string types only https://github.com/Unleash/unleash-client-php/blob/7353c74e1efde40634379282711bdb224df9a857/src/Configuration/UnleashContext.php#L65-L70

So, based on JS client which is the one accepting complex objects I think we can follow your suggestion and return false

Context properties are always strings, regardless of what the surface level APIs in the SDKs may suggest. It's not clear to see in the dynamically typed languages because they can often skip the contextual parsing required in the constraints that the statically typed languages have to do but that leads to... well... this problem.

In this case, this is a broken constraints because it can't be cast to the type that should be inferred from the local context provided by the constraint type so... +1 for return false

Copy link
Collaborator

@rarruda rarruda left a comment

Choose a reason for hiding this comment

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

This PR looks awesome! 🚀

@gastonfournier gastonfournier merged commit d1da09b into main Aug 29, 2024
36 checks passed
@gastonfournier gastonfournier deleted the validate-string-before-upcase branch August 29, 2024 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Custom Context accepts non string value but breaks when case-insensitive is on.
5 participants