NIFI-8666: Allow users to escape parameter names in Expression Language using quotes. (#5133)

This commit is contained in:
markap14 2021-06-15 09:37:34 -04:00 committed by GitHub
parent b49a62e1b5
commit 96a8b2d090
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 3 deletions

View File

@ -143,8 +143,8 @@ attributeRefOrFunctionCall : (attributeRef | standaloneFunction | parameterRefer
referenceOrFunction : DOLLAR LBRACE attributeRefOrFunctionCall (COLON functionCall)* RBRACE ->
^(EXPRESSION attributeRefOrFunctionCall functionCall*);
parameterReference : PARAMETER_REFERENCE_START ATTRIBUTE_NAME RBRACE ->
^(PARAMETER_REFERENCE ATTRIBUTE_NAME);
parameterReference : PARAMETER_REFERENCE_START singleAttrRef RBRACE ->
^(PARAMETER_REFERENCE singleAttrRef);
expression : referenceOrFunction;

View File

@ -344,6 +344,20 @@ public class TestQuery {
verifyEquals("${#{test}:append(' - '):append(#{test})}", attributes, stateValues, parameters,"unit - unit");
}
@Test
public void testParameterReferenceWithSpace() {
final Map<String, String> attributes = Collections.emptyMap();
final Map<String, String> stateValues = Collections.emptyMap();
final Map<String, String> parameters = new HashMap<>();
parameters.put("test param", "unit");
final Query query = Query.compile("${'#{test param}'}");
verifyEquals("${#{'test param'}}", attributes, stateValues, parameters,"unit");
verifyEquals("${#{'test param'}:append(' - '):append(#{'test param'})}", attributes, stateValues, parameters,"unit - unit");
verifyEquals("${#{\"test param\"}}", attributes, stateValues, parameters,"unit");
}
@Test
public void testJsonPath() throws IOException {
Map<String,String> attributes = verifyJsonPathExpressions(

View File

@ -221,12 +221,32 @@ public class StandardParameterContext implements ParameterContext {
public Optional<Parameter> getParameter(final ParameterDescriptor parameterDescriptor) {
readLock.lock();
try {
return Optional.ofNullable(parameters.get(parameterDescriptor));
// When Expression Language is used, the Parameter may require being escaped.
// This is the case, for instance, if a Parameter name has a space in it.
// Because of this, we may have a case where we attempt to get a Parameter by name
// and that Parameter name is enclosed within single tick marks, as a way of escaping
// the name via the Expression Language. In this case, we want to strip out those
// escaping tick marks and use just the raw name for looking up the Parameter.
final ParameterDescriptor unescaped = unescape(parameterDescriptor);
return Optional.ofNullable(parameters.get(unescaped));
} finally {
readLock.unlock();
}
}
private ParameterDescriptor unescape(final ParameterDescriptor descriptor) {
final String parameterName = descriptor.getName().trim();
if ((parameterName.startsWith("'") && parameterName.endsWith("'")) || (parameterName.startsWith("\"") && parameterName.endsWith("\""))) {
final String stripped = parameterName.substring(1, parameterName.length() - 1);
return new ParameterDescriptor.Builder()
.from(descriptor)
.name(stripped)
.build();
}
return descriptor;
}
@Override
public Map<ParameterDescriptor, Parameter> getParameters() {
readLock.lock();