Skip to content

Commit 73a0a81

Browse files
committed
feat: add an option to evaluate as object
https://gravitee.atlassian.net/browse/APIM-12684
1 parent eb2335a commit 73a0a81

File tree

7 files changed

+39
-9
lines changed

7 files changed

+39
-9
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ system:
116116
#### Context variables (Array)
117117
| Name <br>`json name` | Type <br>`constraint` | Mandatory | Default | Description |
118118
|:----------------------|:-----------------------|:----------:|:---------|:-------------|
119+
| Evaluate as string<br>`evaluateAsString`| boolean| | `true`| Evaluate the value as a string. Default is true for backward compatibility.|
119120
| Name<br>`name`| string| | | |
120121
| Value<br>`value`| string| | `{#jsonPath(#calloutResponse.content, '$.field')}`| |
121122

src/main/java/io/gravitee/policy/callout/CalloutHttpPolicy.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,11 @@ private Completable processSuccess(BaseExecutionContext ctx) {
196196
return Flowable.fromIterable(configuration.getVariables())
197197
.flatMapCompletable(variable -> {
198198
ctx.setAttribute(variable.getName(), null);
199-
return Maybe.just(variable.getValue())
200-
.flatMap(value -> ctx.getTemplateEngine().eval(value, String.class))
199+
return Maybe.just(variable)
200+
.flatMap(var -> {
201+
Class<?> clazz = var.isEvaluateAsString() ? String.class : Object.class;
202+
return ctx.getTemplateEngine().eval(var.getValue(), clazz);
203+
})
201204
.doOnSuccess(value -> ctx.setAttribute(variable.getName(), value))
202205
.ignoreElement();
203206
})

src/main/java/io/gravitee/policy/callout/configuration/Variable.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ public class Variable {
3535

3636
private String value;
3737

38+
private boolean evaluateAsString = true;
39+
40+
public Variable(String name, String value) {
41+
this.name = name;
42+
this.value = value;
43+
}
44+
3845
@Override
3946
public boolean equals(Object o) {
4047
if (this == o) return true;

src/main/java/io/gravitee/policy/v3/callout/CalloutHttpPolicyV3.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,12 @@ private void handleSuccess(
300300
.getVariables()
301301
.forEach(variable -> {
302302
try {
303-
String extValue = (variable.getValue() != null)
304-
? tplEngine.evalNow(variable.getValue(), String.class)
305-
: null;
306-
context.setAttribute(variable.getName(), extValue);
303+
if (variable.getValue() != null) {
304+
Class<?> clazz = variable.isEvaluateAsString() ? String.class : Object.class;
305+
context.setAttribute(variable.getName(), tplEngine.evalNow(variable.getValue(), clazz));
306+
} else {
307+
context.setAttribute(variable.getName(), null);
308+
}
307309
} catch (Throwable ex) {
308310
// Do nothing
309311
}

src/main/resources/schemas/schema-form.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@
8383
"title": "Value",
8484
"type": "string",
8585
"default": "{#jsonPath(#calloutResponse.content, '$.field')}"
86+
},
87+
"evaluateAsString": {
88+
"title": "Evaluate as string",
89+
"description": "Evaluate the value as a string. Default is true for backward compatibility.",
90+
"type": "boolean",
91+
"default": true
8692
}
8793
}
8894
},

src/test/java/io/gravitee/policy/callout/CalloutHttpPolicyV4Test.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import io.vertx.rxjava3.core.Vertx;
4646
import java.util.ArrayList;
4747
import java.util.List;
48+
import java.util.Map;
4849
import java.util.concurrent.TimeUnit;
4950
import org.junit.jupiter.api.BeforeEach;
5051
import org.junit.jupiter.api.Nested;
@@ -200,7 +201,8 @@ void should_add_variables_in_context_when_callout_succeed(boolean exitOnError) {
200201
.variables(
201202
List.of(
202203
new Variable("callout1", "static"),
203-
new Variable("callout2", "{#jsonPath(#calloutResponse.content, '$.key')}")
204+
new Variable("callout2", "{#jsonPath(#calloutResponse.content, '$.key')}"),
205+
new Variable("callout3", "{#jsonPath(#calloutResponse.content, '$')}", false)
204206
)
205207
)
206208
.build()
@@ -210,7 +212,10 @@ void should_add_variables_in_context_when_callout_succeed(boolean exitOnError) {
210212
.awaitDone(30, TimeUnit.SECONDS)
211213
.assertComplete();
212214

213-
assertThat(ctx.getAttributes()).containsEntry("callout1", "static").containsEntry("callout2", "a-value");
215+
assertThat(ctx.getAttributes())
216+
.containsEntry("callout1", "static")
217+
.containsEntry("callout2", "a-value")
218+
.containsEntry("callout3", Map.of("key", "a-value"));
214219
}
215220

216221
@ParameterizedTest

src/test/java/io/gravitee/policy/v3/callout/CalloutHttpPolicyV3Test.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
import io.gravitee.policy.callout.configuration.Variable;
4646
import io.vertx.core.Vertx;
4747
import java.util.Collections;
48+
import java.util.List;
49+
import java.util.Map;
4850
import java.util.UUID;
4951
import java.util.concurrent.CountDownLatch;
5052
import java.util.concurrent.TimeUnit;
@@ -264,7 +266,10 @@ private void executeProcess_withMainRequestVariable() throws InterruptedExceptio
264266
when(configuration.getMethod()).thenReturn(HttpMethod.GET);
265267
when(configuration.getUrl()).thenReturn("http://localhost:" + wireMockRule.port() + "/{#request.params['param']}");
266268
when(configuration.getVariables()).thenReturn(
267-
Collections.singletonList(new Variable("my-var", "{#jsonPath(#calloutResponse.content, '$.key')}"))
269+
List.of(
270+
new Variable("my-var", "{#jsonPath(#calloutResponse.content, '$.key')}"),
271+
new Variable("my-object-var", "{#jsonPath(#calloutResponse.content, '$')}", false)
272+
)
268273
);
269274

270275
final CountDownLatch lock = new CountDownLatch(1);
@@ -279,6 +284,7 @@ private void executeProcess_withMainRequestVariable() throws InterruptedExceptio
279284
lock.await(1000, TimeUnit.MILLISECONDS);
280285

281286
verify(executionContext, times(1)).setAttribute(eq("my-var"), eq("value"));
287+
verify(executionContext, times(1)).setAttribute(eq("my-object-var"), eq(Map.of("key", "value")));
282288
verify(policyChain, times(1)).doNext(request, response);
283289

284290
verify(getRequestedFor(urlEqualTo("/content")));

0 commit comments

Comments
 (0)