stricter check of segment order in URI parser Change-Id: I2fd11f0e2047c45ecec36ab555971d0549adaa9a
Signed-off-by: Michael Bolz <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/7715006e Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/7715006e Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/7715006e Branch: refs/heads/master Commit: 7715006eaff5fc8886f6ffb753ca35d1edc74bdc Parents: 30d67c7 Author: Klaus Straubinger <[email protected]> Authored: Fri Sep 11 08:42:14 2015 +0200 Committer: Michael Bolz <[email protected]> Committed: Fri Sep 11 09:14:16 2015 +0200 ---------------------------------------------------------------------- .../core/uri/parser/UriParseTreeVisitor.java | 46 +++---- .../core/uri/antlr/TestFullResourcePath.java | 130 +++++++++++-------- 2 files changed, 96 insertions(+), 80 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/7715006e/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java index 36717cc..bedfaf2 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/UriParseTreeVisitor.java @@ -52,6 +52,7 @@ import org.apache.olingo.server.api.uri.UriResource; import org.apache.olingo.server.api.uri.UriResourceEntitySet; import org.apache.olingo.server.api.uri.UriResourceNavigation; import org.apache.olingo.server.api.uri.UriResourcePartTyped; +import org.apache.olingo.server.api.uri.UriResourceRoot; import org.apache.olingo.server.api.uri.queryoption.expression.BinaryOperatorKind; import org.apache.olingo.server.api.uri.queryoption.expression.MethodKind; import org.apache.olingo.server.api.uri.queryoption.expression.UnaryOperatorKind; @@ -76,9 +77,7 @@ import org.apache.olingo.server.core.uri.UriResourceStartingTypeFilterImpl; import org.apache.olingo.server.core.uri.UriResourceTypedImpl; import org.apache.olingo.server.core.uri.UriResourceValueImpl; import org.apache.olingo.server.core.uri.UriResourceWithKeysImpl; -import org.apache.olingo.server.core.uri.antlr.UriLexer; -import org.apache.olingo.server.core.uri.antlr.UriParserBaseVisitor; -import org.apache.olingo.server.core.uri.antlr.UriParserParser; +import org.apache.olingo.server.core.uri.antlr.*; import org.apache.olingo.server.core.uri.antlr.UriParserParser.AllEOFContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.AllExprContext; import org.apache.olingo.server.core.uri.antlr.UriParserParser.AltAddContext; @@ -321,41 +320,42 @@ public class UriParseTreeVisitor extends UriParserBaseVisitor<Object> { } if (searchInContainer) { + final List<UriResource> parts = context.contextUriInfo.getUriResourceParts(); // check EntitySet EdmEntitySet edmEntitySet = edmEntityContainer.getEntitySet(odi); - if( !context.contextUriInfo.getUriResourceParts().isEmpty() - && context.contextUriInfo.getUriResourceParts().get(0) instanceof UriResourceEntitySet) { - edmEntitySet = null; - } else { - if (edmEntitySet != null) { - UriResourceEntitySetImpl uriResource = new UriResourceEntitySetImpl() - .setEntitSet(edmEntitySet); - context.contextUriInfo.addResourcePart(uriResource); - return null; - } + if (edmEntitySet != null + && (parts.isEmpty() || !(parts.get(0) instanceof UriResourcePartTyped) + || parts.get(0) instanceof UriResourceRoot)) { + context.contextUriInfo.addResourcePart( + new UriResourceEntitySetImpl().setEntitSet(edmEntitySet)); + return null; } - + // check Singleton EdmSingleton edmSingleton = edmEntityContainer.getSingleton(odi); - if (edmSingleton != null) { - UriResourceSingletonImpl uriResource = new UriResourceSingletonImpl() - .setSingleton(edmSingleton); - context.contextUriInfo.addResourcePart(uriResource); + if (edmSingleton != null + && (parts.isEmpty() || !(parts.get(0) instanceof UriResourcePartTyped) + || parts.get(0) instanceof UriResourceRoot)) { + context.contextUriInfo.addResourcePart( + new UriResourceSingletonImpl().setSingleton(edmSingleton)); return null; } // check ActionImport EdmActionImport edmActionImport = edmEntityContainer.getActionImport(odi); - if (edmActionImport != null) { - UriResourceActionImpl uriResource = new UriResourceActionImpl() - .setActionImport(edmActionImport); - context.contextUriInfo.addResourcePart(uriResource); + if (edmActionImport != null + && (parts.isEmpty() || !(parts.get(0) instanceof UriResourcePartTyped) + || parts.get(0) instanceof UriResourceRoot)) { + context.contextUriInfo.addResourcePart( + new UriResourceActionImpl().setActionImport(edmActionImport)); return null; } // check FunctionImport EdmFunctionImport edmFunctionImport = edmEntityContainer.getFunctionImport(odi); - if (edmFunctionImport != null) { + if (edmFunctionImport != null + && (parts.isEmpty() || !(parts.get(0) instanceof UriResourcePartTyped) + || parts.get(0) instanceof UriResourceRoot)) { // read the URI parameters if (ctx.vlNVO.isEmpty()) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/7715006e/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java index b4fe8fe..f20882b 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/antlr/TestFullResourcePath.java @@ -5279,40 +5279,42 @@ public class TestFullResourcePath { .goUpUriValidator() .isCustomParameter(0, "@A", "'2'"); } - - @Test(expected=UriParserException.class) - public void testDoublePercentDecoding() throws Exception { - testUri.run("ESAllPrim%252832767%29"); + + @Test + public void doublePercentDecoding() throws Exception { + testUri.runEx("ESAllPrim%252832767%29").isExSyntax(UriParserSyntaxException.MessageKeys.SYNTAX); } - - @Test(expected=UriParserException.class) - public void testMultipleKeysInResourcePath() throws Exception { + + @Test + public void multipleKeysInResourcePath() throws Exception { // See OLINGO-730 - testUri.run("ESAllPrim(32767)(1)(2)"); + testUri.runEx("ESAllPrim(32767)(1)(2)").isExSemantic(MessageKeys.WRONG_NUMBER_OF_KEY_PROPERTIES); } - - @Test(expected=UriParserException.class) - public void testSimpleKeyInExpandSystemQueryOption() throws Exception { - testUri.run("ESAllPrim(0)", "$expand=NavPropertyETTwoPrimMany(-365)($filter=PropertyString eq 'Test String1')"); + + @Test + public void simpleKeyInExpandSystemQueryOption() throws Exception { + testUri.runEx("ESAllPrim(0)", "$expand=NavPropertyETTwoPrimMany(-365)($filter=PropertyString eq 'Test String1')") + .isExSemantic(MessageKeys.KEY_NOT_ALLOWED); } - - @Test(expected=UriParserException.class) - public void testCompountKeyInExpandSystemQueryOption() throws Exception { - testUri.run("ESAllPrim(0)", "$expand=NavPropertyETTwoPrimMany(PropertyInt16=1,PropertyString=2)" - + "($filter=PropertyString eq 'Test String1')"); + + @Test + public void compoundKeyInExpandSystemQueryOption() throws Exception { + testUri.runEx("ESAllPrim(0)", "$expand=NavPropertyETTwoPrimMany(PropertyInt16=1,PropertyString=2)" + + "($filter=PropertyString eq 'Test String1')") + .isExSemantic(MessageKeys.KEY_NOT_ALLOWED); } - + @Test - public void testKeyPredicatesInExpandFilter() throws Exception { + public void keyPredicatesInExpandFilter() throws Exception { testUri.run("ESKeyNav(0)", "$expand=NavPropertyETTwoKeyNavMany($filter=NavPropertyETTwoKeyNavMany" + "(PropertyInt16=1,PropertyString='2')/PropertyInt16 eq 1)").goPath().goExpand() .first().goPath().isNavProperty("NavPropertyETTwoKeyNavMany", EntityTypeProvider.nameETTwoKeyNav, true) .goUpExpandValidator() .isFilterSerialized("<<NavPropertyETTwoKeyNavMany/PropertyInt16> eq <1>>"); } - + @Test - public void testKeyPredicatesInDoubleExpandedFilter() throws Exception { + public void KeyPredicatesInDoubleExpandedFilter() throws Exception { testUri.run("ESKeyNav(0)", "$expand=NavPropertyETTwoKeyNavMany($expand=NavPropertyETTwoKeyNavMany" + "($filter=NavPropertyETTwoKeyNavMany(PropertyInt16=1,PropertyString='2')/PropertyInt16 eq 1))") .goPath().goExpand() @@ -5322,61 +5324,75 @@ public class TestFullResourcePath { .goUpExpandValidator() .isFilterSerialized("<<NavPropertyETTwoKeyNavMany/PropertyInt16> eq <1>>"); } - - @Test(expected=UriParserException.class) - public void testFilterSystemQueryOptionAnyWithKeyAny() throws Exception { - testUri.run("ESAllPrim", "$filter=NavPropertyETTwoPrimMany(1)" - + "/any(d:d/PropertyInt16 eq 0)"); + + @Test + public void filterSystemQueryOptionAnyWithKeyAny() throws Exception { + testUri.runEx("ESAllPrim", "$filter=NavPropertyETTwoPrimMany(1)/any(d:d/PropertyInt16 eq 0)") + .isExSemantic(MessageKeys.KEY_NOT_ALLOWED); } - - @Test(expected=UriParserException.class) - public void testFilterSystemQueryOptionAnyWithKeyAll() throws Exception { - testUri.run("ESAllPrim", "$filter=NavPropertyETTwoPrimMany(1)" - + "/all(d:d/PropertyInt16 eq 0)"); + + @Test + public void filterSystemQueryOptionAnyWithKeyAll() throws Exception { + testUri.runEx("ESAllPrim", "$filter=NavPropertyETTwoPrimMany(1)/all(d:d/PropertyInt16 eq 0)") + .isExSemantic(MessageKeys.KEY_NOT_ALLOWED); } - + @Test - public void testNavigationPropertyWithCount() throws Exception { + public void navigationPropertyWithCount() throws Exception { testUri.run("ESKeyNav(1)/NavPropertyETTwoKeyNavMany/$count") .goPath().at(0).isEntitySet("ESKeyNav").isKeyPredicate(0, "PropertyInt16", "1") .at(1).isNavProperty("NavPropertyETTwoKeyNavMany", EntityTypeProvider.nameETTwoKeyNav, true) .at(2).isCount(); } - - @Test(expected=UriParserException.class) - public void testNavigationWithMoreThanOneKey() throws Exception { - testUri.run("ESKeyNav(1)/NavPropertyETTwoKeyNavMany(PropertyInt=1,PropertyString='2')" - + "(PropertyInt=1,PropertyString='2')"); + + @Test + public void navigationWithMoreThanOneKey() throws Exception { + testUri.runEx("ESKeyNav(1)/NavPropertyETTwoKeyNavMany(PropertyInt=1,PropertyString='2')" + + "(PropertyInt=1,PropertyString='2')") + .isExSemantic(MessageKeys.WRONG_NUMBER_OF_KEY_PROPERTIES); } - + @Test - public void testEntitySetsInsteadOfNavigationProperties() { - testUri.runEx("ESAllPrim(0)/ESAllPrim(0)/ESAllPrim(0)") - .isExSemantic(MessageKeys.PROPERTY_NOT_IN_TYPE); + public void startElementsInsteadOfNavigationProperties() { + testUri.runEx("ESAllPrim(0)/ESAllPrim(0)/ESAllPrim(0)").isExSemantic(MessageKeys.PROPERTY_NOT_IN_TYPE); + testUri.runEx("ESAllPrim(0)/SINav").isExSemantic(MessageKeys.PROPERTY_NOT_IN_TYPE); + testUri.runEx("ESAllPrim(0)/FICRTString()").isExSemantic(MessageKeys.PROPERTY_NOT_IN_TYPE); + testUri.runEx("ESAllPrim(0)/AIRTString").isExSemantic(MessageKeys.PROPERTY_NOT_IN_TYPE); + testUri.runEx("SI/ESAllPrim(0)").isExSemantic(MessageKeys.PROPERTY_NOT_IN_TYPE); + testUri.runEx("SI/SINav").isExSemantic(MessageKeys.PROPERTY_NOT_IN_TYPE); + testUri.runEx("SI/FICRTString()").isExSemantic(MessageKeys.PROPERTY_NOT_IN_TYPE); + testUri.runEx("SI/AIRTString").isExSemantic(MessageKeys.PROPERTY_NOT_IN_TYPE); + testUri.runEx("FICRTETKeyNav()/ESAllPrim(0)").isExSemantic(MessageKeys.PROPERTY_NOT_IN_TYPE); + testUri.runEx("FICRTETKeyNav()/SINav").isExSemantic(MessageKeys.PROPERTY_NOT_IN_TYPE); + testUri.runEx("FICRTETKeyNav()/FICRTString()").isExSemantic(MessageKeys.PROPERTY_NOT_IN_TYPE); + testUri.runEx("FICRTETKeyNav()/AIRTString").isExSemantic(MessageKeys.PROPERTY_NOT_IN_TYPE); + testUri.runEx("AIRTESAllPrimParam/ESAllPrim(0)").isExSemantic(MessageKeys.RESOURCE_PART_ONLY_FOR_TYPED_PARTS); + testUri.runEx("AIRTESAllPrimParam/SINav").isExSemantic(MessageKeys.RESOURCE_PART_ONLY_FOR_TYPED_PARTS); + testUri.runEx("AIRTESAllPrimParam/FICRTString()").isExSemantic(MessageKeys.RESOURCE_PART_ONLY_FOR_TYPED_PARTS); + testUri.runEx("AIRTESAllPrimParam/AIRTString").isExSemantic(MessageKeys.RESOURCE_PART_ONLY_FOR_TYPED_PARTS); } - + @Test - public void testNavPropertySameNameAsEntitySet() throws Exception { - + public void navPropertySameNameAsEntitySet() throws Exception { testUri.run("ESNavProp(1)/ESNavProp(2)/ESNavProp(3)/ESNavProp") - .goPath() - .at(0).isEntitySet("ESNavProp") - .at(0).isKeyPredicate(0, "a", "1") - .at(1).isNavProperty("ESNavProp", EdmTechTestProvider.nameETNavProp, false) - .at(1).isKeyPredicate(0, "a", "2") - .at(2).isNavProperty("ESNavProp", EdmTechTestProvider.nameETNavProp, false) - .at(2).isKeyPredicate(0, "a", "3") - .at(3).isNavProperty("ESNavProp", EdmTechTestProvider.nameETNavProp, true); + .goPath() + .at(0).isEntitySet("ESNavProp") + .at(0).isKeyPredicate(0, "a", "1") + .at(1).isNavProperty("ESNavProp", EdmTechTestProvider.nameETNavProp, false) + .at(1).isKeyPredicate(0, "a", "2") + .at(2).isNavProperty("ESNavProp", EdmTechTestProvider.nameETNavProp, false) + .at(2).isKeyPredicate(0, "a", "3") + .at(3).isNavProperty("ESNavProp", EdmTechTestProvider.nameETNavProp, true); } - + @Test - public void testFilterLiteralTypes() throws Exception { + public void filterLiteralTypes() throws Exception { testUri.run("ESAllPrim", "$filter='1' eq 42") .goFilter().isBinary(BinaryOperatorKind.EQ) .left().isLiteral("'1'").isLiteralType(EdmString.getInstance()) .root() .right().isLiteral("42").isLiteralType(EdmSByte.getInstance()); - + testUri.run("ESAllPrim", "$filter=127 eq 128") .goFilter().isBinary(BinaryOperatorKind.EQ) .left().isLiteral("127").isLiteralType(EdmSByte.getInstance()) @@ -5445,7 +5461,7 @@ public class TestFullResourcePath { .root() .right().isLiteral("" + Long.MAX_VALUE).isLiteralType(EdmInt64.getInstance()); } - + public static String encode(final String decoded) throws UnsupportedEncodingException { return Encoder.encode(decoded); }
