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(dart_frog): Pipeline does not maintain RequestContext #605

Merged
merged 3 commits into from
Apr 13, 2023

Conversation

felangel
Copy link
Contributor

Status

READY

Description

Currently, when using Dart Frog in a situation where middleware is applied, the RequestContext which the Handler is called with is not the same RequestContext which the Middleware is called with. This is problematic especially in tests because it is not possible to mock dependencies which are accessed via context.read from within middleware.

See the following example:

Future<Response> onRequest(RequestContext context, String id) async {
  Future<Response> handler(RequestContext context) async {
    final user = context.read<User>();
    return Response();
  }
  return handler.use(middleware)(context);
}

Middleware middleware(String id) {
  return (handler) {
    return (context) async {
      final database = context.read<Database>();
      final user = await database.getUserById(id);
      return handler(context.provide(() => user));
    };
  };
}

In the above example, the RequestContext in the middleware is not the same as the RequestContext in the handlers because dart frog creates a new RequestContext internally. This makes it impossible to mock context.read<Database>() in the above example.

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

@felangel felangel added the bug Something isn't working as expected label Apr 13, 2023
@felangel felangel self-assigned this Apr 13, 2023
@felangel felangel requested a review from erickzanardo as a code owner April 13, 2023 00:04
@scarletteliza scarletteliza merged commit 0ee1422 into main Apr 13, 2023
@scarletteliza scarletteliza deleted the fix/dart-frog-retain-request-context branch April 13, 2023 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working as expected
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants