Repository: olingo-odata2 Updated Branches: refs/heads/master d8afd0183 -> d7c682400
[OLINGO-308] fix for json Project: http://git-wip-us.apache.org/repos/asf/olingo-odata2/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata2/commit/d7c68240 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata2/tree/d7c68240 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata2/diff/d7c68240 Branch: refs/heads/master Commit: d7c682400384c8388bcb0025a98376729164d3c4 Parents: d8afd01 Author: Stephan Klevenz <[email protected]> Authored: Wed May 28 16:45:16 2014 +0200 Committer: Stephan Klevenz <[email protected]> Committed: Wed May 28 16:45:16 2014 +0200 ---------------------------------------------------------------------- .../ep/producer/JsonEntryEntityProducer.java | 35 +++++++++++++------- .../producer/JsonEntryEntityProducerTest.java | 25 ++++++++++++++ 2 files changed, 48 insertions(+), 12 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/d7c68240/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonEntryEntityProducer.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonEntryEntityProducer.java b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonEntryEntityProducer.java index 8ad5d42..c1b52fa 100644 --- a/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonEntryEntityProducer.java +++ b/odata2-lib/odata-core/src/main/java/org/apache/olingo/odata2/core/ep/producer/JsonEntryEntityProducer.java @@ -45,6 +45,7 @@ import org.apache.olingo.odata2.api.exception.ODataApplicationException; import org.apache.olingo.odata2.core.commons.ContentType; import org.apache.olingo.odata2.core.commons.Encoder; import org.apache.olingo.odata2.core.ep.aggregator.EntityInfoAggregator; +import org.apache.olingo.odata2.core.ep.aggregator.EntityPropertyInfo; import org.apache.olingo.odata2.core.ep.util.FormatJson; import org.apache.olingo.odata2.core.ep.util.JsonStreamWriter; @@ -80,8 +81,10 @@ public class JsonEntryEntityProducer { writeProperties(entityInfo, data, type); - writeNavigationProperties(writer, entityInfo, data, type); - + if (!properties.isIgnoreKey()) { + writeNavigationProperties(writer, entityInfo, data, type); + } + jsonStreamWriter.endObject(); if (isRootElement) { @@ -175,10 +178,15 @@ public class JsonEntryEntityProducer { final EdmEntityType type) throws EdmException, EntityProviderException, IOException { for (final String propertyName : type.getPropertyNames()) { if (entityInfo.getSelectedPropertyNames().contains(propertyName)) { - jsonStreamWriter.separator(); - jsonStreamWriter.name(propertyName); - JsonPropertyEntityProducer.appendPropertyValue(jsonStreamWriter, entityInfo.getPropertyInfo(propertyName), - data.get(propertyName)); + + EntityPropertyInfo propertyInfo = entityInfo.getPropertyInfo(propertyName); + if (!(entityInfo.getKeyPropertyInfos().contains(propertyInfo) && properties.isIgnoreKey())) { + + jsonStreamWriter.separator(); + jsonStreamWriter.name(propertyName); + JsonPropertyEntityProducer.appendPropertyValue(jsonStreamWriter, entityInfo.getPropertyInfo(propertyName), + data.get(propertyName)); + } } } } @@ -187,12 +195,15 @@ public class JsonEntryEntityProducer { final EdmEntityType type) throws IOException, EntityProviderException, EdmException { jsonStreamWriter.name(FormatJson.METADATA); jsonStreamWriter.beginObject(); - final String self = AtomEntryEntityProducer.createSelfLink(entityInfo, data, null); - location = (properties.getServiceRoot() == null ? "" : properties.getServiceRoot().toASCIIString()) + self; - jsonStreamWriter.namedStringValue(FormatJson.ID, location); - jsonStreamWriter.separator(); - jsonStreamWriter.namedStringValue(FormatJson.URI, location); - jsonStreamWriter.separator(); + String self = null; + if (!properties.isIgnoreKey()) { + self = AtomEntryEntityProducer.createSelfLink(entityInfo, data, null); + location = (properties.getServiceRoot() == null ? "" : properties.getServiceRoot().toASCIIString()) + self; + jsonStreamWriter.namedStringValue(FormatJson.ID, location); + jsonStreamWriter.separator(); + jsonStreamWriter.namedStringValue(FormatJson.URI, location); + jsonStreamWriter.separator(); + } jsonStreamWriter.namedStringValueRaw(FormatJson.TYPE, type.getNamespace() + Edm.DELIMITER + type.getName()); eTag = AtomEntryEntityProducer.createETag(entityInfo, data); if (eTag != null) { http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/d7c68240/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonEntryEntityProducerTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonEntryEntityProducerTest.java b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonEntryEntityProducerTest.java index 8efd99a..7593349 100644 --- a/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonEntryEntityProducerTest.java +++ b/odata2-lib/odata-core/src/test/java/org/apache/olingo/odata2/core/ep/producer/JsonEntryEntityProducerTest.java @@ -18,6 +18,8 @@ ******************************************************************************/ package org.apache.olingo.odata2.core.ep.producer; +import static org.custommonkey.xmlunit.XMLAssert.assertXpathEvaluatesTo; +import static org.custommonkey.xmlunit.XMLAssert.assertXpathExists; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -53,6 +55,8 @@ import org.apache.olingo.odata2.api.ep.callback.WriteFeedCallbackResult; import org.apache.olingo.odata2.api.exception.ODataApplicationException; import org.apache.olingo.odata2.api.processor.ODataResponse; import org.apache.olingo.odata2.api.uri.ExpandSelectTreeNode; +import org.apache.olingo.odata2.core.commons.ContentType; +import org.apache.olingo.odata2.core.ep.AtomEntityProvider; import org.apache.olingo.odata2.core.ep.JsonEntityProvider; import org.apache.olingo.odata2.testutil.fit.BaseTest; import org.apache.olingo.odata2.testutil.helper.StringHelper; @@ -770,4 +774,25 @@ public class JsonEntryEntityProducerTest extends BaseTest { assertNotNull(json); return json; } + + @Test + public void testPostEntryWithoutId() throws Exception { + HashMap<String, Object> roomData = new HashMap<String, Object>(); + + roomData.put("Name", "Neu Schwanstein"); + roomData.put("Seats", new Integer(20)); + roomData.put("Version", new Integer(3)); + + final EntityProviderWriteProperties properties = + EntityProviderWriteProperties.serviceRoot(URI.create(BASE_URI)).ignoreKey(true).build(); + ODataResponse response = + new JsonEntityProvider().writeEntry(MockFacade.getMockEdm().getDefaultEntityContainer().getEntitySet("Rooms"), + roomData, properties); + + String json = verifyResponse(response); + + assertEquals("{\"d\":{\"__metadata\":{\"type\":\"RefScenario.Room\",\"etag\":\"W/\\\"3\\\"\"}," + + "\"Name\":\"Neu Schwanstein\",\"Seats\":20,\"Version\":3}}", json); + } + }
