Skip to content

Commit f0940d7

Browse files
committed
Skipping over groovy metadata class + groovy test.
1 parent 85ed0e2 commit f0940d7

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

jr-objects/pom.xml

+6-3
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ has no other dependencies, and provides additional builder-style content generat
4040
<artifactId>jackson-core</artifactId>
4141
<version>${jackson.version.core}</version>
4242
</dependency>
43+
<dependency>
44+
<groupId>org.codehaus.groovy</groupId>
45+
<artifactId>groovy</artifactId>
46+
<version>3.0.18</version>
47+
<scope>test</scope>
48+
</dependency>
4349
</dependencies>
4450

4551
<build>
@@ -64,9 +70,6 @@ has no other dependencies, and provides additional builder-style content generat
6470
<goal>replace</goal>
6571
</goals>
6672
<phase>generate-sources</phase>
67-
<!--
68-
<phase>process-sources</phase>
69-
-->
7073
</execution>
7174
</executions>
7275
<configuration>

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/BeanPropertyIntrospector.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private POJODefinition _construct(Class<?> beanType, int features)
8888
private static void _introspect(Class<?> currType, Map<String, PropBuilder> props,
8989
int features)
9090
{
91-
if (currType == null || currType == Object.class) {
91+
if (currType == null || currType == Object.class || isGroovyMetaClass(currType)) {
9292
return;
9393
}
9494
// First, check base type
@@ -158,12 +158,7 @@ private static void _introspect(Class<?> currType, Map<String, PropBuilder> prop
158158
}
159159

160160
private static PropBuilder _propFrom(Map<String,PropBuilder> props, String name) {
161-
PropBuilder prop = props.get(name);
162-
if (prop == null) {
163-
prop = Prop.builder(name);
164-
props.put(name, prop);
165-
}
166-
return prop;
161+
return props.computeIfAbsent(name, Prop::builder);
167162
}
168163

169164
private static String decap(String name) {
@@ -182,4 +177,13 @@ private static String decap(String name) {
182177
return name;
183178
}
184179

180+
/**
181+
* Another helper method to deal with Groovy's problematic metadata accessors
182+
*
183+
* @implNote Groovy MetaClass have cyclic reference, and hence the class containing it should not be serialised without
184+
* either removing that reference, or skipping over such references.
185+
*/
186+
protected static boolean isGroovyMetaClass(Class<?> clazz) {
187+
return clazz.getName().startsWith("groovy.lang");
188+
}
185189
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.fasterxml.jackson.jr
2+
3+
import com.fasterxml.jackson.jr.ob.JSON
4+
import com.fasterxml.jackson.jr.ob.TestBase
5+
6+
class GroovyTest extends TestBase {
7+
8+
void testSimpleObject() throws Exception {
9+
var data = JSON.std.asString(new MyClass())
10+
var expected = "{\"aDouble\":0.0,\"aStr\":\"stringData\",\"anInt\":0,\"metaClass\":{}}";
11+
assertEquals(data, expected)
12+
}
13+
14+
private class MyClass {
15+
public int anInt; //testing groovy primitive
16+
public String aStr = "stringData"; //testing allocated object
17+
18+
public double aDouble; //
19+
public Double aDoublesd; //testing boxing object
20+
}
21+
}

0 commit comments

Comments
 (0)