-
Notifications
You must be signed in to change notification settings - Fork 25.7k
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
HTTP_INTERCEPTORS are reset when a lazy loaded module imports another module importing HttpClientModule #20575
Comments
You should import the |
@realappie Sure, but this is quite uncommon that the application module imports a module which has no forRoot() method just because another module uses it (which could be some kind of library etc.). I would expect that this module imports the |
@thomashilzendegen ... then |
@mlc-mlapis Yep, that's true, but there is no |
@thomashilzendegen ... actually it is intended ... because in 99% cases when you need But you are right that |
@mlc-mlapis The funny thing is that this breaks currently, I have the HttpClientModule in my app module (see the Plunker), but when a lazy loaded module imports a module that imports the HttpClientModule, too, the interceptors are gone. This is because that multi-providers don't look at the injector inheritance, I guess. |
@thomashilzendegen same here |
@mlc-mlapis @realappie at least the docs should be updated to warn about importing the |
@macjohnny Actually, it isn't imported in the lazy-loaded module directly. The lazy-loaded module just uses a module which imports the HttpClientModule. And I think this is the dangerous part, you may not know that this module imports the HttpClientModule when its not your code, for example (without having a look). |
@thomashilzendegen, @macjohnny ... a standard structure is to access |
@mlc-mlapis i am sorry but i can‘t follow your argument. |
@macjohnny ... it could happen of course but still ... can you describe that scenario a bit to understand how it was happened as a real case? |
@mlc-mlapis in our app module we added a provider for our custom interceptor. |
@macjohnny ... thanks. Yes, it is a hidden = that's why a dangerous part. It should be notified more explicitly in the doc. But from the app architecture I still can't see any reason to import |
@mlc-mlapis i agree that it is a mistake to import the httpclientmodule in any module other than the app module, however, providing a forRoot() method with all providers would prevent this situation. Alternatively, the httpclientmodule could throw an error, since, as you stated, it is a mistake. But silently allowong it withiut any warning can lead to hard-to-find effects. |
Issues where exactly this has happened before: |
intended behavior. |
@mhevery Ok, maybe just with some more info/explanation how this should be handled when its intended? Just saying intended behavior and closing isn’t quite satisfying for all having this issue. Is the tenor to only import the Thanks for your help. |
I have third-party code that imports One example is swagger-codegen which imports HttpClientModule. Of course, I can change the code generator in this case, but the current behavior is generally a pitfall. |
@JohannesHoppe ... they made a mistake so they have to correct their third-party lib, what else to say. The API should be to inject |
@JohannesHoppe I think this issue does not apply to swagger-codegen, since the However, I agree with you that it would be nice if the mistake of importing the |
@macjohnny I would expect that I'm supposed to import |
@JohannesHoppe in the case of the swagger codegen generated However, it is not necessary to do so, because it does not add any functionality, as the |
* #7354: improve docs about importing multiple ApiModules, prevent angular/angular#20575 (comment) * #7354: prevent angular/angular#20575 (comment) add error message about importing the HttpModule / HttpClientModule * #7354: generate sample files * #7354: generate README.md also for non-npm packages
the di of router behavior is strange |
so as I understand it, it's intended behavior that the HttpClientModule can be imported per module, resulting in the ability to provide individual interceptors per HttpClient without impacting the others. If you'd like to share the same interceptors between all modules, you should import it in the app module only and not import it again in the feature modules. Is that correct? |
@Mobiletainment ... as I know Alex wants to make a change to avoid that. And also your summary is valid for lazy loaded modules and not for eagerly loaded ones. |
Could a warning be provided whenever this happens or some other sort of indication? It is completely unknown when this happens and why. A library (like ngx-markdown which is referenced here) can completely break an app without it being obvious why. |
Yep @Jrubzjeknf is right. The behaviour in A warning at least would be something really nice, it's quite hard to make sure that none of your libs are importing directly HttpClientModule on their own... |
Another example of this issue: dimpu/ngx-md#159 |
I will say I just bumped into this and it was pretty difficult to figure out what my issue was. To make matters more complicated, I have some backend services that do not require authentication and I was very confused why some things were working and some were not, when really, they were all not utilizing the interceptor due to this. I get that this is closed and intentional, but it's hard to debug when you are not aware of it. Most of my beginning searches on google returns people who failed to register the interceptor as a provider, which wasn't my case. |
I agree that HttpClientModule should only be imported in the root of the application and no external libraries. How i found out which library was using it. I installed Augury browser extension and scanned through the imports for the modules which my application used. |
Thanks @ndcunningham ! Now what do I do?
For now, I guess I have to stick with the first solution. |
No 2 is the only way to go, No 1 as a workaround until it's fixed. I'm not aware of any other solution too. |
@alfredott |
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
I'm submitting a...
Current behavior
The
HTTP_INTERCEPTORS
provider token is reset when a lazy loaded module imports another module which imports theHttpClientModule
by itself.Expected behavior
The
HTTP_INTERCEPTORS
from the parent injector should be used.Minimal reproduction of the problem with instructions
https://plnkr.co/edit/up8P57jwD3lUAZ9WyMS4
What is the motivation / use case for changing the behavior?
It seems that this is currently by design that a parent injector is not asked for the already registered providers from a multi-provider token. In my opinion it is not uncommon to have some kind of shared module which is also importing the
HttpClientModule
(because it want's to issue HTTP calls, but should not depend on having the module imported in the app module).Is there any other way to implement this scenario?
Likely relates also to #18894 in some kind.
Environment
The text was updated successfully, but these errors were encountered: