Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

GRAILS-11036 : enhanced ValidationErrorsMarshaller to support ErrorObject. #461

Merged
merged 2 commits into from

2 participants

@memorydoor

@jeffbrown , wonder if my change is right or not, seems like ValidationErrorsMarshaller should just responsible for validation (more exactly field validation), and a Error object should have it's own Marshaller. I think somebody here with more background would have ideas.

@graemerocher

This pull request doesn't come with an associated test, are you able to provide one?

@memorydoor

Added test, please let me know anything I'm missing.

@graemerocher graemerocher merged commit f04a0f3 into grails:2.3.x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
13 ...org/codehaus/groovy/grails/web/converters/marshaller/json/ValidationErrorsMarshaller.java
@@ -27,6 +27,7 @@
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
+import org.springframework.validation.ObjectError;
/**
* @author Siegfried Puchbauer
@@ -64,6 +65,18 @@ public void marshalObject(Object object, JSON json) throws ConverterException {
json.property("message", fe.getDefaultMessage());
}
writer.endObject();
+ } else if (o instanceof ObjectError) {
+ ObjectError fe = (ObjectError) o;
+ writer.object();
+ json.property("object", fe.getObjectName());
+ Locale locale = LocaleContextHolder.getLocale();
+ if (applicationContext != null) {
+ json.property("message", applicationContext.getMessage(fe, locale));
+ }
+ else {
+ json.property("message", fe.getDefaultMessage());
+ }
+ writer.endObject();
}
}
writer.endArray();
View
33 ...dehaus/groovy/grails/web/converters/marshaller/json/ValidationErrorsMarshallerSpec.groovy
@@ -0,0 +1,33 @@
+package org.codehaus.groovy.grails.web.converters.marshaller.json
+
+import grails.converters.JSON
+import org.codehaus.groovy.grails.web.json.JSONWriter
+import org.springframework.validation.Errors
+import org.springframework.validation.ObjectError
+import spock.lang.Specification
+
+class ValidationErrorsMarshallerSpec extends Specification {
+
+ void "Test marshalObject handles org.springframework.validation.ObjectError"() {
+ given:
+ ObjectError objectError = new ObjectError('test', 'Error happening on test object.')
+
+ List<ObjectError> allErrors = [objectError]
+
+ ValidationErrorsMarshaller marshaller = new ValidationErrorsMarshaller()
+ Errors errors = Mock(Errors) {
+ 1 * getAllErrors() >> allErrors
+ }
+
+ JSON json = new JSON()
+
+ StringWriter stringWriter = new StringWriter()
+ json.writer = new JSONWriter(stringWriter)
+
+ when:
+ marshaller.marshalObject(errors, json)
+
+ then:
+ assert stringWriter.toString() == '{"errors":[{"object":"test","message":"Error happening on test object."}]}'
+ }
+}
Something went wrong with that request. Please try again.