From 00d854f3876d03379958805aca3ef9a5e897e1fa Mon Sep 17 00:00:00 2001 From: ramya vasanth Date: Thu, 8 Feb 2018 15:22:43 +0530 Subject: [PATCH] [OLINGO-1229] Validate Function import parameters in Uri Parser --- .../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(-) 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 edaabae3c..04ba04b07 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 parameters, Edm edm, Map 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()); + } + } + } + } } 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 3cce2788b..9d8bf42fb 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 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 2b446a760..e26de0b3e 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() { 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 2589abdb2..6eb96207a 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. 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 5363e5cd5..c97cb9336 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)"); + } }