Repository: olingo-odata4 Updated Branches: refs/heads/master 3190d00fd -> 0d37336eb
OLINGO-914: Adding @odata.id annotation to the JSON payload, when a request for enitity or entities came with option which did not include all the key predicate names according the specification Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/96bdba81 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/96bdba81 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/96bdba81 Branch: refs/heads/master Commit: 96bdba81bb0818f2fe7691c2140c81879594686d Parents: 9658091 Author: Ramesh Reddy <[email protected]> Authored: Fri Mar 25 10:31:49 2016 -0500 Committer: Ramesh Reddy <[email protected]> Committed: Fri Mar 25 10:31:49 2016 -0500 ---------------------------------------------------------------------- .../serializer/json/ODataJsonSerializer.java | 16 ++++++++++++++ .../json/ODataJsonSerializerTest.java | 22 +++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/96bdba81/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java index 904bc9a..1b22501 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializer.java @@ -277,6 +277,19 @@ public class ODataJsonSerializer extends AbstractODataSerializer { json.writeEndArray(); } + private boolean areKeyPredicateNamesSelected(SelectOption select, EdmEntityType type) { + if (select == null || ExpandSelectHelper.isAll(select)) { + return true; + } + final Set<String> selected = ExpandSelectHelper.getSelectedPropertyNames(select.getSelectItems()); + for (String key: type.getKeyPredicateNames()) { + if (!selected.contains(key)) { + return false; + } + } + return true; + } + public void writeEntity(final ServiceMetadata metadata, final EdmEntityType entityType, final Entity entity, final ContextURL contextURL, final ExpandOption expand, final SelectOption select, final boolean onlyReference, final JsonGenerator json) @@ -312,6 +325,9 @@ public class ODataJsonSerializer extends AbstractODataSerializer { if (!isODataMetadataNone && !resolvedType.equals(entityType)) { json.writeStringField(Constants.JSON_TYPE, "#" + entity.getType()); } + if (!isODataMetadataNone && !areKeyPredicateNamesSelected(select, resolvedType)) { + json.writeStringField(Constants.JSON_ID, entity.getId().toASCIIString()); + } writeProperties(metadata, resolvedType, entity.getProperties(), select, json); writeNavigationProperties(metadata, resolvedType, entity, expand, json); json.writeEndObject(); http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/96bdba81/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java index 76ba154..3754da1 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java @@ -610,6 +610,7 @@ public class ODataJsonSerializerTest { final String expectedResult = "{" + "\"@odata.context\":\"$metadata#ESAllPrim(PropertyBoolean,PropertyDate)/$entity\"," + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"@odata.id\":\"ESAllPrim(32767)\"," + "\"PropertyBoolean\":true,\"PropertyDate\":\"2012-12-03\"}"; Assert.assertEquals(expectedResult, resultString); } @@ -654,8 +655,10 @@ public class ODataJsonSerializerTest { + "\"@odata.context\":\"$metadata#ESCompComp(PropertyComp/PropertyComp/PropertyString)\"," + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\"," + "\"value\":[" - + "{\"PropertyComp\":{\"PropertyComp\":{\"PropertyString\":\"String 1\"}}}," - + "{\"PropertyComp\":{\"@odata.type\":\"#olingo.odata.test1.CTCompCompExtended\"," + + "{\"@odata.id\":\"ESCompComp(1)\",\"PropertyComp\":" + + "{\"PropertyComp\":{\"PropertyString\":\"String 1\"}}}," + + "{\"@odata.id\":\"ESCompComp(2)\",\"PropertyComp\":" + + "{\"@odata.type\":\"#olingo.odata.test1.CTCompCompExtended\"," + "\"PropertyComp\":{\"PropertyString\":\"String 2\"}}}]}", resultString); } @@ -708,8 +711,10 @@ public class ODataJsonSerializerTest { + "\"@odata.context\":\"$metadata#ESCompComp(PropertyComp/PropertyComp)\"," + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\"," + "\"value\":[" - + "{\"PropertyComp\":{\"PropertyComp\":{\"PropertyInt16\":123,\"PropertyString\":\"String 1\"}}}," - + "{\"PropertyComp\":{\"@odata.type\":\"#olingo.odata.test1.CTCompCompExtended\"," + + "{\"@odata.id\":\"ESCompComp(1)\",\"PropertyComp\":{\"PropertyComp\":{\"PropertyInt16\":123," + + "\"PropertyString\":\"String 1\"}}}," + + "{\"@odata.id\":\"ESCompComp(2)\",\"PropertyComp\":" + + "{\"@odata.type\":\"#olingo.odata.test1.CTCompCompExtended\"," + "\"PropertyComp\":{\"PropertyInt16\":987,\"PropertyString\":\"String 2\"}}}]}", resultString); } @@ -771,7 +776,7 @@ public class ODataJsonSerializerTest { + "\"@odata.context\":\"$metadata#ESTwoPrim(NavPropertyETAllPrimOne(PropertyDate))/$entity\"," + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\"," + "\"PropertyInt16\":32767,\"PropertyString\":\"Test String4\"," - + "\"NavPropertyETAllPrimOne\":{\"PropertyDate\":\"2012-12-03\"}}", + + "\"NavPropertyETAllPrimOne\":{\"@odata.id\":\"ESAllPrim(32767)\",\"PropertyDate\":\"2012-12-03\"}}", resultString); } @@ -799,6 +804,7 @@ public class ODataJsonSerializerTest { Assert.assertEquals("{" + "\"@odata.context\":\"$metadata#ESAllPrim(PropertySByte)/$entity\"," + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"@odata.id\":\"ESAllPrim(32767)\"," + "\"PropertySByte\":127," + "\"NavPropertyETTwoPrimOne\":{\"PropertyInt16\":32767,\"PropertyString\":\"Test String4\"}," + "\"NavPropertyETTwoPrimMany\":[{\"PropertyInt16\":-365,\"PropertyString\":\"Test String2\"}]}", @@ -827,6 +833,7 @@ public class ODataJsonSerializerTest { Assert.assertEquals("{" + "\"@odata.context\":\"$metadata#ESAllPrim(PropertyTimeOfDay)/$entity\"," + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"@odata.id\":\"ESAllPrim(-32768)\"," + "\"PropertyTimeOfDay\":\"23:49:14\"," + "\"NavPropertyETTwoPrimOne\":null,\"NavPropertyETTwoPrimMany\":[]}", resultString); @@ -860,8 +867,9 @@ public class ODataJsonSerializerTest { + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\"," + "\"PropertyInt16\":-365,\"PropertyString\":\"Test String2\"," + "\"NavPropertyETAllPrimMany\":[" - + "{\"PropertyInt32\":-2147483648,\"NavPropertyETTwoPrimOne\":null,\"NavPropertyETTwoPrimMany\":[]}," - + "{\"PropertyInt32\":0,\"NavPropertyETTwoPrimOne\":null," + + "{\"@odata.id\":\"ESAllPrim(-32768)\",\"PropertyInt32\":-2147483648," + + "\"NavPropertyETTwoPrimOne\":null,\"NavPropertyETTwoPrimMany\":[]}," + + "{\"@odata.id\":\"ESAllPrim(0)\",\"PropertyInt32\":0,\"NavPropertyETTwoPrimOne\":null," + "\"NavPropertyETTwoPrimMany\":[" + "{\"PropertyInt16\":32766,\"PropertyString\":\"Test String1\"}," + "{\"PropertyInt16\":-32766,\"PropertyString\":null},"
