Skip to content

Commit 553ef01

Browse files
committed
Fix and test for GRAILS-8925
1 parent 88301de commit 553ef01

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/taglib/ApplicationTagLibTests.groovy

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,12 @@ class ApplicationTagLibTests extends AbstractGrailsTagTests {
183183
assertOutputEquals('', template, [c:[:]])
184184
assertOutputEquals('foo', template, [c:[a:[b:'foo']]])
185185
}
186+
187+
void testSetTagWithScope() {
188+
def template = '<g:set var="var1" value="1"/>${var1}<g:set var="var1" value="2"/> ${var1}<g:set var="var2" value="3" scope="request"/> ${var2}<g:set var="var2" value="4" scope="request"/> ${var2}'
189+
assertOutputEquals('1 2 3 4', template)
190+
}
191+
186192

187193
void testIteration() {
188194
def template = '''<g:set var="counter" value="${1}" />

grails-web/src/main/groovy/org/codehaus/groovy/grails/web/pages/GroovyPageBinding.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ public class GroovyPageBinding extends AbstractGroovyPageBinding {
3737
private Binding parent;
3838
private GroovyPage owner;
3939
private Set<String> cachedParentVariableNames=new HashSet<String>();
40+
private GroovyPageRequestBinding pageRequestBinding;
41+
private boolean pageRequestBindingInitialized=false;
4042
private boolean root;
4143

4244
public GroovyPageBinding() {
@@ -64,6 +66,24 @@ public GroovyPageBinding(String[] args) {
6466
public Object getProperty(String property) {
6567
return getVariable(property);
6668
}
69+
70+
private GroovyPageRequestBinding findPageRequestBinding() {
71+
if(!pageRequestBindingInitialized && parent != null) {
72+
Binding nextParent = parent;
73+
while(nextParent != null && pageRequestBinding==null) {
74+
if(nextParent instanceof GroovyPageRequestBinding) {
75+
pageRequestBinding = (GroovyPageRequestBinding)nextParent;
76+
}
77+
if (nextParent instanceof GroovyPageBinding) {
78+
nextParent = ((GroovyPageBinding)nextParent).parent;
79+
} else {
80+
nextParent = null;
81+
}
82+
}
83+
pageRequestBindingInitialized=true;
84+
}
85+
return pageRequestBinding;
86+
}
6787

6888
@SuppressWarnings("unchecked")
6989
@Override
@@ -76,9 +96,11 @@ public Object getVariable(String name) {
7696
if (parent != null) {
7797
val = parent.getVariable(name);
7898
if (val != null) {
79-
// cache variable in this context since parent context cannot change during usage of this context
80-
getVariablesMap().put(name, val);
81-
cachedParentVariableNames.add(name);
99+
if(findPageRequestBinding()==null || !findPageRequestBinding().isRequestAttributeVariable(name)) {
100+
// cache variable in this context since parent context cannot change during usage of this context
101+
getVariablesMap().put(name, val);
102+
cachedParentVariableNames.add(name);
103+
}
82104
}
83105
}
84106
}

grails-web/src/main/groovy/org/codehaus/groovy/grails/web/pages/GroovyPageRequestBinding.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class GroovyPageRequestBinding extends AbstractGroovyPageBinding {
3838
private GrailsWebRequest webRequest;
3939
private Map<String, Class<?>> cachedDomainsWithoutPackage;
4040
private boolean developmentMode = Environment.isDevelopmentMode();
41+
private Set<String> requestAttributeVariables=new HashSet<String>();
4142

4243
private static Map<String, LazyRequestBasedValue> lazyRequestBasedValuesMap = new HashMap<String, LazyRequestBasedValue>();
4344
static {
@@ -113,28 +114,33 @@ public GroovyPageRequestBinding(GrailsWebRequest webRequest) {
113114
}
114115
}
115116
}
117+
118+
public boolean isRequestAttributeVariable(String name) {
119+
return requestAttributeVariables.contains(name);
120+
}
116121

117122
@Override
118123
public Object getVariable(String name) {
119124
Object val = getVariablesMap().get(name);
120125
if (val == null && !getVariablesMap().containsKey(name) && webRequest != null) {
121126
val = webRequest.getCurrentRequest().getAttribute(name);
122-
123-
if (val == null) {
127+
if(val != null) {
128+
requestAttributeVariables.add(name);
129+
} else {
124130
LazyRequestBasedValue lazyValue = lazyRequestBasedValuesMap.get(name);
125131
if (lazyValue != null) {
126132
val = lazyValue.evaluate(webRequest);
127133
}
128-
}
129-
130-
if (val == null && cachedDomainsWithoutPackage != null) {
131-
val = cachedDomainsWithoutPackage.get(name);
132-
}
133-
134-
// warn about missing variables in development mode
135-
if (val == null && developmentMode) {
136-
if (log.isDebugEnabled()) {
137-
log.debug("Variable '" + name + "' not found in binding or the value is null.");
134+
135+
if (val == null && cachedDomainsWithoutPackage != null) {
136+
val = cachedDomainsWithoutPackage.get(name);
137+
}
138+
139+
// warn about missing variables in development mode
140+
if (val == null && developmentMode) {
141+
if (log.isDebugEnabled()) {
142+
log.debug("Variable '" + name + "' not found in binding or the value is null.");
143+
}
138144
}
139145
}
140146
}

0 commit comments

Comments
 (0)