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

This commit is contained in:
ramya vasanth 2018-02-08 15:22:43 +05:30
parent db95c1b9db
commit 00d854f387
5 changed files with 52 additions and 1 deletions

View File

@ -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());
}
}
}
}
}

View File

@ -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

View File

@ -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() {

View File

@ -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.

View File

@ -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)");
}
}