Skip to content

Commit df9d405

Browse files
committed
Fix for GRAILS-10980: make encodeAsJSON() and encodeAsXML() work as in older grails versions for other than CharSequence/String and primitive objects
1 parent 81fe4c4 commit df9d405

File tree

6 files changed

+90
-3
lines changed

6 files changed

+90
-3
lines changed

grails-plugin-codecs/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
dependencies {
22
compile 'commons-codec:commons-codec:1.6'
3-
compile project(":grails-web")
4-
}
3+
compile project(":grails-web"), project(":grails-plugin-converters")
4+
}

grails-plugin-codecs/src/main/groovy/org/codehaus/groovy/grails/plugins/codecs/AbstractCharReplacementEncoder.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public AbstractCharReplacementEncoder(CodecIdentifier codecIdentifier) {
5454
* @see org.codehaus.groovy.grails.support.encoding.Encoder#encode(java.lang.Object)
5555
*/
5656
public Object encode(Object o) {
57+
return doCharReplacementEncoding(o);
58+
}
59+
60+
protected final Object doCharReplacementEncoding(Object o) {
5761
if (o == null) {
5862
return null;
5963
}
@@ -72,9 +76,17 @@ else if (o instanceof Character) {
7276
}
7377
}
7478
else {
75-
str = String.valueOf(o);
79+
str = convertToString(o);
7680
}
7781

82+
return escapeCharSequence(str);
83+
}
84+
85+
protected String convertToString(Object o) {
86+
return String.valueOf(o);
87+
}
88+
89+
protected Object escapeCharSequence(CharSequence str) {
7890
if (str == null || str.length() == 0) {
7991
return str;
8092
}

grails-plugin-codecs/src/main/groovy/org/codehaus/groovy/grails/plugins/codecs/HTMLEncoder.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,8 @@ public class HTMLEncoder extends XMLEncoder {
3232
public HTMLEncoder() {
3333
super(HTML_CODEC_IDENTIFIER);
3434
}
35+
36+
protected Object doEncode(Object o) {
37+
return doCharReplacementEncoding(o);
38+
}
3539
}

grails-plugin-codecs/src/main/groovy/org/codehaus/groovy/grails/plugins/codecs/JSONEncoder.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
*/
1616
package org.codehaus.groovy.grails.plugins.codecs;
1717

18+
import grails.converters.JSON;
19+
1820
import org.apache.commons.lang.StringUtils;
1921
import org.codehaus.groovy.grails.support.encoding.CodecIdentifier;
2022
import org.codehaus.groovy.grails.support.encoding.DefaultCodecIdentifier;
23+
import org.springframework.util.ClassUtils;
2124

2225
/**
2326
* Escapes characters in JSON output
@@ -37,6 +40,8 @@ public JSONEncoder() {
3740
super(JSON_CODEC_IDENTIFIER);
3841
}
3942

43+
44+
4045
/* (non-Javadoc)
4146
* @see org.codehaus.groovy.grails.plugins.codecs.AbstractCharReplacementEncoder#escapeCharacter(char, char)
4247
*/
@@ -82,4 +87,24 @@ protected String escapeCharacter(char ch, char previousChar) {
8287
public boolean isApplyToSafelyEncoded() {
8388
return true;
8489
}
90+
91+
@Override
92+
public final Object encode(Object o) {
93+
return doEncode(o);
94+
}
95+
96+
protected Object doEncode(Object o) {
97+
if(o == null) {
98+
return null;
99+
}
100+
if(o instanceof CharSequence || (o != null && ClassUtils.isPrimitiveOrWrapper(o.getClass())) ) {
101+
return super.encode(o);
102+
} else {
103+
return encodeToJsonString(o);
104+
}
105+
}
106+
107+
protected Object encodeToJsonString(Object o) {
108+
return new JSON(o).toString();
109+
}
85110
}

grails-plugin-codecs/src/main/groovy/org/codehaus/groovy/grails/plugins/codecs/XMLEncoder.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
*/
1616
package org.codehaus.groovy.grails.plugins.codecs;
1717

18+
import grails.converters.XML;
19+
1820
import org.codehaus.groovy.grails.support.encoding.CodecIdentifier;
1921
import org.codehaus.groovy.grails.support.encoding.DefaultCodecIdentifier;
22+
import org.springframework.util.ClassUtils;
2023

2124
/**
2225
* Encoder implementation that escapes some characters for inclusion in XML documents
@@ -81,4 +84,24 @@ protected String escapeCharacter(char ch, char previousChar) {
8184
}
8285
return null;
8386
}
87+
88+
@Override
89+
public final Object encode(Object o) {
90+
return doEncode(o);
91+
}
92+
93+
protected Object doEncode(Object o) {
94+
if(o == null) {
95+
return null;
96+
}
97+
if(o instanceof CharSequence || (o != null && ClassUtils.isPrimitiveOrWrapper(o.getClass()))) {
98+
return doCharReplacementEncoding(o);
99+
} else {
100+
return encodeToXmlString(o);
101+
}
102+
}
103+
104+
protected Object encodeToXmlString(Object o) {
105+
return new XML(o).toString();
106+
}
84107
}

grails-test-suite-web/src/test/groovy/org/codehaus/groovy/grails/web/codecs/CodecSpec.groovy

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package org.codehaus.groovy.grails.web.codecs
22

33
import grails.test.mixin.TestMixin
44
import grails.test.mixin.web.GroovyPageUnitTestMixin
5+
import spock.lang.Issue
56
import spock.lang.Specification
67

78
/**
@@ -29,6 +30,28 @@ class CodecSpec extends Specification {
2930
'"<script>"'.encodeAsJavaScript() == '\\u0022\\u003cscript\\u003e\\u0022'
3031
'"<script>"'.encodeAsJavaScript().encodeAsHTML() == '"<script>"'.encodeAsJavaScript()
3132
}
33+
34+
@Issue("GRAILS-10980")
35+
void "JSON codec behaviour like in Grails versions pre 2.3.x"() {
36+
when:
37+
String x=null
38+
then:
39+
[a: 1, b: 2, c: 3].encodeAsJSON() == '{"a":1,"b":2,"c":3}'
40+
x.encodeAsJSON() == null
41+
1.encodeAsJSON() == '1' // convert primitives to string
42+
true.encodeAsJSON() == 'true'
43+
}
44+
45+
@Issue("GRAILS-10980")
46+
void "XML codec behaviour like in Grails versions pre 2.3.x"() {
47+
when:
48+
String x=null
49+
then:
50+
[a: 1, b: 2, c: 3].encodeAsXML() == '<?xml version="1.0" encoding="UTF-8"?><map><entry key="a">1</entry><entry key="b">2</entry><entry key="c">3</entry></map>'
51+
x.encodeAsXML() == null
52+
1.encodeAsXML() == '1' // convert primitives to string
53+
true.encodeAsXML() == 'true'
54+
}
3255

3356
void "Test that the raw method works in GSP"() {
3457
when:"The raw method is called for a GSP expression"

0 commit comments

Comments
 (0)