Skip to content

Commit

Permalink
fix: no stream serialization without uis (#15287)
Browse files Browse the repository at this point in the history
Do not serialize the streamResourceRegistry
if UIs are not serialized.

Fixes #14893
  • Loading branch information
caalador authored and vaadin-bot committed Nov 28, 2022
1 parent f22229c commit eb5c574
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable {

private final Attributes attributes = new Attributes();

private final StreamResourceRegistry resourceRegistry;
private transient StreamResourceRegistry resourceRegistry;

private long lastUnlocked;

Expand Down Expand Up @@ -1056,6 +1056,7 @@ private void readObject(ObjectInputStream stream)
try {
stream.defaultReadObject();
uIs = (Map<Integer, UI>) stream.readObject();
resourceRegistry = (StreamResourceRegistry) stream.readObject();
pendingAccessQueue = new ConcurrentLinkedQueue<>();
} finally {
CurrentInstance.restoreInstances(old);
Expand All @@ -1081,8 +1082,10 @@ private void writeObject(java.io.ObjectOutputStream stream)
stream.defaultWriteObject();
if (serializeUIs) {
stream.writeObject(uIs);
stream.writeObject(resourceRegistry);
} else {
stream.writeObject(new HashMap<>());
stream.writeObject(new StreamResourceRegistry(this));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.junit.Test;
import org.mockito.Mockito;

import com.vaadin.flow.server.StreamRegistration;
import com.vaadin.flow.server.StreamResource;
import com.vaadin.flow.server.VaadinContext;
import com.vaadin.flow.server.VaadinRequest;
import com.vaadin.flow.server.VaadinService;
Expand Down Expand Up @@ -49,6 +51,43 @@ public void testSerializeVaadinSession_notProductionMode_disableDevModeSerializa
session.getUIs().isEmpty());
}

@Test
public void testSerializeVaadinSession_notProductionMode_disableDevModeSerialization_streamResources_deserializedSessionHasNoUIs()
throws Exception {

VaadinService vaadinService = new MockVaadinService(false, false);
VaadinSession session = new VaadinSession(vaadinService);
// This is done only for test purpose to init the session lock,
// should be called by Flow internally as soon as the session has
// been created.
session.refreshTransients(null, vaadinService);
MockUI ui = new MockUI(session);
ui.doInit(null, 42);
session.addUI(ui);

session.lock();
final StreamRegistration name = session.getResourceRegistry()
.registerResource(new StreamResource("name",
() -> new ByteArrayInputStream(new byte[0])));
session.unlock();

session = serializeAndDeserialize(session);
// This is done only for test purpose to refresh the session lock,
// should be called by Flow internally as soon as the session has
// been retrieved from http session.
session.refreshTransients(null, vaadinService);

Assert.assertNotNull(
"UIs map should be available after devmode deserialization",
session.getUIs());
Assert.assertTrue("UIs should be empty after devmode deserialization",
session.getUIs().isEmpty());
Assert.assertTrue(
"StreamResources should be empty after devmode deserialization",
session.getResourceRegistry().getResource(name.getResourceUri())
.isEmpty());
}

@Test
public void testSerializeVaadinSession_notProductionMode_enableDevModeSerialization_deserializedSessionHasUI()
throws Exception {
Expand Down

0 comments on commit eb5c574

Please sign in to comment.