Search code examples
jsongrailshibernate-sessiongrails-3.1

Grails 3.1 json rendering


I have just upgraded my application from grails 3.0.13 to grails 3.1.1. In doing so I got an interesting problem with JSON rendering.

I have been using a custom JSON marshaler:

JSON.registerObjectMarshaller(Appointment) { Appointment appointment ->
    [
        id: appointment.id,
        version: appointment.version,
        resourceChangeable: appointment.resourceChangeable,
        start: appointment.startTime.format("yyyy-MM-dd'T'HH:mm:ss"),
        end: appointment.endTime.format("yyyy-MM-dd'T'HH:mm:ss"),
        displayStartTime: appointment.displayStartTime,
        displayEndTime: appointment.displayEndTime,
        title: appointment.description,
        customerId: appointment.customerId,
        customerName: appointment.customer?.fullName,
        customerPhone: appointment.customer?.cellPhone,
        resourceId: appointment.resourceId,
        resourceName: appointment.resource?.name,
        editUrl: appointment.editUrl,
        updateUrl: appointment.updateUrl,
        errors: appointment.errors,
        eventBackgroundColor: appointment.resource?.backgroundColor,
        notes: appointment.notes
    ]
}

When I try to use this in grails 3.1.1 I get the following error:

No Datastore Session bound to thread, and configuration does not allow creation of non-transactional one here. Stacktrace follows:
org.grails.web.converters.exceptions.ConverterException: java.lang.IllegalStateException: No Datastore Session bound to thread, and configuration does not allow creation of non-transactional one here
    at grails.converters.JSON.value(JSON.java:184) ~[grails-plugin-converters-3.1.1.jar:3.1.1]
    at grails.converters.JSON.convertAnother(JSON.java:144) ~[grails-plugin-converters-3.1.1.jar:3.1.1]
    at grails.converters.JSON.value(JSON.java:184) ~[grails-plugin-converters-3.1.1.jar:3.1.1]
    at grails.converters.JSON.render(JSON.java:119) ~[grails-plugin-converters-3.1.1.jar:3.1.1]
    at grails.converters.JSON.render(JSON.java:132) ~[grails-plugin-converters-3.1.1.jar:3.1.1]
    at grails.artefact.controller.support.ResponseRenderer$Trait$Helper.render(ResponseRenderer.groovy:191) ~[grails-plugin-controllers-3.1.1.jar:3.1.1]
    at se.easycalendar.admin.CalendarController.getAppointments(CalendarController.groovy:39) ~[main/:na]
    at grails.plugin.springsecurity.web.filter.GrailsAnonymousAuthenticationFilter.doFilter(GrailsAnonymousAuthenticationFilter.groovy:53) ~[spring-security-core-3.0.3.jar:na]
    at grails.plugin.springsecurity.web.authentication.logout.MutableLogoutFilter.doFilter(MutableLogoutFilter.groovy:62) ~[spring-security-core-3.0.3.jar:na]
    at grails.plugin.springsecurity.web.SecurityRequestHolderFilter.doFilter(SecurityRequestHolderFilter.groovy:58) ~[spring-security-core-3.0.3.jar:na]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_72]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_72]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_72]
Caused by: java.lang.IllegalStateException: No Datastore Session bound to thread, and configuration does not allow creation of non-transactional one here
    at BootStrap$_closure1$_closure3.doCall(BootStrap.groovy:30) ~[main/:na]
    ... 13 common frames omitted

And it occurs on the appointment.resource?.name row. I have tried different ways of getting around this and have succeeded in a workaround for one usecase, but now I am getting the same error in another place. To change this everywhere seems like the wrong way to do it.

In my domain class I have declared the relationships like this:

class Appointment {
    String uuid = UUID.randomUUID().toString()
    Date startTime
    Date endTime
    String description
    String notes
    Boolean resourceChangeable = true

    Date dateCreated
    Date lastUpdated

    static belongsTo = [resource: Resource, customer: Customer, serviceEntityResource: ServiceEntityResource, sms: Sms]

    static constraints = {
        customer nullable: true
        uuid maxSize: 40
        serviceEntityResource nullable: true
        description maxSize: 100
        notes maxSize: 500, nullable: true, blank: true
        sms nullable: true
    }

    static mapping = {
        startTime index: 'appointment_start_time_idx'
        sms cascade: 'none'
        sort "startTime"
        customer fetch: 'join'
        resource fetch: 'join'
     }
}

So I thought that I should be eagerly loading the resource and customer fields? (I previously in grails 3.0.14 user "customer lazy: false" and it worked.

However it doesn't work now. So has there been a change to how the sessions work in gorm 5? What do I have to do to be able to continue to use relations in json rendering?


Solution

  • I found out the answer to my own question here, and thought I should post the results, in case someone else has the same problem.

    The issue was because of my JSON marshaller. I had used the following:

    JSON.registerObjectMarshaller(Appointment) { Appointment appointment ->
        [
        id: appointment.id,
        version: appointment.version,
        resourceChangeable: appointment.resourceChangeable,
        start: appointment.startTime.format("yyyy-MM-dd'T'HH:mm:ss"),
        end: appointment.endTime.format("yyyy-MM-dd'T'HH:mm:ss"),
        displayStartTime: appointment.displayStartTime,
        displayEndTime: appointment.displayEndTime,
        title: appointment.description,
        customerId: appointment.customerId,
        customerName: appointment.customer?.fullName,
        customerPhone: appointment.customer?.cellPhone,
        resourceId: appointment.resourceId,
        resourceName: appointment.resource?.name,
        editUrl: appointment.editUrl,
        updateUrl: appointment.updateUrl,
        errors: appointment.errors,
        eventBackgroundColor: appointment.resource?.backgroundColor,
        notes: appointment.notes
        ]
    }
    

    The problem is with the following lines:

    customerId: appointment.customerId,
    resourceId: appointment.resourceId,
    

    They worked ok in grails 3.0.x, but didn't work in grails 3.1.x probably because of the switch to gorm 5? Anyway, when I changed to:

    customerId: appointment.customer?.id,
    resourceId: appointment.resource?.id,
    

    Everything worked fine! I think it's a bit strange that I can't use the customerId and resourceId properties (which are stored on the appointment itself). I thought that the issue was from the other rows that accessed the models on other domain objects, but that was not the case.