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

Change CORSConfig to builder pattern #4827

Closed
wants to merge 8 commits into from
Closed

Conversation

qwbarch
Copy link
Contributor

@qwbarch qwbarch commented May 15, 2021

Fixes #1717. I wasn't sure how to use Method's generics so I left it as a String for now.

@hamnis
Copy link
Contributor

hamnis commented May 15, 2021

Hello, and thanks for your contribution.

Needs a sbt scalafmtAll

I would expect a builder to have a build, method or something that produces the final result. Since this builder is immutable, i guess that is not really a problem.

Now we just really have the same as we did, but not able to do equality checks and other useful operations.

Perhaps we should keep the old type around, but make it a non-case class, but with equals, hashCode and toString implemented. Not sure.

@qwbarch
Copy link
Contributor Author

qwbarch commented May 16, 2021

I wasn't sure what to do with allowedOrigins: String => Boolean, so I left it out for equals, and hashCode.
How do I go about checking for equality and calculating a function's hash?

@rossabaker
Copy link
Member

Yeah, we call this "the builder pattern", but maybe "the contraband pattern" is more accurate. I don't think it needs build or result method. We're just trying to promote binary compatibility moving forward.

Function equality is hard. We'd like two functions A => B to be equal if and only if for each A the two functions each return the same B. But our A is String, with infinite inhabitants, so we can't possibly test this! The default definition is reference equality, and it's really the best we can do. I'd just use == and ##. The alternative is to redesign that function into some more constrained AST, but that's not really worth it to recover equality.

@rossabaker
Copy link
Member

rossabaker commented May 16, 2021

We should cherry-pick this to series/0.22, if it's ready before we release it.

@rossabaker rossabaker added breaking Change breaks compatibility (binary or source) module:server labels May 16, 2021
@rossabaker rossabaker added this to the 0.22 milestone May 16, 2021
@qwbarch qwbarch changed the base branch from main to series/0.22 May 16, 2021 08:02
@qwbarch
Copy link
Contributor Author

qwbarch commented May 16, 2021

I cherry-picked my latest commit and brought it over to series/0.22, and then changed this PR's base to that branch as well.
This doesn't look quite right though, did I do it wrong?

@qwbarch
Copy link
Contributor Author

qwbarch commented May 16, 2021

Whoops, I just realized this isn't even merged properly when I was cherry-picking. Going to revert it and be more careful next time

@qwbarch qwbarch changed the base branch from series/0.22 to main May 16, 2021 08:18
@qwbarch
Copy link
Contributor Author

qwbarch commented May 16, 2021

I've also realized I can't switch the source branch, only the target branch (which I don't think makes a difference now that I think about it).
I've switched the target branch back to main for now since it brought in a ton of participants, I don't want to end up giving everyone a notification email 😅

@rossabaker
Copy link
Member

Don't worry about the targeting. I can cherry-pick it so it shows up as you and gets to the right place. 😄

rossabaker
rossabaker previously approved these changes May 17, 2021
@rossabaker rossabaker dismissed their stale review May 17, 2021 04:46

2.12 compilation issue

@rossabaker
Copy link
Member

Looks like that Objects.hash doesn't work on primitives on Scala 2.12 due to an implicit conversion.

I can dig up an example of MurmurHash3 if we want to do it the way it's generated for Scala case classes. Or there are a few examples in our codebase.

@qwbarch
Copy link
Contributor Author

qwbarch commented May 17, 2021

Ah that's unfortunate. I'll look into MurmurHash3 from the other examples

@qwbarch
Copy link
Contributor Author

qwbarch commented May 17, 2021

Looks like that Objects.hash doesn't work on primitives on Scala 2.12 due to an implicit conversion.

I'm curious though, where does the error show up? I want to avoid running into these issues myself but there's no compiler error nor is there a failed test.

Edit: Nevermind I think I know what you meant, since there's an implicit conversion, an incorrect hash is generated I think right?

@rossabaker
Copy link
Member

since there's an implicit conversion, an incorrect hash is generated I think right?

It hit this error in Scala 2.12. Something that was fine in 2.13 and 3:

[error] /home/runner/work/http4s/http4s/server/src/main/scala/org/http4s/server/middleware/CORS.scala:102:7: the result type of an implicit conversion must be more specific than Object
[error]       anyOrigin,
[error]       ^
[error] /home/runner/work/http4s/http4s/server/src/main/scala/org/http4s/server/middleware/CORS.scala:103:7: the result type of an implicit conversion must be more specific than Object
[error]       allowCredentials,
[error]       ^
[error] /home/runner/work/http4s/http4s/server/src/main/scala/org/http4s/server/middleware/CORS.scala:105:7: the result type of an implicit conversion must be more specific than Object
[error]       anyMethod,
[error]       

Copy link
Member

@rossabaker rossabaker left a comment

Choose a reason for hiding this comment

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

LGTM. I'm going to cherry-pick it all back to series/0.22, because there's no CE work to backport.

rossabaker pushed a commit that referenced this pull request May 18, 2021
@rossabaker rossabaker closed this May 18, 2021
@qwbarch
Copy link
Contributor Author

qwbarch commented May 18, 2021

Thanks! Just wondering since I didn't see anything about this on the contribution guide, is there a limit to how many one can work on Good First Issue labeled issues?
The other issues are a bit advanced for my current skill-level so I'm looking to work on some easier ones until I'm more familiar with the library

@rossabaker
Copy link
Member

Nope, absolutely no limit! Just please leave a comment when you start to look at one, so we know we're not duplicating effort. And thanks for the great work on this one!

@qwbarch
Copy link
Contributor Author

qwbarch commented May 18, 2021

Good to hear, thanks for helping me out with the issue. Looking forward to working on other issues!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking Change breaks compatibility (binary or source) module:server
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make CORSConfig more typesafe
3 participants