[BUG][Java][client] Not possible to send payloads on DELETE requests
Created by: bkabrda
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
What's the version of OpenAPI Generator used? -
Have you search for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Bounty to sponsor the fix (example)
Description
I've just hit an issue with Jersey client which doesn't make it possible at all to send payloads with DELETE requests. While the HTTP spec says that semantics of DELETE payload is undefined, there is nothing that strictly prohibits sending payloads with DELETE requests (and I'm actually generating a client for API that needs payloads on DELETE :/ ).
I think this should be allowed in the following way:
- If the body passed in is null, no payload will be sent (not even empty object).
- If the body passed in is not null, payload will be sent in the same way as it is for e.g. POST and other methods.
Does that sound like a good compromise?
openapi-generator version
4.1.0 (but seems to be problem in all versions AFAICS)
OpenAPI declaration file content or url
Command line used for generation
Steps to reproduce
Generate a jersey2 client from OpenAPI spec that defines a json payload on DELETE request, try to run invoke the method for deletion with a non-null payload - no payload is sent.
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/3276
Suggest a fix
I'm using the following patch to make this work. If it looks ok, I can submit it as a PR (probably after https://github.com/OpenAPITools/openapi-generator/pull/3703 is merged, since it touches same parts of code and my patch might need to change depending on the final changes in #3703).
diff --git a/Java/libraries/jersey2/ApiClient.mustache b/Java/libraries/jersey2/ApiClient.mustache
index ab6a4a1353..579dc1ae1c 100644
--- a/Java/libraries/jersey2/ApiClient.mustache
+++ b/Java/libraries/jersey2/ApiClient.mustache
@@ -716,7 +716,12 @@ public class ApiClient {
} else if ("PUT".equals(method)) {
response = invocationBuilder.put(entity);
} else if ("DELETE".equals(method)) {
- response = invocationBuilder.delete();
+ // if body is null or Object, no entity was passed in => call delete without payload
+ if (body == null || body.getClass() == java.lang.Object.class) {
+ response = invocationBuilder.delete();
+ } else {
+ response = invocationBuilder.method("DELETE", entity);
+ }
} else if ("PATCH".equals(method)) {
response = invocationBuilder.method("PATCH", entity);
} else if ("HEAD".equals(method)) {
@@ -776,6 +781,8 @@ public class ApiClient {
clientConfig.register(json);
clientConfig.register(JacksonFeature.class);
clientConfig.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
+ // turn off compliance validation to be able to send payloads with DELETE calls
+ clientConfig.property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
if (debugging) {
{{^supportJava6}}
clientConfig.register(new LoggingFeature(java.util.logging.Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME), java.util.logging.Level.INFO, LoggingFeature.Verbosity.PAYLOAD_ANY, 1024*50 /* Log payloads up to 50K */));
@@ -786,6 +793,9 @@ public class ApiClient {
{{#supportJava6}}
clientConfig.register(new LoggingFilter(java.util.logging.Logger.getLogger(LoggingFilter.class.getName()), true));
{{/supportJava6}}
+ } else {
+ // suppress warnings for payloads with DELETE calls
+ java.util.logging.Logger.getLogger("org.glassfish.jersey").setLevel(java.util.logging.Level.SEVERE);
}
performAdditionalClientConfiguration(clientConfig);
return ClientBuilder.newClient(clientConfig);