Skip to content

Commit

Permalink
Adapt test to check for proper generation of domain class names. Add …
Browse files Browse the repository at this point in the history
…logic to XML marshaller to emit class name as an attribute.
  • Loading branch information
AFSDevGuy committed Nov 22, 2017
1 parent 1c5a880 commit 4f2f2ea
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
Expand Up @@ -214,7 +214,7 @@ private ProxyHandler getProxyHandler() {

private void initDeepXMLConfiguration() {
DefaultConverterConfiguration<XML> deepConfig = new DefaultConverterConfiguration<XML>(ConvertersConfigurationHolder.getConverterConfiguration(XML.class), getProxyHandler());
deepConfig.registerObjectMarshaller(new org.grails.web.converters.marshaller.xml.DeepDomainClassMarshaller(includeDomainVersionProperty(getGrailsConfig(), "xml"), getProxyHandler(), grailsApplication));
deepConfig.registerObjectMarshaller(new org.grails.web.converters.marshaller.xml.DeepDomainClassMarshaller(includeDomainVersionProperty(getGrailsConfig(), "xml"), includeDomainClassProperty(getGrailsConfig(), "xml"), getProxyHandler(), grailsApplication));
ConvertersConfigurationHolder.setNamedConverterConfiguration(XML.class, "deep", deepConfig);
}

Expand Down
Expand Up @@ -38,6 +38,10 @@ public DeepDomainClassMarshaller(boolean includeDomainVersion, ProxyHandler prox
super(includeDomainVersion, proxyHandler, application);
}

public DeepDomainClassMarshaller(boolean includeVersion, boolean includeClass, ProxyHandler proxyHandler, GrailsApplication application) {
super(includeVersion, includeClass, proxyHandler, application);
}

@Override
protected boolean isRenderDomainClassRelations() {
return true;
Expand Down
Expand Up @@ -48,6 +48,7 @@
public class DomainClassMarshaller extends IncludeExcludePropertyMarshaller<XML> {

protected final boolean includeVersion;
protected boolean includeClass = false;
protected ProxyHandler proxyHandler;
protected GrailsApplication application;

Expand All @@ -65,6 +66,12 @@ public DomainClassMarshaller(boolean includeVersion, ProxyHandler proxyHandler,
this.proxyHandler = proxyHandler;
}

public DomainClassMarshaller(boolean includeVersion, boolean includeClass, ProxyHandler proxyHandler, GrailsApplication application) {
this(includeVersion, proxyHandler, application);
this.includeClass = includeClass;
}


public boolean supports(Object object) {
String name = ConverterUtil.trimProxySuffix(object.getClass().getName());
return application.isArtefactOfType(DomainClassArtefactHandler.TYPE, name);
Expand Down Expand Up @@ -93,6 +100,9 @@ public void marshalObject(Object value, XML xml) throws ConverterException {
Object versionValue = beanWrapper.getPropertyValue(domainClass.getVersion().getName());
xml.attribute("version", String.valueOf(versionValue));
}
if(includeClass && shouldInclude(includeExcludeSupport, includes, excludes, value, "class")) {
xml.attribute("class",domainClass.getClazz().getName());
}

GrailsDomainClassProperty[] properties = domainClass.getPersistentProperties();

Expand Down
Expand Up @@ -12,9 +12,15 @@ import spock.lang.Specification

class DomainClassMarshallerSpec extends Specification {


void setup() {
}

void initJson(boolean domainClassname) {
final initializer = new ConvertersConfigurationInitializer()
def grailsApplication = new DefaultGrailsApplication(Author, Book, RenamedIdentifier)
grailsApplication.config.setAt("grails.converters.json.domain.include.class", domainClassname)
grailsApplication.config.setAt("grails.converters.xml.domain.include.class", domainClassname)
grailsApplication.initialise()
def mappingContext = new KeyValueMappingContext("json")
mappingContext.addPersistentEntities(Book, Author, RenamedIdentifier)
Expand All @@ -26,12 +32,14 @@ class DomainClassMarshallerSpec extends Specification {
grailsApplication.setMappingContext(mappingContext)
initializer.grailsApplication = grailsApplication
initializer.initialize()

}

void "Test DomainClassMarshaller's should maintain order of relations"() {
def json, xml

when:
initJson(false)
def book = new Book(
id: 1, version: 2,
authorsSet: authors as Set,
Expand All @@ -58,12 +66,43 @@ class DomainClassMarshallerSpec extends Specification {
}

void "test marshaller should render the ID properly"() {
initJson(false)
when:
RenamedIdentifier ri = new RenamedIdentifier(newId: 3, name: "Sally")

then:
new JSON(ri).toString() == '{"newId":3,"name":"Sally"}'
}

void "test marshallers generate class names when options are set"() {
def json, xml
initJson(true)
when:
def book = new Book(
id: 1, version: 2,
authorsSet: authors as Set,
authorsMap: authors.inject([:]) { acc, val ->
acc[val.name] = val
acc
}
)
JSON.use('deep') {
json = book as JSON
}
XML.use('deep') {
xml = book as XML
}

then:
json.toString() == expectedJson
xml.toString() == expectedXml

where:
authors | expectedJson | expectedXml
[new Author(id: 1, name: 'a'), new Author(id: 2, name: 'b')] | '{"class":"org.grails.web.converters.marshaller.json.Book","id":1,"authorsSet":[{"class":"org.grails.web.converters.marshaller.json.Author","id":1,"name":"a"},{"class":"org.grails.web.converters.marshaller.json.Author","id":2,"name":"b"}],"authorsMap":{"a":{"class":"org.grails.web.converters.marshaller.json.Author","id":1,"name":"a"},"b":{"class":"org.grails.web.converters.marshaller.json.Author","id":2,"name":"b"}}}' | '<?xml version="1.0" encoding="UTF-8"?><book id="1" class="org.grails.web.converters.marshaller.json.Book"><authorsSet><author id="1" class="org.grails.web.converters.marshaller.json.Author"><name>a</name></author><author id="2" class="org.grails.web.converters.marshaller.json.Author"><name>b</name></author></authorsSet><authorsMap><entry key="a" id="1" class="org.grails.web.converters.marshaller.json.Author"><name>a</name></entry><entry key="b" id="2" class="org.grails.web.converters.marshaller.json.Author"><name>b</name></entry></authorsMap></book>'
[new Author(id: 2, name: 'b'), new Author(id: 1, name: 'a')] | '{"class":"org.grails.web.converters.marshaller.json.Book","id":1,"authorsSet":[{"class":"org.grails.web.converters.marshaller.json.Author","id":2,"name":"b"},{"class":"org.grails.web.converters.marshaller.json.Author","id":1,"name":"a"}],"authorsMap":{"b":{"class":"org.grails.web.converters.marshaller.json.Author","id":2,"name":"b"},"a":{"class":"org.grails.web.converters.marshaller.json.Author","id":1,"name":"a"}}}' | '<?xml version="1.0" encoding="UTF-8"?><book id="1" class="org.grails.web.converters.marshaller.json.Book"><authorsSet><author id="2" class="org.grails.web.converters.marshaller.json.Author"><name>b</name></author><author id="1" class="org.grails.web.converters.marshaller.json.Author"><name>a</name></author></authorsSet><authorsMap><entry key="b" id="2" class="org.grails.web.converters.marshaller.json.Author"><name>b</name></entry><entry key="a" id="1" class="org.grails.web.converters.marshaller.json.Author"><name>a</name></entry></authorsMap></book>'

}
}

@Entity
Expand Down

0 comments on commit 4f2f2ea

Please sign in to comment.