Repository: olingo-odata4
Updated Branches:
  refs/heads/master db95c1b9d -> 00d854f38


[OLINGO-1229] Validate Function import parameters in Uri Parser


Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo
Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/00d854f3
Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/00d854f3
Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/00d854f3

Branch: refs/heads/master
Commit: 00d854f3876d03379958805aca3ef9a5e897e1fa
Parents: db95c1b
Author: ramya vasanth <ramya.vasa...@sap.com>
Authored: Thu Feb 8 15:22:43 2018 +0530
Committer: ramya vasanth <ramya.vasa...@sap.com>
Committed: Thu Feb 8 15:22:43 2018 +0530

----------------------------------------------------------------------
 .../server/core/uri/parser/ParserHelper.java    | 33 ++++++++++++++++++++
 .../core/uri/parser/ResourcePathParser.java     |  1 +
 .../uri/validator/UriValidationException.java   |  4 ++-
 .../server-core-exceptions-i18n.properties      |  1 +
 .../server/core/uri/parser/UriParserTest.java   | 14 +++++++++
 5 files changed, 52 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/00d854f3/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/ParserHelper.java
----------------------------------------------------------------------
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 edaabae..04ba04b 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
@@ -600,4 +600,37 @@ public class ParserHelper {
           optionName, optionValue);
     }
   }
+  
+  protected static void validateFunctionParameterFacets(final EdmFunction 
function, 
+      final List<UriParameter> parameters, Edm edm, Map<String, 
AliasQueryOption> aliases) 
+          throws UriParserException, UriValidationException {
+    for (UriParameter parameter : parameters) {
+      EdmParameter edmParameter = function.getParameter(parameter.getName());
+      final EdmType type = edmParameter.getType();
+      final EdmTypeKind kind = type.getKind();
+      if ((kind == EdmTypeKind.PRIMITIVE || kind == EdmTypeKind.DEFINITION || 
kind == EdmTypeKind.ENUM)
+          && !edmParameter.isCollection()) {
+        final EdmPrimitiveType primitiveType = (EdmPrimitiveType) type;
+        String text = null;
+        try {
+          text = parameter.getAlias() == null ?
+              parameter.getText() :
+                aliases.containsKey(parameter.getAlias()) ?
+                    parseAliasValue(parameter.getAlias(),
+                        edmParameter.getType(), edmParameter.isNullable(), 
edmParameter.isCollection(),
+                        edm, type, aliases).getText() : null;
+          primitiveType.valueOfString(primitiveType.fromUriLiteral(text),
+              edmParameter.isNullable(), edmParameter.getMaxLength(), 
edmParameter.getPrecision(), 
+              edmParameter.getScale(), true,
+              edmParameter.getMapping() == null ?
+                  primitiveType.getDefaultType() :
+                    edmParameter.getMapping().getMappedJavaClass());
+        } catch (final EdmPrimitiveTypeException e) {
+          throw new UriValidationException(
+              "Invalid value '" + text + "' for parameter " + 
parameter.getName(), e,
+              UriValidationException.MessageKeys.INVALID_VALUE_FOR_PROPERTY, 
parameter.getName());
+        }
+      }
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/00d854f3/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/ResourcePathParser.java
----------------------------------------------------------------------
diff --git 
a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/ResourcePathParser.java
 
b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/ResourcePathParser.java
index 3cce278..9d8bf42 100644
--- 
a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/ResourcePathParser.java
+++ 
b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/parser/ResourcePathParser.java
@@ -406,6 +406,7 @@ public class ResourcePathParser {
       }
     }
     ParserHelper.validateFunctionParameters(function, parameters, edm, null, 
aliases);
+    ParserHelper.validateFunctionParameterFacets(function, parameters, edm, 
aliases);
     UriResourceFunctionImpl resource = new 
UriResourceFunctionImpl(edmFunctionImport, function, parameters);
     if (tokenizer.next(TokenKind.OPEN)) {
       if (function.getReturnType() != null

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/00d854f3/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidationException.java
----------------------------------------------------------------------
diff --git 
a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidationException.java
 
b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidationException.java
index 2b446a7..e26de0b 100644
--- 
a/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidationException.java
+++ 
b/lib/server-core/src/main/java/org/apache/olingo/server/core/uri/validator/UriValidationException.java
@@ -58,7 +58,9 @@ public class UriValidationException extends 
ODataLibraryException {
     /** parameter: missing parameter name */
     MISSING_PARAMETER,
     /** parameter: missing alias name */
-    MISSING_ALIAS;
+    MISSING_ALIAS,
+    /** invalid value for Property**/
+    INVALID_VALUE_FOR_PROPERTY;
 
     @Override
     public String getKey() {

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/00d854f3/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties
----------------------------------------------------------------------
diff --git 
a/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties 
b/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties
index 2589abd..6eb9620 100644
--- a/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties
+++ b/lib/server-core/src/main/resources/server-core-exceptions-i18n.properties
@@ -97,6 +97,7 @@ UriValidationException.UNALLOWED_KIND_BEFORE_COUNT=The kind 
'%1$s' is not allowe
 UriValidationException.UNALLOWED_RESOURCE_PATH=The resource part '%1$s' is not 
allowed.
 UriValidationException.MISSING_PARAMETER=Missing mandatory parameter '%1$s'.
 UriValidationException.MISSING_ALIAS=Missing alias '%1$s'.
+UriValidationException.INVALID_VALUE_FOR_PROPERTY=Invalid value for property 
'%1$s'.
 
 ContentNegotiatorException.UNSUPPORTED_ACCEPT_TYPES=The content-type range 
'%1$s' is not supported as value of the Accept header.
 ContentNegotiatorException.UNSUPPORTED_CONTENT_TYPE=The content type '%1$s' is 
not supported.

http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/00d854f3/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/parser/UriParserTest.java
----------------------------------------------------------------------
diff --git 
a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/parser/UriParserTest.java
 
b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/parser/UriParserTest.java
index 5363e5c..c97cb93 100644
--- 
a/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/parser/UriParserTest.java
+++ 
b/lib/server-test/src/test/java/org/apache/olingo/server/core/uri/parser/UriParserTest.java
@@ -917,4 +917,18 @@ public class UriParserTest {
     testUri.run("ESTwoKeyNav", "custom=ABC")
         .isCustomParameter(0, "custom", "ABC");
   }
+  
+  @Test
+  public void testValidationOnFunctions() throws Exception {
+    testUri.runEx("FICRTETTwoKeyNavParam(ParameterInt16='32')")
+    
.isExValidation(UriValidationException.MessageKeys.INVALID_VALUE_FOR_PROPERTY);
+  
+    testUri.runEx("FICRTETTwoKeyNavParam(ParameterInt16=null)")
+    .isExValidation(UriValidationException.MessageKeys.MISSING_PARAMETER);
+  
+    testUri.runEx("FICRTETTwoKeyNavParam(ParameterInt16=@p1)", "@p1='32'")
+    .isExSemantic(UriParserSemanticException.MessageKeys.UNKNOWN_PART);
+    
+    testUri.run("FICRTETTwoKeyNavParam(ParameterInt16=32)");
+  }
 }

Reply via email to