Skip to content

Commit

Permalink
feat: improve map access with dot
Browse files Browse the repository at this point in the history
  • Loading branch information
gcusnieux committed Mar 3, 2022
1 parent d13db74 commit b76050f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -150,7 +150,7 @@
<properties>
<json-path.version>2.6.0</json-path.version>
<gravitee-common.version>1.25.0</gravitee-common.version>
<gravitee-gateway-api.version>1.31.0</gravitee-gateway-api.version>
<gravitee-gateway-api.version>1.31.1-SNAPSHOT</gravitee-gateway-api.version>
<jmh.version>1.21</jmh.version>
</properties>
</project>
32 changes: 32 additions & 0 deletions src/main/java/io/gravitee/el/spel/context/ReadOnlyMapAccessor.java
@@ -0,0 +1,32 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.gravitee.el.spel.context;

import org.springframework.context.expression.MapAccessor;
import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;

/**
* @author Guillaume CUSNIEUX (guillaume.cusnieux at graviteesource.com)
* @author GraviteeSource Team
*/
public class ReadOnlyMapAccessor extends MapAccessor {

@Override
public boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException {
return false;
}
}
Expand Up @@ -31,7 +31,8 @@ public class SecuredEvaluationContext implements EvaluationContext {
// Read only property access.
private static final List<PropertyAccessor> propertyAccessors = Arrays.asList(
DataBindingPropertyAccessor.forReadOnlyAccess(),
new HttpHeadersPropertyAccessor()
new HttpHeadersPropertyAccessor(),
new ReadOnlyMapAccessor()
);

// Secure method resolver to allow only whitelisted methods.
Expand Down
Expand Up @@ -27,6 +27,8 @@
import io.gravitee.el.exceptions.ExpressionEvaluationException;
import io.gravitee.el.spel.EvaluableRequest;
import io.gravitee.el.spel.Request;
import io.gravitee.gateway.api.Response;
import io.gravitee.gateway.api.context.SimpleExecutionContext;
import io.gravitee.gateway.api.http.HttpHeaders;
import java.util.*;
import org.junit.Assert;
Expand Down Expand Up @@ -573,4 +575,28 @@ public void shouldEvaluateSimpleContentWithSpace() {
String value = engine.getValue(expression, String.class);
assertEquals(value, expression);
}

@Test
public void shouldReadMapValueWithDot() {
final MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
parameters.put("param", Collections.singletonList("myparam"));

when(request.parameters()).thenReturn(parameters);
when(request.path()).thenReturn("/stores/99/products/123456");

TemplateEngine engine = TemplateEngine.templateEngine();
engine.getTemplateContext().setVariable("request", new EvaluableRequest(request));

MultiValueMap<String, String> paramsValue = engine.getValue("{#request.params}", MultiValueMap.class);
assertEquals("myparam", engine.getValue("{#request.params['param']}", String.class));
assertEquals("myparam", engine.getValue("{#request.params.param}", String.class));

io.gravitee.gateway.api.Request req = Mockito.mock(io.gravitee.gateway.api.Request.class);
Response res = Mockito.mock(Response.class);
SimpleExecutionContext simpleExecutionContext = new SimpleExecutionContext(req, res);
simpleExecutionContext.setAttribute("gravitee.attribute.api", "my-api-id");
engine.getTemplateContext().setVariable("context", simpleExecutionContext);
assertEquals("my-api-id", engine.getValue("{#context.attributes['api']}", String.class));
assertEquals("my-api-id", engine.getValue("{#context.attributes.api}", String.class));
}
}

0 comments on commit b76050f

Please sign in to comment.