[OLINGO-1237]Serialization changes for odata.metadata=minimal

This commit is contained in:
Archana Rai 2018-02-22 11:59:49 +05:30
parent 48f93fd871
commit 75cc7197e3
2 changed files with 308 additions and 12 deletions

View File

@ -66,10 +66,12 @@ public class EdmAssistedJsonSerializer implements EdmAssistedSerializer {
protected final boolean isIEEE754Compatible; protected final boolean isIEEE754Compatible;
protected final boolean isODataMetadataNone; protected final boolean isODataMetadataNone;
protected final boolean isODataMetadataFull;
public EdmAssistedJsonSerializer(final ContentType contentType) { public EdmAssistedJsonSerializer(final ContentType contentType) {
this.isIEEE754Compatible = ContentTypeHelper.isODataIEEE754Compatible(contentType); this.isIEEE754Compatible = ContentTypeHelper.isODataIEEE754Compatible(contentType);
this.isODataMetadataNone = ContentTypeHelper.isODataMetadataNone(contentType); this.isODataMetadataNone = ContentTypeHelper.isODataMetadataNone(contentType);
this.isODataMetadataFull = ContentTypeHelper.isODataMetadataFull(contentType);
} }
@Override @Override
@ -207,15 +209,17 @@ public class EdmAssistedJsonSerializer implements EdmAssistedSerializer {
if (eTag != null) { if (eTag != null) {
json.writeStringField(Constants.JSON_ETAG, eTag); json.writeStringField(Constants.JSON_ETAG, eTag);
} }
if (type != null) { if(isODataMetadataFull){
json.writeStringField(Constants.JSON_TYPE, type); if (type != null) {
} json.writeStringField(Constants.JSON_TYPE, type);
if (id == null) { }
if (writeNullId) { if (id == null) {
json.writeNullField(Constants.JSON_ID); if (writeNullId) {
json.writeNullField(Constants.JSON_ID);
}
} else {
json.writeStringField(Constants.JSON_ID, id.toASCIIString());
} }
} else {
json.writeStringField(Constants.JSON_ID, id.toASCIIString());
} }
} }
} }
@ -326,7 +330,7 @@ public class EdmAssistedJsonSerializer implements EdmAssistedSerializer {
final ComplexValue value) throws IOException, SerializerException { final ComplexValue value) throws IOException, SerializerException {
json.writeStartObject(); json.writeStartObject();
if (typeName != null && !isODataMetadataNone) { if (typeName != null && isODataMetadataFull) {
json.writeStringField(Constants.JSON_TYPE, typeName); json.writeStringField(Constants.JSON_TYPE, typeName);
} }
@ -362,7 +366,7 @@ public class EdmAssistedJsonSerializer implements EdmAssistedSerializer {
protected void valuable(JsonGenerator json, final Valuable valuable, final String name, final EdmType type, protected void valuable(JsonGenerator json, final Valuable valuable, final String name, final EdmType type,
final EdmProperty edmProperty) throws IOException, SerializerException { final EdmProperty edmProperty) throws IOException, SerializerException {
if (!isODataMetadataNone if (isODataMetadataFull
&& !(valuable instanceof Annotation) && !valuable.isComplex()) { && !(valuable instanceof Annotation) && !valuable.isComplex()) {
String typeName = valuable.getType(); String typeName = valuable.getType();

View File

@ -56,9 +56,13 @@ public class EdmAssistedJsonSerializerTest {
new EdmTechProvider(), Collections.<EdmxReference> emptyList(), null); new EdmTechProvider(), Collections.<EdmxReference> emptyList(), null);
private static final EdmEntityContainer entityContainer = metadata.getEdm().getEntityContainer(); private static final EdmEntityContainer entityContainer = metadata.getEdm().getEntityContainer();
private final EdmAssistedSerializer serializer; private final EdmAssistedSerializer serializer;
private final EdmAssistedSerializer serializerMin;
private final EdmAssistedSerializer serializerNone;
public EdmAssistedJsonSerializerTest() throws SerializerException { public EdmAssistedJsonSerializerTest() throws SerializerException {
serializer = oData.createEdmAssistedSerializer(ContentType.JSON); serializer = oData.createEdmAssistedSerializer(ContentType.JSON_FULL_METADATA);
serializerMin = oData.createEdmAssistedSerializer(ContentType.JSON);
serializerNone = oData.createEdmAssistedSerializer(ContentType.JSON_NO_METADATA);
} }
@Test @Test
@ -136,7 +140,7 @@ public class EdmAssistedJsonSerializerTest {
+ "\"Property3@odata.type\":\"#Byte\",\"Property3\":20}]}", + "\"Property3@odata.type\":\"#Byte\",\"Property3\":20}]}",
serialize( serialize(
oData.createEdmAssistedSerializer( oData.createEdmAssistedSerializer(
ContentType.create(ContentType.JSON, ContentType.PARAMETER_IEEE754_COMPATIBLE, "true")), ContentType.create(ContentType.JSON_FULL_METADATA, ContentType.PARAMETER_IEEE754_COMPATIBLE, "true")),
metadata, null, entityCollection, null)); metadata, null, entityCollection, null));
} }
@ -373,4 +377,292 @@ public class EdmAssistedJsonSerializerTest {
EdmAssistedSerializerOptions.with().contextURL(contextURLBuilder.build()).build()) EdmAssistedSerializerOptions.with().contextURL(contextURLBuilder.build()).build())
.getContent()); .getContent());
} }
@Test
public void entityCollectionSimpleMetadataMin() throws Exception {
Entity entity = new Entity();
entity.setId(null);
entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE, 1.25F));
EntityCollection entityCollection = new EntityCollection();
entityCollection.getEntities().add(entity);
Assert.assertEquals("{\"@odata.context\":\"$metadata#EntitySet(Property1)\","
+ "\"value\":[{\"Property1\":1.25}]}",
serialize(serializerMin, metadata, null, entityCollection, null));
}
@Test
public void entityCollectionSimpleMetadataNone() throws Exception {
Entity entity = new Entity();
entity.setId(null);
entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE, 1.25F));
EntityCollection entityCollection = new EntityCollection();
entityCollection.getEntities().add(entity);
Assert.assertEquals("{\"value\":[{\"Property1\":1.25}]}",
serialize(serializerNone, metadata, null, entityCollection, null));
}
@Test
public void entityCollectionMetadataMin() throws Exception {
Entity entity = new Entity();
entity.setId(null);
entity.addProperty(new Property(null, "Property0", ValueType.PRIMITIVE, null))
.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE, 1));
Calendar date = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
date.clear();
date.set(2000, 1, 29);
entity.addProperty(new Property("Edm.Date", "Property2", ValueType.PRIMITIVE, date))
.addProperty(new Property("Edm.DateTimeOffset", "Property3", ValueType.PRIMITIVE, date))
.addProperty(new Property(null, "Property4", ValueType.COLLECTION_PRIMITIVE,
Arrays.asList(true, false, null)));
EntityCollection entityCollection = new EntityCollection();
entityCollection.getEntities().add(entity);
entityCollection.setCount(2);
entityCollection.setNext(URI.create("nextLink"));
Assert.assertEquals(
"{\"@odata.context\":\"$metadata#EntitySet(Property0,Property1,Property2,Property3,Property4)\","
+ "\"@odata.count\":2,"
+ "\"value\":[{"
+ "\"Property0\":null,"
+ "\"Property1\":1,"
+ "\"Property2\":\"2000-02-29\","
+ "\"Property3\":\"2000-02-29T00:00:00Z\","
+ "\"Property4\":[true,false,null]}],"
+ "\"@odata.nextLink\":\"nextLink\"}",
serialize(serializerMin, metadata, null, entityCollection, null));
}
@Test
public void entityCollectionMetadataNone() throws Exception {
Entity entity = new Entity();
entity.setId(null);
entity.addProperty(new Property(null, "Property0", ValueType.PRIMITIVE, null))
.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE, 1));
Calendar date = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
date.clear();
date.set(2000, 1, 29);
entity.addProperty(new Property("Edm.Date", "Property2", ValueType.PRIMITIVE, date))
.addProperty(new Property("Edm.DateTimeOffset", "Property3", ValueType.PRIMITIVE, date))
.addProperty(new Property(null, "Property4", ValueType.COLLECTION_PRIMITIVE,
Arrays.asList(true, false, null)));
EntityCollection entityCollection = new EntityCollection();
entityCollection.getEntities().add(entity);
entityCollection.setCount(2);
entityCollection.setNext(URI.create("nextLink"));
Assert.assertEquals(
"{"
+ "\"@odata.count\":2,"
+ "\"value\":[{"
+ "\"Property0\":null,"
+ "\"Property1\":1,"
+ "\"Property2\":\"2000-02-29\","
+ "\"Property3\":\"2000-02-29T00:00:00Z\","
+ "\"Property4\":[true,false,null]}],"
+ "\"@odata.nextLink\":\"nextLink\"}",
serialize(serializerNone, metadata, null, entityCollection, null));
}
@Test
public void entityCollectionWithComplexPropertyMetadataMin() throws Exception {
Entity entity = new Entity();
entity.setId(null);
entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE, 1L));
ComplexValue complexValue = new ComplexValue();
complexValue.getValue().add(new Property(null, "Inner1", ValueType.PRIMITIVE,
BigDecimal.TEN.scaleByPowerOfTen(-5)));
Calendar time = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
time.clear();
time.set(Calendar.HOUR_OF_DAY, 13);
time.set(Calendar.SECOND, 59);
time.set(Calendar.MILLISECOND, 999);
complexValue.getValue().add(new Property("Edm.TimeOfDay", "Inner2", ValueType.PRIMITIVE, time));
entity.addProperty(new Property("Namespace.ComplexType", "Property2", ValueType.COMPLEX, complexValue));
EntityCollection entityCollection = new EntityCollection();
entityCollection.getEntities().add(entity);
Assert.assertEquals("{\"@odata.context\":\"$metadata#EntitySet(Property1,Property2)\","
+ "\"value\":[{"
+ "\"Property1\":1,"
+ "\"Property2\":{"
+ "\"Inner1\":0.00010,"
+ "\"Inner2\":\"13:00:59.999\"}}]}",
serialize(serializerMin, metadata, null, entityCollection, null));
}
@Test
public void entityCollectionWithComplexPropertyMetadataNone() throws Exception {
Entity entity = new Entity();
entity.setId(null);
entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE, 1L));
ComplexValue complexValue = new ComplexValue();
complexValue.getValue().add(new Property(null, "Inner1", ValueType.PRIMITIVE,
BigDecimal.TEN.scaleByPowerOfTen(-5)));
Calendar time = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
time.clear();
time.set(Calendar.HOUR_OF_DAY, 13);
time.set(Calendar.SECOND, 59);
time.set(Calendar.MILLISECOND, 999);
complexValue.getValue().add(new Property("Edm.TimeOfDay", "Inner2", ValueType.PRIMITIVE, time));
entity.addProperty(new Property("Namespace.ComplexType", "Property2", ValueType.COMPLEX, complexValue));
EntityCollection entityCollection = new EntityCollection();
entityCollection.getEntities().add(entity);
Assert.assertEquals("{"
+ "\"value\":[{"
+ "\"Property1\":1,"
+ "\"Property2\":{"
+ "\"Inner1\":0.00010,"
+ "\"Inner2\":\"13:00:59.999\"}}]}",
serialize(serializerNone, metadata, null, entityCollection, null));
}
@Test
public void entityCollectionWithComplexCollectionMin() throws Exception {
final EdmEntitySet entitySet = entityContainer.getEntitySet("ESMixPrimCollComp");
ComplexValue complexValue1 = new ComplexValue();
complexValue1.getValue().add(new Property(null, "PropertyInt16", ValueType.PRIMITIVE, 1));
complexValue1.getValue().add(new Property(null, "PropertyString", ValueType.PRIMITIVE, "one"));
ComplexValue complexValue2 = new ComplexValue();
complexValue2.getValue().add(new Property(null, "PropertyInt16", ValueType.PRIMITIVE, 2));
complexValue2.getValue().add(new Property(null, "PropertyString", ValueType.PRIMITIVE, "two"));
ComplexValue complexValue3 = new ComplexValue();
complexValue3.getValue().add(new Property(null, "PropertyInt16", ValueType.PRIMITIVE, 3));
complexValue3.getValue().add(new Property(null, "PropertyString", ValueType.PRIMITIVE, "three"));
EntityCollection entityCollection = new EntityCollection();
entityCollection.getEntities().add(new Entity()
.addProperty(new Property(null, "CollPropertyComp", ValueType.COLLECTION_COMPLEX,
Arrays.asList(complexValue1, complexValue2, complexValue3))));
Assert.assertEquals("{\"@odata.context\":\"$metadata#ESMixPrimCollComp(CollPropertyComp)\","
+ "\"value\":[{"
+ "\"CollPropertyComp\":["
+ "{\"PropertyInt16\":1,\"PropertyString\":\"one\"},"
+ "{\"PropertyInt16\":2,\"PropertyString\":\"two\"},"
+ "{\"PropertyInt16\":3,\"PropertyString\":\"three\"}]}]}",
serialize(serializerMin, metadata, entitySet, entityCollection, "CollPropertyComp"));
}
@Test
public void entityCollectionWithComplexCollectionNone() throws Exception {
final EdmEntitySet entitySet = entityContainer.getEntitySet("ESMixPrimCollComp");
ComplexValue complexValue1 = new ComplexValue();
complexValue1.getValue().add(new Property(null, "PropertyInt16", ValueType.PRIMITIVE, 1));
complexValue1.getValue().add(new Property(null, "PropertyString", ValueType.PRIMITIVE, "one"));
ComplexValue complexValue2 = new ComplexValue();
complexValue2.getValue().add(new Property(null, "PropertyInt16", ValueType.PRIMITIVE, 2));
complexValue2.getValue().add(new Property(null, "PropertyString", ValueType.PRIMITIVE, "two"));
ComplexValue complexValue3 = new ComplexValue();
complexValue3.getValue().add(new Property(null, "PropertyInt16", ValueType.PRIMITIVE, 3));
complexValue3.getValue().add(new Property(null, "PropertyString", ValueType.PRIMITIVE, "three"));
EntityCollection entityCollection = new EntityCollection();
entityCollection.getEntities().add(new Entity()
.addProperty(new Property(null, "CollPropertyComp", ValueType.COLLECTION_COMPLEX,
Arrays.asList(complexValue1, complexValue2, complexValue3))));
Assert.assertEquals("{"
+ "\"value\":[{"
+ "\"CollPropertyComp\":["
+ "{\"PropertyInt16\":1,\"PropertyString\":\"one\"},"
+ "{\"PropertyInt16\":2,\"PropertyString\":\"two\"},"
+ "{\"PropertyInt16\":3,\"PropertyString\":\"three\"}]}]}",
serialize(serializerNone, metadata, entitySet, entityCollection, "CollPropertyComp"));
}
@Test
public void entityCollectionWithEmptyCollectionMin() throws Exception {
final EdmEntitySet entitySet = entityContainer.getEntitySet("ESMixPrimCollComp");
EntityCollection entityCollection = new EntityCollection();
entityCollection.getEntities().add(new Entity()
.addProperty(new Property(null, "CollPropertyString", ValueType.COLLECTION_PRIMITIVE,
Collections.emptyList())));
Assert.assertEquals(
"{\"@odata.context\":\"$metadata#ESMixPrimCollComp(CollPropertyString)\","
+ "\"value\":[{\"CollPropertyString\":[]}]}",
serialize(serializerMin, metadata, entitySet, entityCollection, "CollPropertyString"));
}
@Test
public void entityCollectionWithEmptyCollectionNone() throws Exception {
final EdmEntitySet entitySet = entityContainer.getEntitySet("ESMixPrimCollComp");
EntityCollection entityCollection = new EntityCollection();
entityCollection.getEntities().add(new Entity()
.addProperty(new Property(null, "CollPropertyString", ValueType.COLLECTION_PRIMITIVE,
Collections.emptyList())));
Assert.assertEquals(
"{"
+ "\"value\":[{\"CollPropertyString\":[]}]}",
serialize(serializerNone, metadata, entitySet, entityCollection, "CollPropertyString"));
}
@Test
public void expandMetadataMin() throws Exception {
final Entity relatedEntity1 = new Entity().addProperty(new Property(null, "Related1", ValueType.PRIMITIVE, 1.5));
final Entity relatedEntity2 = new Entity().addProperty(new Property(null, "Related1", ValueType.PRIMITIVE, 2.75));
EntityCollection target = new EntityCollection();
target.getEntities().add(relatedEntity1);
target.getEntities().add(relatedEntity2);
Link link = new Link();
link.setTitle("NavigationProperty");
link.setInlineEntitySet(target);
Entity entity = new Entity();
entity.setId(null);
entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE, (short) 1));
entity.getNavigationLinks().add(link);
EntityCollection entityCollection = new EntityCollection();
entityCollection.getEntities().add(entity);
Assert.assertEquals("{\"@odata.context\":\"$metadata#EntitySet(Property1,NavigationProperty(Related1))\","
+ "\"value\":[{"
+ "\"Property1\":1,"
+ "\"NavigationProperty\":["
+ "{\"Related1\":1.5},"
+ "{\"Related1\":2.75}]}]}",
serialize(serializerMin, metadata, null, entityCollection, "Property1,NavigationProperty(Related1)"));
}
@Test
public void expandMetadataNone() throws Exception {
final Entity relatedEntity1 = new Entity().addProperty(new Property(null, "Related1", ValueType.PRIMITIVE, 1.5));
final Entity relatedEntity2 = new Entity().addProperty(new Property(null, "Related1", ValueType.PRIMITIVE, 2.75));
EntityCollection target = new EntityCollection();
target.getEntities().add(relatedEntity1);
target.getEntities().add(relatedEntity2);
Link link = new Link();
link.setTitle("NavigationProperty");
link.setInlineEntitySet(target);
Entity entity = new Entity();
entity.setId(null);
entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE, (short) 1));
entity.getNavigationLinks().add(link);
EntityCollection entityCollection = new EntityCollection();
entityCollection.getEntities().add(entity);
Assert.assertEquals("{"
+ "\"value\":[{"
+ "\"Property1\":1,"
+ "\"NavigationProperty\":["
+ "{\"Related1\":1.5},"
+ "{\"Related1\":2.75}]}]}",
serialize(serializerNone, metadata, null, entityCollection, "Property1,NavigationProperty(Related1)"));
}
@Test
public void metadataMin() throws Exception {
final ServiceMetadata metadata = oData.createServiceMetadata(null, Collections.<EdmxReference> emptyList(),
new MetadataETagSupport("W/\"42\""));
Entity entity = new Entity();
entity.setType("Namespace.EntityType");
entity.setId(URI.create("ID"));
entity.setETag("W/\"1000\"");
Link link = new Link();
link.setHref("editLink");
entity.setEditLink(link);
entity.setMediaContentSource(URI.create("media"));
entity.addProperty(new Property(null, "Property1", ValueType.PRIMITIVE,
UUID.fromString("12345678-ABCD-1234-CDEF-123456789012")));
EntityCollection entityCollection = new EntityCollection();
entityCollection.getEntities().add(entity);
Assert.assertEquals("{\"@odata.context\":\"$metadata#EntitySet(Property1)\","
+ "\"@odata.metadataEtag\":\"W/\\\"42\\\"\",\"value\":[{"
+ "\"@odata.etag\":\"W/\\\"1000\\\"\","
+ "\"Property1\":\"12345678-abcd-1234-cdef-123456789012\","
+ "\"@odata.editLink\":\"editLink\","
+ "\"@odata.mediaReadLink\":\"editLink/$value\"}]}",
serialize(serializerMin, metadata, null, entityCollection, null));
}
} }