Open guizmaii opened 2 months ago
Do you happen to know if this an issue only of the TestServer or does it also affect the "real" server implementation?
Do you happen to know if this an issue only of the TestServer or does it also affect the "real" server implementation?
I don't know. I can try to reproduce it in my app 🤔
I might be wrong but I'm thinking that in an app the usage pattern would be a bit different, probably it'd require a middleware to provide the env? Unless you're adding apps to the server dynamically that's it
/bounty $250
/attempt #3066
with your implementation plan/claim #3066
in the PR body to claim the bountyThank you for contributing to zio/zio-http!
Add a bounty • Share on socials
Attempt | Started (GMT+0) | Solution |
---|---|---|
🟢 @exi | Sep 20, 2024, 7:21:09 AM | #3160 |
/attempt #3066
I'd like to take a look at this
Algora profile | Completed bounties | Tech | Active attempts | Options |
---|---|---|---|---|
@exi | 2 bounties from 2 projects | JavaScript, Nix, Clojure & more |
Cancel attempt |
This Bug is not technically a bug but an unfortunate behaviour of the addRoutes call.
During the TestServer.addRoutes
call, the routes are bound to the current environment here:
https://github.com/zio/zio-http/blob/502331c7c8ec548b326120a9596864d30fabf4a7/zio-http-testkit/src/main/scala/zio/http/TestServer.scala#L94
Which in itself is not wrong, because the second call to addRoutes
actually contains the correct MyServer
service.
However it then goes and merges the old routes with the new routes, giving precedence to the old routes with the old environment:
https://github.com/zio/zio-http/blob/502331c7c8ec548b326120a9596864d30fabf4a7/zio-http/jvm/src/main/scala/zio/http/netty/server/NettyDriver.scala#L70
If we flip this here to say (newApp ++ oldApp)
instead of (oldApp ++ newApp)
, the behaviour is as expected.
@jdegoes I'd be happy to submit a tiny PR to flip this and make it work as expected.
@exi Go for it :)
💡 @exi submitted a pull request that claims the bounty. You can visit your bounty board to reward.
I just found this comment: https://github.com/zio/zio-http/blob/502331c7c8ec548b326120a9596864d30fabf4a7/zio-http/shared/src/main/scala/zio/http/Routes.scala#L50-L57
It seems like it's the intended behaviour to have old routes take precendence. So this test case here is (sadly) working as intended as long as you are not replacing the TestServer
for each test run. If you pull the whole environment including the TestServer
into the inner environment, it works as expected.
Gentle ping. I'm not quite sure what else there is to do after I figured out why this behaviour is like it is and there has been no meaningful feedback for 2 weeks now. I'd be happy to implement whatever change is best, but as it stands, this is working as intended.
Reproducer:
The test will fail on the second iteration of the
check
with the following assertion error:The
Env
of theapi
handler is never updated. It always contains the instance ofMyService
returning1
This is quite a nasty bug.
Cc @jdegoes @987Nabil