From 04db1e4e23cb9c833efd9982a148a7a29306bb4f Mon Sep 17 00:00:00 2001 From: "Renat R. Safiullin" Date: Tue, 16 Jun 2026 22:49:06 +0300 Subject: [PATCH] Honor CDI scope of @ServerEndpoint beans in CdiComponentProvider CdiComponentProvider.create() always produced a fresh endpoint instance per WebSocket connection via InjectionTarget, ignoring the bean's CDI scope. For an @ApplicationScoped or @Singleton @ServerEndpoint this meant the per-connection endpoint object differed from the contextual bean seen by CDI event observers and other injection points, so state set in a lifecycle callback (e.g. a Set populated in @OnOpen) was invisible to observers that broadcast to it. Resolve the managed bean for the endpoint class and, when it is normal-scoped (@ApplicationScoped, @SessionScoped, ...) or @Singleton, return the shared contextual reference from BeanManager.getReference() so callbacks, injected collaborators and observers all act on the same instance. Dependent-scoped (and non-bean) endpoints keep the previous per-connection behavior and are still cleaned up in destroy(); shared references are intentionally not registered for per-connection destruction. Adds a regression test covering @ApplicationScoped, @Singleton and @Dependent endpoints. Fixes #961 Signed-off-by: Renat R. Safiullin --- containers/glassfish/cdi/pom.xml | 6 + .../tyrus/gf/cdi/CdiComponentProvider.java | 81 +++++++-- .../gf/cdi/CdiComponentProviderTest.java | 168 ++++++++++++++++++ 3 files changed, 242 insertions(+), 13 deletions(-) create mode 100644 containers/glassfish/cdi/src/test/java/org/glassfish/tyrus/gf/cdi/CdiComponentProviderTest.java diff --git a/containers/glassfish/cdi/pom.xml b/containers/glassfish/cdi/pom.xml index 81af13bc..9e003e07 100755 --- a/containers/glassfish/cdi/pom.xml +++ b/containers/glassfish/cdi/pom.xml @@ -1,6 +1,7 @@