camunda-community-hub / camunda-platform-7-rest-client-java

Community extension to generate a Java client from the provided Camunda 7 OpenAPI descitpion and also warp it into Spring Boot
Apache License 2.0
57 stars 10 forks source link

Message Correlation fails with ApiException due to invalid UTF-8 encoding #107

Closed lmoesle closed 7 months ago

lmoesle commented 8 months ago

Message Correlation fails with an ApiException if the CorrelationMessageDto Object contains any special Characters (Umlaute like ä, ö, ü, ß, ...).

org.camunda.community.rest.client.invoker.ApiException: Invalid UTF-8 middle byte 0x74

I did do some debugging and the api client seems to use the wrong charset (ISO_8859_1 instead of UTF-8). The error occurs in the generated ApiClient when the Content-Type is application/json and the charset null. The default charset is used for the request which somehow is ISO_8859_1. This causes the serializer to serialize the object to invalid json.

The error started occuring after migrating our application to spring boot 3.1.5 and upgrading the camunda-engine-rest-client-openapi-java to 7.20.2.

FYI these are the logs of our application:

// call stack
de/muenchen/oss/digiwf/camunda/connector/message/MessageServiceImpl.java:52
org/camunda/community/camunda-engine-rest-client-openapi-java/7.20.2/camunda-engine-rest-client-openapi-java-7.20.2-sources.jar!/org/camunda/community/rest/client/api/MessageApi.java:76
org/camunda/community/camunda-engine-rest-client-openapi-java/7.20.2/camunda-engine-rest-client-openapi-java-7.20.2-sources.jar!/org/camunda/community/rest/client/invoker/ApiClient.java:1033
// "wrong" content type is loaded
org/camunda/community/camunda-engine-rest-client-openapi-java/7.20.2/camunda-engine-rest-client-openapi-java-7.20.2-sources.jar!/org/camunda/community/rest/client/invoker/ApiClient.java:703->getContentType
// call serialization
org/camunda/community/camunda-engine-rest-client-openapi-java/7.20.2/camunda-engine-rest-client-openapi-java-7.20.2-sources.jar!/org/camunda/community/rest/client/invoker/ApiClient.java:1037
// create StringEntity
org/camunda/community/camunda-engine-rest-client-openapi-java/7.20.2/camunda-engine-rest-client-openapi-java-7.20.2-sources.jar!/org/camunda/community/rest/client/invoker/ApiClient.java:735
// default charset is loaded
org/apache/httpcomponents/core5/httpcore5/5.2.3/httpcore5-5.2.3-sources.jar!/org/apache/hc/core5/http/io/entity/StringEntity.java:65

This is a snippet of our code calling camunda-engine-rest-client-openapi-java Lib:

public class MessageServiceImpl implements MessageService {  

    private final MessageApi messageApi;  

    @Override  
    public void correlateMessage(final CorrelateMessage correlateMessage) {  
        // [...]
        this.messageApi.deliverMessage(correlationMessageDto);  
    }  

}
lmoesle commented 8 months ago

I found out that there is another Camunda Community Rest Api implementation (https://github.com/camunda-community-hub/camunda-platform-7-rest-client-spring-boot). We swapped the dependency in our application which fixed the error

dtissera commented 7 months ago

I had same issue and fix it by sub classing ApiClient

`@Component @Primary public class Utf8CamundaApiClient extends ApiClient {

@Override
public <T> T invokeAPI(String path, String method, List<Pair> queryParams, List<Pair> collectionQueryParams,
                       String urlQueryDeepObject, Object body, Map<String, String> headerParams, Map<String, String> cookieParams,
                       Map<String, Object> formParams, String accept, String contentType, String[] authNames,
                       TypeReference<T> returnType) throws ApiException {
    if ("application/json".equals(contentType)) {
        contentType = "application/json;charset=utf-8";
    }

    return super.invokeAPI(path, method, queryParams, collectionQueryParams, urlQueryDeepObject, body, headerParams,
            cookieParams, formParams, accept, contentType, authNames, returnType);
}

@PostConstruct
public void initMe() {
    Configuration.setDefaultApiClient(this);
}

}`

berndruecker commented 7 months ago

@holgerhagen is this what we discussed - fixed by https://github.com/camunda-community-hub/camunda-platform-7-rest-client-java/pull/109?

holgerhagen commented 7 months ago

@berndruecker yes, the update of the openapi-generator should fix this issue as well.