Skip to content

Commit 18400f7

Browse files
vaadin-botcaalador
andauthored
fix: no stream serialization without uis (#15287) (#15290)
Do not serialize the streamResourceRegistry if UIs are not serialized. Fixes #14893 Co-authored-by: caalador <mikael.grankvist@vaadin.com>
1 parent 4e69825 commit 18400f7

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/VaadinSession.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable {
131131

132132
private final Attributes attributes = new Attributes();
133133

134-
private final StreamResourceRegistry resourceRegistry;
134+
private transient StreamResourceRegistry resourceRegistry;
135135

136136
private long lastUnlocked;
137137

@@ -1056,6 +1056,7 @@ private void readObject(ObjectInputStream stream)
10561056
try {
10571057
stream.defaultReadObject();
10581058
uIs = (Map<Integer, UI>) stream.readObject();
1059+
resourceRegistry = (StreamResourceRegistry) stream.readObject();
10591060
pendingAccessQueue = new ConcurrentLinkedQueue<>();
10601061
} finally {
10611062
CurrentInstance.restoreInstances(old);
@@ -1081,8 +1082,10 @@ private void writeObject(java.io.ObjectOutputStream stream)
10811082
stream.defaultWriteObject();
10821083
if (serializeUIs) {
10831084
stream.writeObject(uIs);
1085+
stream.writeObject(resourceRegistry);
10841086
} else {
10851087
stream.writeObject(new HashMap<>());
1088+
stream.writeObject(new StreamResourceRegistry(this));
10861089
}
10871090
}
10881091

‎flow-server/src/test/java/com/vaadin/tests/server/SerializationTest.java‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import org.junit.Test;
1414
import org.mockito.Mockito;
1515

16+
import com.vaadin.flow.server.StreamRegistration;
17+
import com.vaadin.flow.server.StreamResource;
1618
import com.vaadin.flow.server.VaadinContext;
1719
import com.vaadin.flow.server.VaadinRequest;
1820
import com.vaadin.flow.server.VaadinService;
@@ -49,6 +51,43 @@ public void testSerializeVaadinSession_notProductionMode_disableDevModeSerializa
4951
session.getUIs().isEmpty());
5052
}
5153

54+
@Test
55+
public void testSerializeVaadinSession_notProductionMode_disableDevModeSerialization_streamResources_deserializedSessionHasNoUIs()
56+
throws Exception {
57+
58+
VaadinService vaadinService = new MockVaadinService(false, false);
59+
VaadinSession session = new VaadinSession(vaadinService);
60+
// This is done only for test purpose to init the session lock,
61+
// should be called by Flow internally as soon as the session has
62+
// been created.
63+
session.refreshTransients(null, vaadinService);
64+
MockUI ui = new MockUI(session);
65+
ui.doInit(null, 42);
66+
session.addUI(ui);
67+
68+
session.lock();
69+
final StreamRegistration name = session.getResourceRegistry()
70+
.registerResource(new StreamResource("name",
71+
() -> new ByteArrayInputStream(new byte[0])));
72+
session.unlock();
73+
74+
session = serializeAndDeserialize(session);
75+
// This is done only for test purpose to refresh the session lock,
76+
// should be called by Flow internally as soon as the session has
77+
// been retrieved from http session.
78+
session.refreshTransients(null, vaadinService);
79+
80+
Assert.assertNotNull(
81+
"UIs map should be available after devmode deserialization",
82+
session.getUIs());
83+
Assert.assertTrue("UIs should be empty after devmode deserialization",
84+
session.getUIs().isEmpty());
85+
Assert.assertTrue(
86+
"StreamResources should be empty after devmode deserialization",
87+
session.getResourceRegistry().getResource(name.getResourceUri())
88+
.isEmpty());
89+
}
90+
5291
@Test
5392
public void testSerializeVaadinSession_notProductionMode_enableDevModeSerialization_deserializedSessionHasUI()
5493
throws Exception {

0 commit comments

Comments
 (0)