Gson map empty to null field in Pojo (rest-service.xml)

Hi,
I use rest-service.xml with simple pojos as parameters for my Rest-API. I implemented a bean validation in this pojos too. For example I have there a @NotNull constraint. I noticed that my (own and not cuba) frontend send empty strings. So the validation does not fire because the value is not null.
Is there a way to define the “global” GSON mapping to map empty strings to null?

Thanks! Reini

Hi Reinhard,

Assuming you are using bean validation, note that there are several annotations so perform the validation of different empty string variants: @notEmpty and @notBlank as well. Here is an explanation on them: https://www.baeldung.com/java-bean-validation-not-null-empty-blank

Perhaps you can just leverage those.

Cheers
Mario

Thank you @mario!
I know this constraints and @NotBlank would be much better than @NotNull for my use case. But is there a way to manipulate the internal gson builder from CUBA? Because I have also the use case that some fields must be null in some use case…

Thanks! Reini

Hi,
Services invocations are handled by the com.haulmont.addon.restapi.api.controllers.ServicesController which delegates the calls to the com.haulmont.addon.restapi.api.service.ServicesControllerManager.

Inside the _invokeServiceMethod method you’ll find that request parameter is converted to the object using the com.haulmont.addon.restapi.api.common.RestParseUtils#toObject method. Both RestParseUtils and ServicesControllerManager are spring beans so you a free to override these beans in your project.