diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerImpl.java index 705c76718..da4856a09 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ODataHandlerImpl.java @@ -18,7 +18,6 @@ */ package org.apache.olingo.server.core; -import java.util.Collections; import java.util.LinkedList; import java.util.List; @@ -47,12 +46,9 @@ import org.apache.olingo.server.api.serializer.RepresentationType; import org.apache.olingo.server.api.serializer.SerializerException; import org.apache.olingo.server.api.uri.UriInfo; import org.apache.olingo.server.api.uri.queryoption.FormatOption; -import org.apache.olingo.server.api.uri.queryoption.QueryOption; -import org.apache.olingo.server.api.uri.queryoption.SystemQueryOption; import org.apache.olingo.server.api.uri.queryoption.SystemQueryOptionKind; import org.apache.olingo.server.core.debug.ServerCoreDebugger; import org.apache.olingo.server.core.uri.parser.Parser; -import org.apache.olingo.server.core.uri.parser.UriDecoder; import org.apache.olingo.server.core.uri.parser.UriParserException; import org.apache.olingo.server.core.uri.parser.UriParserSemanticException; import org.apache.olingo.server.core.uri.parser.UriParserSyntaxException; diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchFacadeImpl.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchFacadeImpl.java index 4d5a77d68..ea7a72d41 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchFacadeImpl.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchFacadeImpl.java @@ -28,7 +28,6 @@ import org.apache.olingo.server.api.deserializer.batch.BatchDeserializerExceptio import org.apache.olingo.server.api.deserializer.batch.BatchRequestPart; import org.apache.olingo.server.api.deserializer.batch.ODataResponsePart; import org.apache.olingo.server.api.processor.BatchProcessor; -import org.apache.olingo.server.core.ODataHandlerImpl; import org.apache.olingo.server.core.deserializer.batch.BatchParserCommon; public class BatchFacadeImpl implements BatchFacade { diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchPartHandler.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchPartHandler.java index a40006aa3..e83e9455f 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchPartHandler.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/batchhandler/BatchPartHandler.java @@ -29,7 +29,6 @@ import org.apache.olingo.server.api.deserializer.batch.BatchDeserializerExceptio import org.apache.olingo.server.api.deserializer.batch.BatchRequestPart; import org.apache.olingo.server.api.deserializer.batch.ODataResponsePart; import org.apache.olingo.server.api.processor.BatchProcessor; -import org.apache.olingo.server.core.ODataHandlerImpl; import org.apache.olingo.server.core.batchhandler.referenceRewriting.BatchReferenceRewriter; public class BatchPartHandler { diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/ParserHelper.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/ParserHelper.java index e42a511ff..0ca0ce6e2 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/ParserHelper.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/ParserHelper.java @@ -306,15 +306,12 @@ public class ParserHelper { } } - if (tokenizer.next(TokenKind.ODataIdentifier)) { + if(tokenizer.next(TokenKind.GuidValue)) { + keys.add(parseSimpleKey(tokenizer, edm, referringType, aliases, keyPropertyRefs, referencedNames, true)); + } else if (tokenizer.next(TokenKind.ODataIdentifier)) { keys.addAll(compoundKey(tokenizer, edmEntityType, edm, referringType, aliases)); } else if (keyPropertyRefs.size() - referencedNames.size() == 1) { - for (final EdmKeyPropertyRef candidate : keyPropertyRefs) { - if (referencedNames.get(candidate.getName()) == null) { - keys.add(simpleKey(tokenizer, candidate, edm, referringType, aliases)); - break; - } - } + keys.add(parseSimpleKey(tokenizer, edm, referringType, aliases, keyPropertyRefs, referencedNames, false)); } else { throw new UriParserSemanticException( "Expected " + (keyPropertyRefs.size() -referencedNames.size()) + " key predicates but found one.", @@ -349,14 +346,35 @@ public class ParserHelper { } } + private static UriParameter parseSimpleKey(final UriTokenizer tokenizer, final Edm edm, final EdmType referringType, + final Map aliases, + final List keyPropertyRefs, + final Map referencedNames, final boolean tokenConsumed) + throws UriParserException, UriValidationException { + + for (final EdmKeyPropertyRef candidate : keyPropertyRefs) { + if (referencedNames.get(candidate.getName()) == null) { + return simpleKey(tokenizer, candidate, edm, referringType, aliases, tokenConsumed); + } + } + throw new UriParserSemanticException("No suitable key found.", + UriParserSemanticException.MessageKeys.WRONG_NUMBER_OF_KEY_PROPERTIES, + "0", String.valueOf(keyPropertyRefs.size())); + } + private static UriParameter simpleKey(UriTokenizer tokenizer, final EdmKeyPropertyRef edmKeyPropertyRef, - final Edm edm, final EdmType referringType, final Map aliases) + final Edm edm, final EdmType referringType, + final Map aliases, final boolean tokenConsumed) throws UriParserException, UriValidationException { final EdmProperty edmProperty = edmKeyPropertyRef == null ? null : edmKeyPropertyRef.getProperty(); final EdmPrimitiveType primitiveType = edmProperty == null ? null : (EdmPrimitiveType) edmProperty.getType(); final boolean nullable = edmProperty != null && edmProperty.isNullable(); - if (nextPrimitiveTypeValue(tokenizer, primitiveType, nullable)) { + boolean primitiveTypeAvailable = tokenConsumed; + if(!tokenConsumed) { + primitiveTypeAvailable = nextPrimitiveTypeValue(tokenizer, primitiveType, nullable); + } + if (primitiveTypeAvailable) { final String literalValue = tokenizer.getText(); ParserHelper.requireNext(tokenizer, TokenKind.CLOSE); return createUriParameter(edmProperty, edmKeyPropertyRef.getName(), literalValue, edm, referringType, aliases); diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/parser/ParserTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/parser/ParserTest.java index af523aa47..21741ab9a 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/parser/ParserTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/parser/ParserTest.java @@ -43,6 +43,33 @@ import static org.junit.Assert.fail; */ public class ParserTest { + @Test + public void keyPropertyGuid() throws Exception { + final String entitySetName = "ESGuid"; + final String keyPropertyName = "a"; + EdmProperty keyProperty = Mockito.mock(EdmProperty.class); + Mockito.when(keyProperty.getType()) + .thenReturn(OData.newInstance().createPrimitiveTypeInstance(EdmPrimitiveTypeKind.Guid)); + EdmKeyPropertyRef keyPropertyRef = Mockito.mock(EdmKeyPropertyRef.class); + Mockito.when(keyPropertyRef.getName()).thenReturn(keyPropertyName); + Mockito.when(keyPropertyRef.getProperty()).thenReturn(keyProperty); + EdmEntityType entityType = Mockito.mock(EdmEntityType.class); + Mockito.when(entityType.getKeyPredicateNames()).thenReturn(Collections.singletonList(keyPropertyName)); + Mockito.when(entityType.getKeyPropertyRefs()).thenReturn(Collections.singletonList(keyPropertyRef)); + EdmEntitySet entitySet = Mockito.mock(EdmEntitySet.class); + Mockito.when(entitySet.getName()).thenReturn(entitySetName); + Mockito.when(entitySet.getEntityType()).thenReturn(entityType); + EdmEntityContainer container = Mockito.mock(EdmEntityContainer.class); + Mockito.when(container.getEntitySet(entitySetName)).thenReturn(entitySet); + Edm mockedEdm = Mockito.mock(Edm.class); + Mockito.when(mockedEdm.getEntityContainer()).thenReturn(container); + new TestUriValidator().setEdm(mockedEdm) + .run("ESGuid(f89dee73-af9f-4cd4-b330-db93c25ff3c7)") + .goPath() + .at(0).isEntitySet(entitySetName) + .at(0).isKeyPredicate(0, keyPropertyName, "f89dee73-af9f-4cd4-b330-db93c25ff3c7"); + } + @Test public void navPropertySameNameAsEntitySet() throws Exception { final String namespace = "namespace"; diff --git a/samples/tutorials/p0_all/src/main/java/myservice/mynamespace/data/Storage.java b/samples/tutorials/p0_all/src/main/java/myservice/mynamespace/data/Storage.java index 3413e328b..d63262578 100644 --- a/samples/tutorials/p0_all/src/main/java/myservice/mynamespace/data/Storage.java +++ b/samples/tutorials/p0_all/src/main/java/myservice/mynamespace/data/Storage.java @@ -566,23 +566,27 @@ public class Storage { private void initAdvertisementSampleData() { final List advertisements = manager.getEntityCollection(DemoEdmProvider.ES_ADVERTISEMENTS_NAME); - + Entity entity = new Entity(); - entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, + entity.setType(DemoEdmProvider.ET_ADVERTISEMENT_FQN.getFullQualifiedNameAsString()); + entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, UUID.fromString("f89dee73-af9f-4cd4-b330-db93c25ff3c7"))); entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Old School Lemonade Store, Retro Style")); entity.addProperty(new Property(null, "AirDate", ValueType.PRIMITIVE, Timestamp.valueOf("2012-11-07 00:00:00"))); entity.addProperty(new Property(null, MEDIA_PROPERTY_NAME, ValueType.PRIMITIVE, "Super content".getBytes())); entity.setMediaContentType(ContentType.parse("text/plain").toContentTypeString()); + entity.setId(createId(entity, "ID")); advertisements.add(entity); - + entity = new Entity(); - entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, + entity.setType(DemoEdmProvider.ET_ADVERTISEMENT_FQN.getFullQualifiedNameAsString()); + entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, UUID.fromString("db2d2186-1c29-4d1e-88ef-a127f521b9c67"))); entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Early morning start, need coffee")); entity.addProperty(new Property(null, "AirDate", ValueType.PRIMITIVE, Timestamp.valueOf("2000-02-29 00:00:00"))); entity.addProperty(new Property(null, MEDIA_PROPERTY_NAME, ValueType.PRIMITIVE, "Super content2".getBytes())); entity.setMediaContentType(ContentType.parse("text/plain").toContentTypeString()); + entity.setId(createId(entity, "ID")); advertisements.add(entity); } @@ -646,6 +650,8 @@ private void initAdvertisementSampleData() { return DemoEdmProvider.ES_CATEGORIES_NAME; } else if (DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString().equals(entity.getType())) { return DemoEdmProvider.ES_PRODUCTS_NAME; + } else if (DemoEdmProvider.ET_ADVERTISEMENT_FQN.getFullQualifiedNameAsString().equals(entity.getType())) { + return DemoEdmProvider.ES_ADVERTISEMENTS_NAME; } return entity.getType(); } diff --git a/samples/tutorials/p10_media/src/main/java/myservice/mynamespace/data/Storage.java b/samples/tutorials/p10_media/src/main/java/myservice/mynamespace/data/Storage.java index ad9925bd6..732af862f 100644 --- a/samples/tutorials/p10_media/src/main/java/myservice/mynamespace/data/Storage.java +++ b/samples/tutorials/p10_media/src/main/java/myservice/mynamespace/data/Storage.java @@ -431,23 +431,27 @@ public class Storage { } private void initAdvertisementSampleData() { - + Entity entity = new Entity(); - entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, + entity.setType(DemoEdmProvider.ET_ADVERTISEMENT_FQN.getFullQualifiedNameAsString()); + entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, UUID.fromString("f89dee73-af9f-4cd4-b330-db93c25ff3c7"))); entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Old School Lemonade Store, Retro Style")); entity.addProperty(new Property(null, "AirDate", ValueType.PRIMITIVE, Timestamp.valueOf("2012-11-07 00:00:00"))); entity.addProperty(new Property(null, MEDIA_PROPERTY_NAME, ValueType.PRIMITIVE, "Super content".getBytes())); entity.setMediaContentType(ContentType.parse("text/plain").toContentTypeString()); + entity.setId(createId(entity, "ID")); advertisements.add(entity); - + entity = new Entity(); - entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, + entity.setType(DemoEdmProvider.ET_ADVERTISEMENT_FQN.getFullQualifiedNameAsString()); + entity.addProperty(new Property(null, "ID", ValueType.PRIMITIVE, UUID.fromString("db2d2186-1c29-4d1e-88ef-a127f521b9c67"))); entity.addProperty(new Property(null, "Name", ValueType.PRIMITIVE, "Early morning start, need coffee")); entity.addProperty(new Property(null, "AirDate", ValueType.PRIMITIVE, Timestamp.valueOf("2000-02-29 00:00:00"))); entity.addProperty(new Property(null, MEDIA_PROPERTY_NAME, ValueType.PRIMITIVE, "Super content2".getBytes())); entity.setMediaContentType(ContentType.parse("text/plain").toContentTypeString()); + entity.setId(createId(entity, "ID")); advertisements.add(entity); } @@ -474,6 +478,8 @@ public class Storage { return DemoEdmProvider.ES_CATEGORIES_NAME; } else if(DemoEdmProvider.ET_PRODUCT_FQN.getFullQualifiedNameAsString().equals(entity.getType())) { return DemoEdmProvider.ES_PRODUCTS_NAME; + } else if (DemoEdmProvider.ET_ADVERTISEMENT_FQN.getFullQualifiedNameAsString().equals(entity.getType())) { + return DemoEdmProvider.ES_ADVERTISEMENTS_NAME; } return entity.getType(); }