Skip to content

Commit

Permalink
Skipping over groovy metadata class + groovy test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shounaks committed Feb 17, 2024
1 parent 85ed0e2 commit f0940d7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
9 changes: 6 additions & 3 deletions jr-objects/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ has no other dependencies, and provides additional builder-style content generat
<artifactId>jackson-core</artifactId>
<version>${jackson.version.core}</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>3.0.18</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -64,9 +70,6 @@ has no other dependencies, and provides additional builder-style content generat
<goal>replace</goal>
</goals>
<phase>generate-sources</phase>
<!--
<phase>process-sources</phase>
-->
</execution>
</executions>
<configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private POJODefinition _construct(Class<?> beanType, int features)
private static void _introspect(Class<?> currType, Map<String, PropBuilder> props,
int features)
{
if (currType == null || currType == Object.class) {
if (currType == null || currType == Object.class || isGroovyMetaClass(currType)) {
return;
}
// First, check base type
Expand Down Expand Up @@ -158,12 +158,7 @@ private static void _introspect(Class<?> currType, Map<String, PropBuilder> prop
}

private static PropBuilder _propFrom(Map<String,PropBuilder> props, String name) {
PropBuilder prop = props.get(name);
if (prop == null) {
prop = Prop.builder(name);
props.put(name, prop);
}
return prop;
return props.computeIfAbsent(name, Prop::builder);
}

private static String decap(String name) {
Expand All @@ -182,4 +177,13 @@ private static String decap(String name) {
return name;
}

/**
* Another helper method to deal with Groovy's problematic metadata accessors
*
* @implNote Groovy MetaClass have cyclic reference, and hence the class containing it should not be serialised without
* either removing that reference, or skipping over such references.
*/
protected static boolean isGroovyMetaClass(Class<?> clazz) {
return clazz.getName().startsWith("groovy.lang");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.fasterxml.jackson.jr

import com.fasterxml.jackson.jr.ob.JSON
import com.fasterxml.jackson.jr.ob.TestBase

class GroovyTest extends TestBase {

void testSimpleObject() throws Exception {
var data = JSON.std.asString(new MyClass())
var expected = "{\"aDouble\":0.0,\"aStr\":\"stringData\",\"anInt\":0,\"metaClass\":{}}";
assertEquals(data, expected)
}

private class MyClass {
public int anInt; //testing groovy primitive
public String aStr = "stringData"; //testing allocated object

public double aDouble; //
public Double aDoublesd; //testing boxing object
}
}

0 comments on commit f0940d7

Please sign in to comment.