Skip to content

Commit af81aea

Browse files
committed
JTE MapModelAndView not unpacking properly
- fix #3599
1 parent cb958c9 commit af81aea

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed

modules/jooby-jte/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
<scope>test</scope>
3434
</dependency>
3535

36+
<dependency>
37+
<groupId>org.mockito</groupId>
38+
<artifactId>mockito-core</artifactId>
39+
<scope>test</scope>
40+
</dependency>
41+
3642
<dependency>
3743
<groupId>org.jacoco</groupId>
3844
<artifactId>org.jacoco.agent</artifactId>

modules/jooby-jte/src/main/java/io/jooby/jte/JteTemplateEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public DataBuffer render(Context ctx, ModelAndView<?> modelAndView) {
3737
var output = new DataBufferOutput(buffer, StandardCharsets.UTF_8);
3838
var attributes = ctx.getAttributes();
3939
if (modelAndView instanceof MapModelAndView mapModelAndView) {
40-
var mapModel = new HashMap<>();
40+
var mapModel = new HashMap<String, Object>();
4141
mapModel.putAll(attributes);
4242
mapModel.putAll(mapModelAndView.getModel());
4343
jte.render(modelAndView.getView(), mapModel, output);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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.jte;
7+
8+
import static org.mockito.ArgumentMatchers.eq;
9+
import static org.mockito.ArgumentMatchers.isA;
10+
import static org.mockito.Mockito.*;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
import org.junit.jupiter.api.Test;
16+
17+
import gg.jte.TemplateEngine;
18+
import gg.jte.TemplateOutput;
19+
import io.jooby.Context;
20+
import io.jooby.MapModelAndView;
21+
import io.jooby.ModelAndView;
22+
import io.jooby.buffer.DataBuffer;
23+
import io.jooby.buffer.DataBufferFactory;
24+
25+
public class Issue3599 {
26+
27+
@Test
28+
public void shouldNotCallObjectMethodOnMapModels() {
29+
var bufferFactory = mock(DataBufferFactory.class);
30+
var buffer = mock(DataBuffer.class);
31+
when(bufferFactory.allocateBuffer()).thenReturn(buffer);
32+
33+
var attributes = Map.<String, Object>of("foo", 1);
34+
var mapModel = new HashMap<String, Object>();
35+
mapModel.put("param1", new Issue3599());
36+
37+
var ctx = mock(Context.class);
38+
when(ctx.getBufferFactory()).thenReturn(bufferFactory);
39+
when(ctx.getAttributes()).thenReturn(attributes);
40+
41+
var jteParams = new HashMap<String, Object>();
42+
jteParams.putAll(attributes);
43+
jteParams.putAll(mapModel);
44+
45+
var jte = mock(TemplateEngine.class);
46+
47+
var engine = new JteTemplateEngine(jte);
48+
var modelAndView = new MapModelAndView("template.jte", mapModel);
49+
engine.render(ctx, modelAndView);
50+
51+
verify(jte, times(1)).render(eq("template.jte"), eq(jteParams), isA(TemplateOutput.class));
52+
}
53+
54+
@Test
55+
public void shouldCallObjectMethodOnObjectModels() {
56+
var bufferFactory = mock(DataBufferFactory.class);
57+
var buffer = mock(DataBuffer.class);
58+
when(bufferFactory.allocateBuffer()).thenReturn(buffer);
59+
60+
var attributes = Map.<String, Object>of("foo", 1);
61+
var model = new Issue3599();
62+
63+
var ctx = mock(Context.class);
64+
when(ctx.getBufferFactory()).thenReturn(bufferFactory);
65+
when(ctx.getAttributes()).thenReturn(attributes);
66+
67+
var jte = mock(TemplateEngine.class);
68+
69+
var engine = new JteTemplateEngine(jte);
70+
var modelAndView = new ModelAndView<>("template.jte", model);
71+
engine.render(ctx, modelAndView);
72+
73+
verify(jte, times(1)).render(eq("template.jte"), eq(model), isA(TemplateOutput.class));
74+
}
75+
}

0 commit comments

Comments
 (0)