You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/01/04 20:58:37 UTC

[groovy] branch GROOVY-5169 updated: GROOVY-7682, GROOVY-8371: JSON output: exclude static properties

This is an automated email from the ASF dual-hosted git repository.

emilles pushed a commit to branch GROOVY-5169
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/GROOVY-5169 by this push:
     new 924efcd  GROOVY-7682, GROOVY-8371: JSON output: exclude static properties
924efcd is described below

commit 924efcd480532480556413d8c12103c2ac890a15
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Tue Jan 4 14:58:29 2022 -0600

    GROOVY-7682, GROOVY-8371: JSON output: exclude static properties
---
 subprojects/groovy-json/build.gradle               | 14 +++--
 .../java/groovy/json/DefaultJsonGenerator.java     |  2 +
 .../test/groovy/groovy/json/JsonOutputTest.groovy  | 72 +++++++++++++++++++++-
 3 files changed, 82 insertions(+), 6 deletions(-)

diff --git a/subprojects/groovy-json/build.gradle b/subprojects/groovy-json/build.gradle
index d9fcb6b..726e824 100644
--- a/subprojects/groovy-json/build.gradle
+++ b/subprojects/groovy-json/build.gradle
@@ -21,13 +21,19 @@ plugins {
 }
 
 dependencies {
-    api rootProject  // JsonBuilder extends GroovyObjectSupport...
+    api rootProject
+
     testImplementation project(':groovy-test')
     testImplementation project(':groovy-dateutil')
     testImplementation 'net.javacrumbs.json-unit:json-unit:2.28.0'
-    testRuntimeOnly "org.slf4j:slf4j-api:${versions.slf4j}"
-    testRuntimeOnly project(':groovy-ant') // for JavadocAssertionTests
-    testRuntimeOnly 'com.google.code.gson:gson:2.8.9' // json-unit requires gson, jackson1, or jackson2
+
+    testRuntimeOnly project(':'), {
+        capabilities {
+            requireCapability 'org.apache.groovy:groovy-grapes'
+        }
+    }
+    testRuntimeOnly project(':groovy-ant')
+    testRuntimeOnly 'com.google.code.gson:gson:2.8.9' // json-unit requires gson, jackson1 or jackson2
 }
 
 plugins.withId('eclipse') {
diff --git a/subprojects/groovy-json/src/main/java/groovy/json/DefaultJsonGenerator.java b/subprojects/groovy-json/src/main/java/groovy/json/DefaultJsonGenerator.java
index 360ff72..cb9a8b8 100644
--- a/subprojects/groovy-json/src/main/java/groovy/json/DefaultJsonGenerator.java
+++ b/subprojects/groovy-json/src/main/java/groovy/json/DefaultJsonGenerator.java
@@ -55,6 +55,7 @@ import static groovy.json.JsonOutput.EMPTY_STRING_CHARS;
 import static groovy.json.JsonOutput.OPEN_BRACE;
 import static groovy.json.JsonOutput.OPEN_BRACKET;
 import static java.lang.reflect.Modifier.isPublic;
+import static java.lang.reflect.Modifier.isStatic;
 
 /**
  * A JsonGenerator that can be configured with various {@link JsonGenerator.Options}.
@@ -242,6 +243,7 @@ public class DefaultJsonGenerator implements JsonGenerator {
 
         for (MetaProperty mp : metaProperties) {
             if (!isPublic(mp.getModifiers())) continue; // GROOVY-5169
+            if ( isStatic(mp.getModifiers())) continue; // GROOVY-7682
 
             // skip write-only property: see File
             if (mp instanceof MetaBeanProperty) {
diff --git a/subprojects/groovy-json/src/test/groovy/groovy/json/JsonOutputTest.groovy b/subprojects/groovy-json/src/test/groovy/groovy/json/JsonOutputTest.groovy
index 2f053a5..41c3ceb 100644
--- a/subprojects/groovy-json/src/test/groovy/groovy/json/JsonOutputTest.groovy
+++ b/subprojects/groovy-json/src/test/groovy/groovy/json/JsonOutputTest.groovy
@@ -325,7 +325,7 @@ final class JsonOutputTest {
                 ] as JsonStreet[])
         ])
 
-        assert JsonOutput.prettyPrint(JsonOutput.toJson(city)) == '''\
+        assert JsonOutput.prettyPrint(toJson(city)) == '''\
             {
                 "name": "Paris",
                 "districts": [
@@ -448,7 +448,7 @@ final class JsonOutputTest {
         assert toJson({'\u0002' 0}) == '{"\\u0002":0}'
     }
 
-    @Test
+    @Test // GROOVY-7519
     void testFile() {
         def file  = File.createTempFile('test', 'file-json')
         file.deleteOnExit()
@@ -473,6 +473,74 @@ final class JsonOutputTest {
             assert json == '{"bar":"bar"}'
         '''
     }
+
+    @Test // GROOVY-7682
+    void testStackOverflowError1() {
+        assertScript '''
+            @Grab('joda-time:joda-time:2.10.10')
+            import org.joda.time.DateTime
+            import org.joda.time.format.DateTimeFormat
+            import org.joda.time.format.DateTimeFormatter
+
+            DateTimeFormatter formatter = DateTimeFormat.forPattern('yyyy-MM-dd HH:mm:ss.SSS z')
+            DateTime dt = formatter.parseDateTime('2015-11-20 13:37:21.123 EST')
+            String json = groovy.json.JsonOutput.toJson(dt)
+            net.javacrumbs.jsonunit.JsonAssert.assertJsonEquals("""{
+                "afterNow":false,
+                "beforeNow":true,
+                "centuryOfEra":20,
+                "dayOfMonth":20,
+                "dayOfWeek":5,
+                "dayOfYear":324,
+                "equalNow":false,
+                "era":1,
+                "hourOfDay":13,
+                "millisOfDay":49041123,
+                "millisOfSecond":123,
+                "minuteOfDay":817,
+                "minuteOfHour":37,
+                "monthOfYear":11,
+                "secondOfDay":49041,
+                "secondOfMinute":21,
+                "weekOfWeekyear":47,
+                "weekyear":2015,
+                "year":2015,
+                "yearOfCentury":15,
+                "yearOfEra":2015,
+                "zone":{
+                    "ID":"America/New_York",
+                    "fixed":false,
+                    "uncachedZone":{
+                        "ID":"America/New_York",
+                        "cachable":true,
+                        "fixed":false
+                    }
+                }
+            }""", json)
+        '''
+    }
+
+    @Test // GROOVY-8371
+    void testStackOverflowError2() {
+        assertScript '''
+            final  date = java.time.LocalDate.of(1970, 1, 1)
+            String json = groovy.json.JsonOutput.toJson(date)
+            net.javacrumbs.jsonunit.JsonAssert.assertJsonEquals("""{
+                "chronology":{
+                    "calendarType":"iso8601",
+                    "id":"ISO"
+                },
+                "dayOfMonth":1,
+                "dayOfWeek":"THURSDAY",
+                "dayOfYear":1,
+                "era":"CE",
+                "leapYear":false,
+                "month":"JANUARY",
+                "monthValue":1,
+                "year":1970
+            }""", json)
+        '''
+    }
 }
 
 //------------------------------------------------------------------------------