Skip to content

Commit f485b62

Browse files
fjtiradorgdoliveira
authored andcommitted
[KOGITO-9861] Fixing compilation issues with nested classes and arrays (apache#3242)
* [KOGITO-9861] Fixing compilation issues with nested and arrays * [KOGITO-9861] Adding IT test
1 parent 5620198 commit f485b62

File tree

8 files changed

+198
-8
lines changed

8 files changed

+198
-8
lines changed

kogito-serverless-workflow/kogito-serverless-workflow-runtime/src/main/java/org/kie/kogito/serverless/workflow/WorkflowWorkItemHandler.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.kie.kogito.internal.process.runtime.KogitoWorkItemHandler;
2727
import org.kie.kogito.internal.process.runtime.KogitoWorkItemManager;
2828
import org.kie.kogito.jackson.utils.JsonObjectUtils;
29-
import org.kie.kogito.jackson.utils.ObjectMapperFactory;
3029
import org.slf4j.Logger;
3130
import org.slf4j.LoggerFactory;
3231

@@ -48,10 +47,13 @@ public void executeWorkItem(KogitoWorkItem workItem, KogitoWorkItemManager manag
4847
protected static <V> V buildBody(Map<String, Object> params, Class<V> clazz) {
4948
for (Object obj : params.values()) {
5049
if (obj != null && clazz.isAssignableFrom(obj.getClass())) {
50+
logger.trace("Invoking workitemhandler with value {}", obj);
5151
return clazz.cast(obj);
5252
}
5353
}
54-
return ObjectMapperFactory.get().convertValue(params, clazz);
54+
V value = JsonObjectUtils.convertValue(params, clazz);
55+
logger.trace("Invoking workitemhandler with value {}", value);
56+
return value;
5557
}
5658

5759
@Override

quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/quarkus/serverless/workflow/ClassAnnotatedWorkflowHandlerGenerator.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.kie.kogito.codegen.api.context.KogitoBuildContext;
3131

3232
import com.github.javaparser.ast.NodeList;
33+
import com.github.javaparser.ast.type.ClassOrInterfaceType;
3334

3435
import static com.github.javaparser.StaticJavaParser.parseClassOrInterfaceType;
3536
import static com.github.javaparser.StaticJavaParser.parseType;
@@ -46,16 +47,31 @@ public Collection<WorkflowHandlerGeneratedFile> generateHandlerClasses(KogitoBui
4647
protected abstract Stream<WorkflowHandlerGeneratedFile> generateHandler(KogitoBuildContext context, AnnotationInstance a);
4748

4849
protected final com.github.javaparser.ast.type.Type fromClass(Type param) {
50+
return fromClass(param, true);
51+
}
52+
53+
protected final com.github.javaparser.ast.type.Type fromClass(Type param, boolean includeGeneric) {
4954
switch (param.kind()) {
5055
case CLASS:
51-
return parseClassOrInterfaceType(param.asClassType().name().toString());
56+
return parseClassOrInterfaceType(fromDotName(param.asClassType().name()));
5257
case PRIMITIVE:
53-
return parseType(param.asPrimitiveType().name().toString());
58+
return parseType(fromDotName(param.asPrimitiveType().name()));
5459
case PARAMETERIZED_TYPE:
55-
return parseClassOrInterfaceType(param.asParameterizedType().name().toString())
56-
.setTypeArguments(NodeList.nodeList(param.asParameterizedType().arguments().stream().map(this::fromClass).collect(Collectors.toList())));
60+
ClassOrInterfaceType result = parseClassOrInterfaceType(fromDotName(param.asParameterizedType().name()));
61+
if (includeGeneric) {
62+
result.setTypeArguments(NodeList.nodeList(param.asParameterizedType().arguments().stream().map(this::fromClass).collect(Collectors.toList())));
63+
}
64+
return result;
5765
default:
5866
throw new UnsupportedOperationException("Kind " + param.kind() + " is not supported");
5967
}
6068
}
69+
70+
private String fromDotName(DotName dotName) {
71+
String result = dotName.toString();
72+
if (dotName.isInner()) {
73+
result = result.replace('$', '.');
74+
}
75+
return result;
76+
}
6177
}

quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-deployment/src/main/java/org/kie/kogito/quarkus/serverless/workflow/openapi/WorkflowOpenApiHandlerGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ private WorkflowHandlerGeneratedFile generateHandler(KogitoBuildContext context,
109109
if (annotation != null) {
110110
methodCallExpr.addArgument(new CastExpr(fromClass(param), new MethodCallExpr(parameters, "remove").addArgument(new StringLiteralExpr(annotation.value().asString()))));
111111
} else {
112-
methodCallExpr.addArgument(new MethodCallExpr("buildBody").addArgument(parameters).addArgument(new ClassExpr(fromClass(param))));
112+
methodCallExpr.addArgument(new MethodCallExpr("buildBody").addArgument(parameters).addArgument(new ClassExpr(fromClass(param, false))));
113113
}
114114
}
115115
clazz.addMethod("getRestClass", Keyword.PROTECTED).setType(parseClassOrInterfaceType(Class.class.getCanonicalName()).setTypeArguments(classNameType))

quarkus/extensions/kogito-quarkus-serverless-workflow-extension/kogito-quarkus-serverless-workflow-integration-test/src/main/resources/application.properties

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ quarkus.flyway.clean-at-start=true
99

1010
quarkus.http.test-port=0
1111
quarkus.log.level=INFO
12-
#quarkus.log.category."org.kie.kogito".level=DEBUG
12+
#quarkus.log.category."org.kie.kogito.serverless.workflow".level=DEBUG
1313

1414
# To include the greethidden workflow
1515
kogito.codegen.ignoreHiddenFiles=false
@@ -20,6 +20,7 @@ quarkus.kubernetes-client.devservices.enabled=false
2020
# OpenApi client properties, see OperationsMockService, which is mocking these two services
2121
quarkus.rest-client.multiplication.url=${multiplication-service-mock.url}
2222
quarkus.rest-client.subtraction.url=${subtraction-service-mock.url}
23+
quarkus.rest-client.array_yaml.url=${array-service-mock.url}
2324

2425
# OpenApi client properties to access the general purpose external-service, which is mocked by the ExternalServiceMock
2526
quarkus.rest-client.external_service_yaml.url=${external-service-mock.url}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"id": "openapiarray",
3+
"name": "Open API Array Test",
4+
"version": "v1.0",
5+
"start": "DoIt",
6+
"functions": [
7+
{
8+
"name": "testArray",
9+
"operation": "specs/array.yaml#testArray"
10+
}
11+
],
12+
"states": [
13+
{
14+
"name": "DoIt",
15+
"type": "operation",
16+
"actions": [
17+
{
18+
"name": "testArray",
19+
"functionRef": {
20+
"refName": "testArray",
21+
"arguments": ".inputArray"
22+
}
23+
}],
24+
"end": true
25+
}
26+
]
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
openapi: 3.0.3
3+
info:
4+
title: Generated API
5+
version: "1.0"
6+
paths:
7+
/testArray:
8+
post:
9+
operationId: testArray
10+
requestBody:
11+
content:
12+
application/json:
13+
schema:
14+
type: array
15+
items:
16+
type: number
17+
responses:
18+
"200":
19+
description: OK
20+
content:
21+
application/json:
22+
schema:
23+
type: array
24+
items:
25+
type: number
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.kie.kogito.quarkus.workflows;
20+
21+
import java.util.Arrays;
22+
import java.util.Collections;
23+
24+
import org.junit.jupiter.api.BeforeAll;
25+
import org.junit.jupiter.api.Test;
26+
27+
import io.quarkus.test.common.QuarkusTestResource;
28+
import io.quarkus.test.junit.QuarkusIntegrationTest;
29+
import io.restassured.RestAssured;
30+
import io.restassured.http.ContentType;
31+
32+
import static io.restassured.RestAssured.given;
33+
import static org.hamcrest.CoreMatchers.is;
34+
import static org.hamcrest.CoreMatchers.notNullValue;
35+
36+
@QuarkusTestResource(OpenAPIArrayMockService.class)
37+
@QuarkusIntegrationTest
38+
class OpenAPIArrayFlowIT {
39+
40+
@BeforeAll
41+
static void init() {
42+
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails();
43+
}
44+
45+
@Test
46+
void testArray() {
47+
given()
48+
.contentType(ContentType.JSON)
49+
.when()
50+
.body(Collections.singletonMap("inputArray", Arrays.asList(1, 2, 3, 4)))
51+
.post("/openapiarray")
52+
.then()
53+
.statusCode(201)
54+
.body("id", notNullValue())
55+
.body("workflowdata.response", is(Arrays.asList(1, 2, 3, 4)));
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.kie.kogito.quarkus.workflows;
20+
21+
import java.util.Map;
22+
import java.util.function.UnaryOperator;
23+
24+
import com.github.tomakehurst.wiremock.WireMockServer;
25+
import com.github.tomakehurst.wiremock.client.MappingBuilder;
26+
27+
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
28+
29+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
30+
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
31+
import static com.github.tomakehurst.wiremock.client.WireMock.post;
32+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
33+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
34+
35+
public class OpenAPIArrayMockService implements QuarkusTestResourceLifecycleManager {
36+
37+
private static WireMockServer arrayService;
38+
39+
@Override
40+
public Map<String, String> start() {
41+
arrayService = startServer("[1,2,3,4]", p -> p);
42+
return Map.of("array-service-mock.url", arrayService.baseUrl());
43+
}
44+
45+
@Override
46+
public void stop() {
47+
if (arrayService != null) {
48+
arrayService.stop();
49+
}
50+
}
51+
52+
private static WireMockServer startServer(final String response, UnaryOperator<MappingBuilder> function) {
53+
final WireMockServer server = new WireMockServer(options().dynamicPort());
54+
server.start();
55+
server.stubFor(function.apply(post(urlEqualTo("/testArray")).withRequestBody(equalToJson("[1,2,3,4]")))
56+
.withPort(server.port())
57+
.willReturn(aResponse()
58+
.withHeader("Content-Type", "application/json")
59+
.withBody(response)));
60+
return server;
61+
}
62+
}

0 commit comments

Comments
 (0)