Skip to content

Commit c0fdab3

Browse files
committed
going to the previous code version and updating the knative result handler to use the JsonNodeResultHandlerSupplier
Signed-off-by: oEscal <pedroescaleira@hotmail.com>
1 parent d476723 commit c0fdab3

4 files changed

Lines changed: 14 additions & 124 deletions

File tree

quarkus/addons/knative/serving/deployment/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/deployment/customfunctions/KnativeTypeHandler.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.kie.kogito.serverless.workflow.parser.ParserContext;
3535
import org.kie.kogito.serverless.workflow.parser.VariableInfo;
3636
import org.kie.kogito.serverless.workflow.parser.types.WorkItemTypeHandler;
37+
import org.kie.kogito.serverless.workflow.suppliers.JsonNodeResultHandlerSupplier;
3738
import org.kie.kogito.serverless.workflow.suppliers.ParamsRestBodyBuilderSupplier;
3839
import org.kogito.workitem.rest.RestWorkItemHandler;
3940

@@ -80,10 +81,7 @@ public class KnativeTypeHandler extends WorkItemTypeHandler {
8081

8182
node.workParameter(KnativeWorkItemHandler.SERVICE_PROPERTY_NAME, operation.getService())
8283
.workParameter(KnativeWorkItemHandler.PATH_PROPERTY_NAME, operation.getPath())
83-
.workParameter(RestWorkItemHandler.METHOD, operation.getHttpMethod())
84-
.workParameter(RestWorkItemHandler.RETURN_HEADERS, operation.returnHeaders())
85-
.workParameter(RestWorkItemHandler.RETURN_STATUS_CODE, operation.returnStatusCode())
86-
.workParameter(RestWorkItemHandler.FAIL_ON_STATUS_ERROR, operation.failOnStatusError());
84+
.workParameter(RestWorkItemHandler.METHOD, operation.getHttpMethod());
8785

8886
return addFunctionArgs(workflow,
8987
fillWorkItemHandler(workflow, context, node, functionDef),
@@ -110,6 +108,7 @@ private static List<String> getPayloadFields(FunctionRef functionRef) {
110108
context.getContext(), String.class, DEFAULT_REQUEST_TIMEOUT_VALUE);
111109

112110
return node.workParameter(RestWorkItemHandler.BODY_BUILDER, new ParamsRestBodyBuilderSupplier())
111+
.workParameter(RestWorkItemHandler.RESULT_HANDLER, new JsonNodeResultHandlerSupplier())
113112
.workParameter(RestWorkItemHandler.REQUEST_TIMEOUT_IN_MILLIS, requestTimeout)
114113
.metaData(TaskDescriptor.KEY_WORKITEM_TYPE, RestWorkItemHandler.REST_TASK_TYPE)
115114
.workName(KnativeWorkItemHandler.NAME);

quarkus/addons/knative/serving/runtime/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/Operation.java

Lines changed: 1 addition & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@ public final class Operation {
3838

3939
static final String METHOD_PARAMETER_NAME = "method";
4040

41-
static final String RETURN_HEADERS_PARAMETER_NAME = "returnHeaders";
42-
43-
static final String RETURN_STATUS_CODE_PARAMETER_NAME = "returnStatusCode";
44-
45-
static final String FAIL_ON_STATUS_ERROR_PARAMETER_NAME = "failOnStatusError";
46-
4741
private final String service;
4842

4943
private final String path;
@@ -52,20 +46,11 @@ public final class Operation {
5246

5347
private final HttpMethod httpMethod;
5448

55-
private final boolean returnHeaders;
56-
57-
private final boolean returnStatusCode;
58-
59-
private final boolean failOnStatusError;
60-
6149
private Operation(Builder builder) {
6250
this.service = Objects.requireNonNull(builder.service);
6351
this.path = builder.path != null ? builder.path : "/";
6452
this.isCloudEvent = builder.isCloudEvent;
6553
this.httpMethod = builder.httpMethod;
66-
this.returnHeaders = builder.returnHeaders;
67-
this.returnStatusCode = builder.returnStatusCode;
68-
this.failOnStatusError = builder.failOnStatusError;
6954
validate(this);
7055
}
7156

@@ -96,18 +81,6 @@ public HttpMethod getHttpMethod() {
9681
return httpMethod;
9782
}
9883

99-
public boolean returnHeaders() {
100-
return returnHeaders;
101-
}
102-
103-
public boolean returnStatusCode() {
104-
return returnStatusCode;
105-
}
106-
107-
public boolean failOnStatusError() {
108-
return failOnStatusError;
109-
}
110-
11184
public static Operation parse(String value) {
11285
String[] parts = value.split("\\?", 2);
11386

@@ -123,9 +96,6 @@ public static Operation parse(String value) {
12396
.withPath(params.get(PATH_PARAMETER_NAME))
12497
.withIsCloudEvent(Boolean.parseBoolean(params.get(CLOUD_EVENT_PARAMETER_NAME)))
12598
.withMethod(HttpMethod.valueOf(params.getOrDefault(METHOD_PARAMETER_NAME, DEFAULT_HTTP_METHOD.name()).toUpperCase()))
126-
.withReturnHeaders(Boolean.parseBoolean(params.get(RETURN_HEADERS_PARAMETER_NAME)))
127-
.withReturnStatusCode(Boolean.parseBoolean(params.get(RETURN_STATUS_CODE_PARAMETER_NAME)))
128-
.withFailOnStatusError(Boolean.parseBoolean(params.getOrDefault(FAIL_ON_STATUS_ERROR_PARAMETER_NAME, "true")))
12999
.build();
130100
}
131101

@@ -143,9 +113,6 @@ public boolean equals(Object o) {
143113
}
144114
Operation operation = (Operation) o;
145115
return isCloudEvent == operation.isCloudEvent
146-
&& returnHeaders == operation.returnHeaders
147-
&& returnStatusCode == operation.returnStatusCode
148-
&& failOnStatusError == operation.failOnStatusError
149116
&& Objects.equals(service, operation.service)
150117
&& Objects.equals(path, operation.path)
151118
&& Objects.equals(httpMethod, operation.httpMethod);
@@ -158,15 +125,12 @@ public String toString() {
158125
", path='" + path + '\'' +
159126
", isCloudEvent=" + isCloudEvent +
160127
", httpMethod=" + httpMethod +
161-
", returnHeaders=" + returnHeaders +
162-
", returnStatusCode=" + returnStatusCode +
163-
", failOnStatusError=" + failOnStatusError +
164128
'}';
165129
}
166130

167131
@Override
168132
public int hashCode() {
169-
return Objects.hash(service, path, isCloudEvent, httpMethod, returnHeaders, returnStatusCode, failOnStatusError);
133+
return Objects.hash(service, path, isCloudEvent, httpMethod);
170134
}
171135

172136
public static class Builder {
@@ -179,12 +143,6 @@ public static class Builder {
179143

180144
private HttpMethod httpMethod = DEFAULT_HTTP_METHOD;
181145

182-
private boolean returnHeaders;
183-
184-
private boolean returnStatusCode;
185-
186-
private boolean failOnStatusError;
187-
188146
private Builder() {
189147
}
190148

@@ -208,21 +166,6 @@ public Builder withMethod(HttpMethod httpMethod) {
208166
return this;
209167
}
210168

211-
public Builder withReturnHeaders(boolean returnHeaders) {
212-
this.returnHeaders = returnHeaders;
213-
return this;
214-
}
215-
216-
public Builder withReturnStatusCode(boolean returnStatusCode) {
217-
this.returnStatusCode = returnStatusCode;
218-
return this;
219-
}
220-
221-
public Builder withFailOnStatusError(boolean failOnStatusError) {
222-
this.failOnStatusError = failOnStatusError;
223-
return this;
224-
}
225-
226169
public Operation build() {
227170
return new Operation(this);
228171
}

quarkus/addons/knative/serving/runtime/src/main/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/PlainJsonKnativeParamsDecorator.java

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,17 @@
1818
*/
1919
package org.kie.kogito.addons.quarkus.knative.serving.customfunctions;
2020

21-
import java.util.HashMap;
2221
import java.util.List;
2322
import java.util.Map;
24-
import java.util.Set;
2523

2624
import org.kie.kogito.event.cloudevents.utils.CloudEventUtils;
2725
import org.kie.kogito.internal.process.workitem.KogitoWorkItem;
2826
import org.kogito.workitem.rest.decorators.PrefixParamsDecorator;
2927

30-
import com.fasterxml.jackson.core.JsonProcessingException;
31-
import com.fasterxml.jackson.databind.JsonNode;
32-
import com.fasterxml.jackson.databind.ObjectMapper;
33-
import com.fasterxml.jackson.databind.node.ObjectNode;
34-
3528
import io.vertx.mutiny.ext.web.client.HttpRequest;
3629

3730
import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.KnativeWorkItemHandler.CLOUDEVENT_SENT_AS_PLAIN_JSON_ERROR_MESSAGE;
3831
import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.KnativeWorkItemHandler.ID;
39-
import static org.kie.kogito.serverless.workflow.SWFConstants.MODEL_WORKFLOW_VAR;
4032

4133
public final class PlainJsonKnativeParamsDecorator extends PrefixParamsDecorator {
4234

@@ -45,44 +37,12 @@ public void decorate(KogitoWorkItem workItem, Map<String, Object> parameters, Ht
4537
if (isCloudEvent(KnativeFunctionPayloadSupplier.getPayload(parameters))) {
4638
throw new IllegalArgumentException(CLOUDEVENT_SENT_AS_PLAIN_JSON_ERROR_MESSAGE);
4739
}
48-
buildFromParams(workItem, parameters, request);
40+
41+
super.decorate(workItem, parameters, request);
4942
}
5043

5144
private static boolean isCloudEvent(Map<String, Object> payload) {
5245
List<String> cloudEventMissingAttributes = CloudEventUtils.getMissingAttributes(payload);
5346
return !payload.isEmpty() && (cloudEventMissingAttributes.isEmpty() || (cloudEventMissingAttributes.size() == 1 && cloudEventMissingAttributes.contains(ID)));
5447
}
55-
56-
private void buildFromParams(KogitoWorkItem workItem, Map<String, Object> parameters, HttpRequest<?> request) {
57-
Map<String, Object> inputModel = new HashMap<>();
58-
59-
Object inputModelObject = parameters.get(MODEL_WORKFLOW_VAR);
60-
61-
ObjectNode inputModelCopy = null;
62-
if (inputModelObject instanceof ObjectNode objectNode) {
63-
ObjectMapper mapper = new ObjectMapper();
64-
objectNode.fields().forEachRemaining(entry -> {
65-
JsonNode value = entry.getValue();
66-
Object rawValue = mapper.convertValue(value, Object.class);
67-
inputModel.put(entry.getKey(), rawValue);
68-
});
69-
70-
try {
71-
inputModelCopy = (ObjectNode) mapper.readTree(objectNode.toString());
72-
} catch (JsonProcessingException e) {
73-
throw new RuntimeException("Failed to copy MODEL_WORKFLOW_VAR", e);
74-
}
75-
}
76-
77-
Set<String> paramsRemove = super.extractHeadersQueries(workItem, inputModel, request);
78-
super.decorate(workItem, parameters, request);
79-
80-
if (inputModelCopy != null) {
81-
// mutate the safe copy
82-
inputModelCopy.remove(paramsRemove);
83-
84-
// replace the original entry in parameters with the copy
85-
parameters.put(MODEL_WORKFLOW_VAR, inputModelCopy);
86-
}
87-
}
8848
}

quarkus/addons/knative/serving/runtime/src/test/java/org/kie/kogito/addons/quarkus/knative/serving/customfunctions/OperationTest.java

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,44 +29,32 @@
2929
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
3030
import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType;
3131
import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.Operation.CLOUD_EVENT_PARAMETER_NAME;
32-
import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.Operation.FAIL_ON_STATUS_ERROR_PARAMETER_NAME;
3332
import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.Operation.METHOD_PARAMETER_NAME;
3433
import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.Operation.PATH_PARAMETER_NAME;
35-
import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.Operation.RETURN_HEADERS_PARAMETER_NAME;
36-
import static org.kie.kogito.addons.quarkus.knative.serving.customfunctions.Operation.RETURN_STATUS_CODE_PARAMETER_NAME;
3734

3835
class OperationTest {
3936

4037
public static final String SERVICE = "service";
4138

4239
public static Stream<Arguments> parseSource() {
4340
return Stream.of(
44-
Arguments.of(SERVICE, Operation.builder().withService(SERVICE).withFailOnStatusError(true).build()),
41+
Arguments.of(SERVICE, Operation.builder().withService(SERVICE).build()),
4542

46-
Arguments.of("service?", Operation.builder().withService(SERVICE).withFailOnStatusError(true).build()),
43+
Arguments.of("service?", Operation.builder().withService(SERVICE).build()),
4744

48-
Arguments.of("service?" + PATH_PARAMETER_NAME + "=/my_path", Operation.builder().withService(SERVICE).withPath("/my_path").withFailOnStatusError(true).build()),
45+
Arguments.of("service?" + PATH_PARAMETER_NAME + "=/my_path", Operation.builder().withService(SERVICE).withPath("/my_path").build()),
4946

50-
Arguments.of("service?" + CLOUD_EVENT_PARAMETER_NAME + "=true", Operation.builder().withService(SERVICE).withFailOnStatusError(true).withIsCloudEvent(true).build()),
47+
Arguments.of("service?" + CLOUD_EVENT_PARAMETER_NAME + "=true", Operation.builder().withService(SERVICE).withIsCloudEvent(true).build()),
5148

52-
Arguments.of("service?" + METHOD_PARAMETER_NAME + "=GET", Operation.builder().withService(SERVICE).withFailOnStatusError(true).withMethod(HttpMethod.GET).build()),
49+
Arguments.of("service?" + METHOD_PARAMETER_NAME + "=GET", Operation.builder().withService(SERVICE).withMethod(HttpMethod.GET).build()),
5350

54-
Arguments.of("service?" + METHOD_PARAMETER_NAME + "=get", Operation.builder().withService(SERVICE).withFailOnStatusError(true).withMethod(HttpMethod.GET).build()),
51+
Arguments.of("service?" + METHOD_PARAMETER_NAME + "=get", Operation.builder().withService(SERVICE).withMethod(HttpMethod.GET).build()),
5552

5653
Arguments.of("service?" + PATH_PARAMETER_NAME + "=/my_path&" + CLOUD_EVENT_PARAMETER_NAME + "=true",
57-
Operation.builder().withService(SERVICE).withPath("/my_path").withFailOnStatusError(true).withIsCloudEvent(true).build()),
54+
Operation.builder().withService(SERVICE).withPath("/my_path").withIsCloudEvent(true).build()),
5855

5956
Arguments.of("service?" + PATH_PARAMETER_NAME + "=/my_path&" + CLOUD_EVENT_PARAMETER_NAME + "=false&" + METHOD_PARAMETER_NAME + "=GET",
60-
Operation.builder().withService(SERVICE).withPath("/my_path").withFailOnStatusError(true).withIsCloudEvent(false).withMethod(HttpMethod.GET).build()),
61-
62-
Arguments.of("service?" + FAIL_ON_STATUS_ERROR_PARAMETER_NAME + "=false",
63-
Operation.builder().withService(SERVICE).withFailOnStatusError(false).build()),
64-
65-
Arguments.of("service?" + RETURN_HEADERS_PARAMETER_NAME + "=true",
66-
Operation.builder().withService(SERVICE).withFailOnStatusError(true).withReturnHeaders(true).build()),
67-
68-
Arguments.of("service?" + RETURN_STATUS_CODE_PARAMETER_NAME + "=true",
69-
Operation.builder().withService(SERVICE).withFailOnStatusError(true).withReturnStatusCode(true).build()));
57+
Operation.builder().withService(SERVICE).withPath("/my_path").withIsCloudEvent(false).withMethod(HttpMethod.GET).build()));
7058
}
7159

7260
public static Stream<Arguments> invalidOperationSource() {

0 commit comments

Comments
 (0)