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
This commit is contained in:
parent
9658091ae1
commit
96bdba81bb
|
@ -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();
|
||||
|
|
|
@ -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},"
|
||||
|
|
Loading…
Reference in New Issue