fix(dio): support classes that implement Interceptor - #2591
Open
AlexV525 wants to merge 1 commit into
Open
Conversation
PR #2567 introduced private instance methods (_invokeRequest, _invokeResponse, _invokeError) on Interceptor and dispatched the pipeline through them. Classes using `implements Interceptor` do not inherit private methods, causing NoSuchMethodError at runtime (#2590). Move dynamic dispatch to a top-level _invokeCallbackDynamically helper and call it via closures at each dispatch site. The pipeline now references only the public onRequest/onResponse/onError methods, so both `extends Interceptor` and `implements Interceptor` work. Added regression tests for synchronous and async `implements Interceptor` usage across all three interceptor stages. Co-Authored-By: GLM-5.2 <noreply@zhipu.ai>
This comment was marked as duplicate.
This comment was marked as duplicate.
Member
Author
|
(Workflows are failing because of flutter/flutter#191056) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2590
Problem
NoSuchMethodError: Class 'X' has no instance method '_invokeError'at runtime when a class usesimplements Interceptorinstead ofextends Interceptor.PR #2567 added private instance methods (
_invokeRequest,_invokeResponse,_invokeError) onInterceptorand dispatched the interceptor pipeline through them. These methods exist only via inheritance — a class thatimplements Interceptorprovides the publiconRequest/onResponse/onErrormethods but does not inherit the private ones, so the pipeline crashes at runtime.This is a behavioral regression introduced in 5.11.0 (the release that shipped #2567).
Fix
Move the dynamic-dispatch mechanism from private instance methods on
Interceptorto a top-level helper function_invokeCallbackDynamically, and call it via closures at each dispatch site (DioMixin.fetchandQueuedInterceptor._handle*). The pipeline now references exclusively the publiconRequest/onResponse/onErrormethods. Bothextends Interceptorandimplements Interceptorwork.The dynamic invocation is preserved: a
Future<void>returned by anasyncoverride of avoidmethod is still captured at runtime so_observeInterceptorCallbackcan observe asynchronous errors — the original motivation from #2567.Why a top-level function instead of private instance methods?
Private instance methods are not part of the interface contract. Any class using
implements Interceptorwould not inherit them. A top-level function that takes the public method as a parameter keeps dispatch through the public API surface only. TheInterceptorclass doc comment now documents this dispatch contract.Compatibility
extends Interceptor,InterceptorsWrapper,QueuedInterceptor, orQueuedInterceptorsWrapper.implements Interceptorusers.Verification
Added 3 regression tests covering
implements Interceptor:onRequestoverride that throws — dynamic dispatch captures theFuture, observer rejects the handler.dart analyze --fatal-infosclean. 55 tests pass acrossinterceptor_test.dartandqueued_interceptor_test.dart(including the existing #2565 deduplication regression test, all #2499 async-hang tests, and wrapper-subclass override tests). Broader non-network suite (dio_mixin_test,cancel_token_test,exception_test): 68 passed, 1 skipped (TLS).Unverified: Web platform (Chrome/Firefox) not run locally — dynamic dispatch is a Dart language-spec guarantee, not a VM optimization, and the same pattern has been used in
_InterceptorWrapperMixinsince #2567.AI Attribution
Implementation, tests, and analysis by GLM-5.2.