diff --git a/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/json/GenericJavaBeanMarshaller.java b/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/json/GenericJavaBeanMarshaller.java index 424aed49ee1..ca526be4789 100644 --- a/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/json/GenericJavaBeanMarshaller.java +++ b/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/json/GenericJavaBeanMarshaller.java @@ -61,6 +61,7 @@ public void marshalObject(Object o, JSON json) throws ConverterException { if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name)) continue; if (readMethod != null && !(name.equals("metaClass")) && !(name.equals("class"))) { + if (Modifier.isStatic(readMethod.getModifiers())) continue; if (readMethod.getAnnotation(PersistenceMethod.class) != null) continue; if (readMethod.getAnnotation(ControllerMethod.class) != null) continue; Object value = readMethod.invoke(o, (Object[]) null); diff --git a/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/json/GroovyBeanMarshaller.java b/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/json/GroovyBeanMarshaller.java index 70249bcd28a..4bccb38bf93 100644 --- a/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/json/GroovyBeanMarshaller.java +++ b/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/json/GroovyBeanMarshaller.java @@ -63,6 +63,7 @@ public void marshalObject(Object o, JSON json) throws ConverterException { if (!shouldInclude(includeExcludeSupport, includes, excludes, o, name)) continue; if (readMethod != null && !(name.equals("metaClass")) && !(name.equals("class"))) { + if (Modifier.isStatic(readMethod.getModifiers())) continue; if (readMethod.getAnnotation(PersistenceMethod.class) != null) continue; if (readMethod.getAnnotation(ControllerMethod.class) != null) continue; Object value = readMethod.invoke(o, (Object[]) null); diff --git a/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/xml/GenericJavaBeanMarshaller.java b/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/xml/GenericJavaBeanMarshaller.java index 34fd3d2338c..f00d6966ed3 100644 --- a/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/xml/GenericJavaBeanMarshaller.java +++ b/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/xml/GenericJavaBeanMarshaller.java @@ -45,6 +45,7 @@ public void marshalObject(Object o, XML xml) throws ConverterException { String name = property.getName(); Method readMethod = property.getReadMethod(); if (readMethod != null) { + if (Modifier.isStatic(readMethod.getModifiers())) continue; Object value = readMethod.invoke(o, (Object[]) null); xml.startNode(name); xml.convertAnother(value); diff --git a/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/xml/GroovyBeanMarshaller.java b/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/xml/GroovyBeanMarshaller.java index 4ab7915b137..6b724c01bc5 100644 --- a/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/xml/GroovyBeanMarshaller.java +++ b/grails-converters/src/main/groovy/org/grails/web/converters/marshaller/xml/GroovyBeanMarshaller.java @@ -63,6 +63,7 @@ public void marshalObject(Object o, XML xml) throws ConverterException { if (isEntity && (name.equals(GormProperties.ATTACHED) || name.equals(GormProperties.ERRORS))) continue; Method readMethod = property.getReadMethod(); if (readMethod != null && !(name.equals("metaClass")) && !(name.equals("class"))) { + if (Modifier.isStatic(readMethod.getModifiers())) continue; if (readMethod.getAnnotation(PersistenceMethod.class) != null) continue; if (readMethod.getAnnotation(ControllerMethod.class) != null) continue; Object value = readMethod.invoke(o, (Object[]) null); diff --git a/grails-converters/src/test/groovy/org/grails/web/converters/marshaller/json/StaticPropertySpec.groovy b/grails-converters/src/test/groovy/org/grails/web/converters/marshaller/json/StaticPropertySpec.groovy new file mode 100644 index 00000000000..7c0168e45f8 --- /dev/null +++ b/grails-converters/src/test/groovy/org/grails/web/converters/marshaller/json/StaticPropertySpec.groovy @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.grails.web.converters.marshaller.json + +import spock.lang.Specification + +import org.springframework.context.ApplicationContext + +import grails.converters.JSON +import grails.core.DefaultGrailsApplication +import grails.validation.Constrained +import org.grails.datastore.mapping.keyvalue.mapping.config.KeyValueMappingContext +import org.grails.datastore.mapping.model.MappingContext +import org.grails.web.converters.configuration.ConvertersConfigurationInitializer + +class StaticPropertySpec extends Specification { + void initJson() { + final initializer = new ConvertersConfigurationInitializer() + def grailsApplication = new DefaultGrailsApplication(MyGroovyBean) + grailsApplication.initialise() + def mappingContext = new KeyValueMappingContext("json") + grailsApplication.setApplicationContext(Stub(ApplicationContext) { + getBean('grailsDomainClassMappingContext', MappingContext) >> { + mappingContext + } + }) + grailsApplication.setMappingContext(mappingContext) + initializer.grailsApplication = grailsApplication + initializer.initialize() + + } + + void "static property should be excluded"() { + given: + initJson() + + when: + MyGroovyBean bean = new MyGroovyBean(aProperty: 'testing') + + then: + def jsonString = new JSON(bean).toString() + jsonString == '{"aProperty":"testing"}' + } +} + +class MyGroovyBean { + static Map getConstraintsMap() { + [:] + } + + String aProperty +} diff --git a/grails-test-suite-web/src/test/groovy/org/grails/plugins/web/rest/render/xml/DefaultXmlRendererSpec.groovy b/grails-test-suite-web/src/test/groovy/org/grails/plugins/web/rest/render/xml/DefaultXmlRendererSpec.groovy index 945ccdfc41b..4df7ef3cdb1 100644 --- a/grails-test-suite-web/src/test/groovy/org/grails/plugins/web/rest/render/xml/DefaultXmlRendererSpec.groovy +++ b/grails-test-suite-web/src/test/groovy/org/grails/plugins/web/rest/render/xml/DefaultXmlRendererSpec.groovy @@ -18,7 +18,6 @@ */ package org.grails.plugins.web.rest.render.xml -import groovy.transform.CompileStatic import groovy.xml.XmlSlurper import grails.converters.XML import grails.core.DefaultGrailsApplication @@ -36,7 +35,6 @@ import org.grails.web.servlet.mvc.GrailsWebRequest import org.springframework.mock.web.MockHttpServletRequest import org.springframework.mock.web.MockHttpServletResponse import org.springframework.mock.web.MockServletContext -import spock.lang.PendingFeature import spock.lang.Specification /** @@ -56,7 +54,6 @@ class DefaultXmlRendererSpec extends Specification implements DomainUnitTest