Skip to content

Commit 15c2e94

Browse files
committed
1 parent 57b0ef6 commit 15c2e94

8 files changed

Lines changed: 78 additions & 4 deletions

File tree

src/main/java/act/view/ActionViewVarDef.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,19 @@
2222

2323
import act.app.ActionContext;
2424
import act.util.ActContext;
25+
import org.osgl.inject.BeanSpec;
2526

2627
public abstract class ActionViewVarDef extends VarDef {
28+
2729
protected ActionViewVarDef(String name, Class<?> type) {
2830
super(name, type);
2931
}
3032

33+
protected ActionViewVarDef(String name, BeanSpec spec) {
34+
super(name, spec);
35+
}
36+
37+
3138
@Override
3239
public final Object evaluate(ActContext context) {
3340
return eval((ActionContext) context);

src/main/java/act/view/ImplicitVariableProvider.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ private Method findMethod(Class<?> cls, String methodName) {
204204
private void register(Meta meta, Class<?> cls, Method method, App app) {
205205
final ReflectedActionViewVarDef def = new ReflectedActionViewVarDef(meta, cls, method, app);
206206
if (def.supportMailer) {
207-
MailerViewVarDef mailerVarDef = new MailerViewVarDef(meta.varName, def.returnType()) {
207+
MailerViewVarDef mailerVarDef = new MailerViewVarDef(meta.varName, def.returnType(app)) {
208208
@Override
209209
public Object eval(MailerContext context) {
210210
return def.getValue(context.app());
@@ -213,7 +213,7 @@ public Object eval(MailerContext context) {
213213
Act.viewManager().registerAppDefinedVar(mailerVarDef);
214214
}
215215
if (def.supportAction) {
216-
ActionViewVarDef actionVarDef = new ActionViewVarDef(meta.varName, def.returnType()) {
216+
ActionViewVarDef actionVarDef = new ActionViewVarDef(meta.varName, def.returnType(app)) {
217217
@Override
218218
public Object eval(ActionContext context) {
219219
return def.getValue(context.app());
@@ -280,8 +280,8 @@ private boolean requireAction(java.lang.reflect.Type type) {
280280
return false;
281281
}
282282

283-
Class<?> returnType() {
284-
return method.getReturnType();
283+
BeanSpec returnType(App app) {
284+
return BeanSpec.of(method.getGenericReturnType(), null, app.injector());
285285
}
286286

287287
Object getValue(App app) {

src/main/java/act/view/MailerViewVarDef.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,17 @@
2222

2323
import act.mail.MailerContext;
2424
import act.util.ActContext;
25+
import org.osgl.inject.BeanSpec;
2526

2627
public abstract class MailerViewVarDef extends VarDef {
2728
protected MailerViewVarDef(String name, Class<?> type) {
2829
super(name, type);
2930
}
3031

32+
protected MailerViewVarDef(String name, BeanSpec spec) {
33+
super(name, spec);
34+
}
35+
3136
@Override
3237
public final Object evaluate(ActContext context) {
3338
return eval((MailerContext) context);

src/main/java/act/view/VarDef.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222

2323
import act.util.ActContext;
24+
import org.osgl.inject.BeanSpec;
2425
import org.osgl.util.E;
2526
import org.osgl.util.S;
2627

@@ -48,6 +49,23 @@ protected VarDef(String name, Class<?> type) {
4849
this.type = type.getCanonicalName().replace('$', '.');
4950
}
5051

52+
/**
53+
* Construct an implicit variable by name and {@link BeanSpec bean spec}
54+
*
55+
* @param name the name of the variable. Could be referenced in
56+
* view template to get the variable
57+
* @param type the {@link BeanSpec} of the variable. Some view solution e.g.
58+
* Rythm needs to explicitly declare the template
59+
* arguments. And type information is used by those
60+
* static template engines
61+
*/
62+
protected VarDef(String name, BeanSpec type) {
63+
E.illegalArgumentIf(S.blank(name), "VarDef name cannot be empty");
64+
E.NPE(type);
65+
this.name = name;
66+
this.type = type.toString();
67+
}
68+
5169
public String name() {
5270
return name;
5371
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package testapp.endpoint.ghissues;
2+
3+
import act.controller.annotation.TemplateContext;
4+
import act.controller.annotation.UrlContext;
5+
import act.view.ProvidesImplicitTemplateVariable;
6+
import org.osgl.mvc.annotation.GetAction;
7+
import org.osgl.util.C;
8+
9+
import java.util.List;
10+
11+
/**
12+
* Test `@ResponseStatus` annotation on direct return object
13+
*/
14+
@UrlContext("287")
15+
@TemplateContext("gh/287")
16+
public class GH287 extends GithubIssueBase {
17+
18+
@ProvidesImplicitTemplateVariable("bar")
19+
public List<Integer> bar() {
20+
return C.list(1, 2, 3);
21+
}
22+
23+
@GetAction
24+
public void foo() {
25+
throw render();
26+
}
27+
28+
}

testapp/src/main/java/testapp/endpoint/ghissues/GithubIssueBase.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33

44
import act.controller.Controller;
5+
import act.controller.annotation.TemplateContext;
56
import act.controller.annotation.UrlContext;
67

78
@UrlContext("/gh")
9+
@TemplateContext("/gh")
810
public class GithubIssueBase extends Controller.Base {
911
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@(bar.get(0) + bar.get(1) + bar.get(2))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package testapp.endpoint;
2+
3+
import org.junit.Test;
4+
5+
public class GHIssue287 extends EndpointTester {
6+
7+
@Test
8+
public void testFoo() throws Exception {
9+
url("/gh/287").get();
10+
bodyContains("6");
11+
}
12+
13+
}

0 commit comments

Comments
 (0)