From c0043485b77c045b8eae887d8aa08f154431b611 Mon Sep 17 00:00:00 2001 From: Maxim Gorbunkov Date: Thu, 11 May 2017 14:57:19 +0400 Subject: [PATCH] PL-9071 REST API serialization error if entity is a POJO field --- .../core/app/serialization/EntitySerialization.java | 6 ++++++ .../app/serialization/EntitySerializationAPI.java | 11 ++++++++++- .../com/haulmont/restapi/common/RestParseUtils.java | 8 +------- .../restapi/service/ServicesControllerManager.java | 2 +- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/modules/global/src/com/haulmont/cuba/core/app/serialization/EntitySerialization.java b/modules/global/src/com/haulmont/cuba/core/app/serialization/EntitySerialization.java index 3943886d63..47194c332a 100644 --- a/modules/global/src/com/haulmont/cuba/core/app/serialization/EntitySerialization.java +++ b/modules/global/src/com/haulmont/cuba/core/app/serialization/EntitySerialization.java @@ -108,6 +108,12 @@ public class EntitySerialization implements EntitySerializationAPI { return createGsonForSerialization(view, options).toJson(entities); } + @Override + public String objectToJson(Object object, EntitySerializationOption... options) { + context.remove(); + return createGsonForSerialization(null, options).toJson(object); + } + @Override public Entity entityFromJson(String json, @Nullable MetaClass metaClass, diff --git a/modules/global/src/com/haulmont/cuba/core/app/serialization/EntitySerializationAPI.java b/modules/global/src/com/haulmont/cuba/core/app/serialization/EntitySerializationAPI.java index 8f92f62a61..bde9c06af6 100644 --- a/modules/global/src/com/haulmont/cuba/core/app/serialization/EntitySerializationAPI.java +++ b/modules/global/src/com/haulmont/cuba/core/app/serialization/EntitySerializationAPI.java @@ -53,7 +53,7 @@ public interface EntitySerializationAPI { EntitySerializationOption... options); /** - * Serialized a collection of entities to the JSON array. Method works like the {@link #toJson(Entity, View, + * Serializes a collection of entities to the JSON array. Method works like the {@link #toJson(Entity, View, * EntitySerializationOption...)}, but return a JSON array as a result. * * @param entities a list of entities to be serialized @@ -83,6 +83,15 @@ public interface EntitySerializationAPI { */ String toJson(Collection entities); + /** + * Serializes any custom POJO or collection of POJOs or JSON. If some field in POJO is an entity then this field + * will be serialized according to entity serialization rules + * + * @param object any POJO or collection of POJOs + * @param options options specifying how a JSON object graph for fields with type 'Entity' will be serialized + * @return a string that represents a JSON object or JSON array + */ + String objectToJson(Object object, EntitySerializationOption... options); /** * Deserializes a JSON object to the entity. diff --git a/modules/rest-api/src/com/haulmont/restapi/common/RestParseUtils.java b/modules/rest-api/src/com/haulmont/restapi/common/RestParseUtils.java index c70c631db4..0f4fe956d6 100644 --- a/modules/rest-api/src/com/haulmont/restapi/common/RestParseUtils.java +++ b/modules/rest-api/src/com/haulmont/restapi/common/RestParseUtils.java @@ -123,14 +123,8 @@ public class RestParseUtils { return gson.fromJson(json, clazz); } - public String serializePOJO(Object pojoInstance, Class clazz) { - Gson gson = new Gson(); - return gson.toJson(pojoInstance, clazz); - } - public String serialize(Object instance) { - Gson gson = new Gson(); - return gson.toJson(instance); + return entitySerializationAPI.objectToJson(instance); } public Map parseParamsJson(String paramsJson) { diff --git a/modules/rest-api/src/com/haulmont/restapi/service/ServicesControllerManager.java b/modules/rest-api/src/com/haulmont/restapi/service/ServicesControllerManager.java index 88d9bb371e..ec62fbedb0 100644 --- a/modules/rest-api/src/com/haulmont/restapi/service/ServicesControllerManager.java +++ b/modules/rest-api/src/com/haulmont/restapi/service/ServicesControllerManager.java @@ -180,7 +180,7 @@ public class ServicesControllerManager { if (datatype != null) { return new ServiceCallResult(datatype.format(methodResult), false); } else { - return new ServiceCallResult(restParseUtils.serializePOJO(methodResult, methodReturnType), true); + return new ServiceCallResult(restParseUtils.serialize(methodResult), true); } } }