Skip to content

Commit 033c502

Browse files
committed
template-engine: must initialize flash attributes only when there is one flash cookie
- fix #3607
1 parent d6222a0 commit 033c502

File tree

6 files changed

+107
-1
lines changed

6 files changed

+107
-1
lines changed

Diff for: jooby/src/main/java/io/jooby/Context.java

+7
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,13 @@ public interface Context extends Registry {
154154
*/
155155
@NonNull FlashMap flash();
156156

157+
/**
158+
* Flash map or null when no flash cookie exists.
159+
*
160+
* @return Flash map or null when no flash cookie exists.
161+
*/
162+
@Nullable FlashMap flashOrNull();
163+
157164
/**
158165
* Get a flash attribute.
159166
*

Diff for: jooby/src/main/java/io/jooby/DefaultContext.java

+6
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ default boolean matches(String pattern) {
110110
FlashMap.NAME, key -> FlashMap.create(this, getRouter().getFlashCookie().clone()));
111111
}
112112

113+
@Nullable @Override
114+
default FlashMap flashOrNull() {
115+
var flashCookie = cookie(getRouter().getFlashCookie().getName());
116+
return flashCookie.isMissing() ? null : flash();
117+
}
118+
113119
/**
114120
* Get a flash attribute.
115121
*

Diff for: jooby/src/main/java/io/jooby/ForwardingContext.java

+5
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,11 @@ public FlashMap flash() {
673673
return ctx.flash();
674674
}
675675

676+
@Nullable @Override
677+
public FlashMap flashOrNull() {
678+
return ctx.flashOrNull();
679+
}
680+
676681
@NonNull @Override
677682
public Value flash(@NonNull String name) {
678683
return ctx.flash(name);

Diff for: jooby/src/main/java/io/jooby/TemplateEngine.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public interface TemplateEngine extends MessageEncoder {
3939
@Override
4040
default DataBuffer encode(@NonNull Context ctx, @NonNull Object value) throws Exception {
4141
// initialize flash and session attributes (if any)
42-
ctx.flash();
42+
ctx.flashOrNull();
4343
ctx.sessionOrNull();
4444

4545
ctx.setDefaultResponseType(MediaType.html);

Diff for: jooby/src/test/java/io/jooby/Issue3607.java

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby;
7+
8+
import static org.mockito.Mockito.*;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
import io.jooby.buffer.DataBuffer;
13+
import io.jooby.buffer.DefaultDataBufferFactory;
14+
15+
public class Issue3607 {
16+
17+
private static class TemplateEngineImpl implements TemplateEngine {
18+
@Override
19+
public DataBuffer render(Context ctx, ModelAndView<?> modelAndView) throws Exception {
20+
// do nothing
21+
return DefaultDataBufferFactory.sharedInstance.wrap(new byte[0]);
22+
}
23+
}
24+
25+
@Test
26+
public void shouldNotGenerateEmptyFlashMap() throws Exception {
27+
var ctx = mock(Context.class);
28+
29+
var templateEngine = new TemplateEngineImpl();
30+
templateEngine.encode(ctx, ModelAndView.map("index.html"));
31+
32+
verify(ctx, times(1)).flashOrNull();
33+
verify(ctx, times(1)).sessionOrNull();
34+
verify(ctx, times(1)).setDefaultResponseType(MediaType.html);
35+
}
36+
}

Diff for: tests/src/test/java/io/jooby/i3607/Issue3607.java

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.i3607;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
import java.util.Map;
11+
import java.util.concurrent.CountDownLatch;
12+
import java.util.concurrent.atomic.AtomicBoolean;
13+
14+
import io.jooby.FlashMap;
15+
import io.jooby.ModelAndView;
16+
import io.jooby.junit.ServerTest;
17+
import io.jooby.junit.ServerTestRunner;
18+
import io.jooby.pebble.PebbleModule;
19+
20+
public class Issue3607 {
21+
@ServerTest
22+
public void shouldNotGenerateEmptyFlashMap(ServerTestRunner runner) throws InterruptedException {
23+
var latch = new CountDownLatch(1);
24+
var mustBeNull = new AtomicBoolean(false);
25+
runner
26+
.define(
27+
app -> {
28+
app.install(new PebbleModule());
29+
app.use(
30+
next ->
31+
ctx -> {
32+
ctx.onComplete(
33+
done -> {
34+
mustBeNull.set(!done.getAttributes().containsKey(FlashMap.NAME));
35+
latch.countDown();
36+
});
37+
return next.apply(ctx);
38+
});
39+
app.get("/3607", ctx -> ModelAndView.map("index.pebble", Map.of("name", "Pebble")));
40+
})
41+
.ready(
42+
client -> {
43+
client.get(
44+
"/3607",
45+
rsp -> {
46+
assertEquals("Hello Pebble!", rsp.body().string().trim());
47+
});
48+
});
49+
latch.await();
50+
assertTrue(mustBeNull.get(), "Flash map must be null");
51+
}
52+
}

0 commit comments

Comments
 (0)