From 6573fea690b77b2e6946b75228edb65623a6cfd4 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Thu, 11 Jun 2020 18:06:00 -0700 Subject: [PATCH 1/2] Differentiate request with no body vs request that contains the null value --- .../Java/libraries/jersey2/ApiClient.mustache | 25 ++++-- .../Java/libraries/jersey2/api.mustache | 5 +- .../org/openapitools/client/ApiClient.java | 85 +++++++++++++++++-- .../client/api/AnotherFakeApi.java | 5 +- .../org/openapitools/client/api/FakeApi.java | 70 ++++++++++++--- .../client/api/FakeClassnameTags123Api.java | 5 +- .../org/openapitools/client/api/PetApi.java | 45 ++++++++-- .../org/openapitools/client/api/StoreApi.java | 20 ++++- .../org/openapitools/client/api/UserApi.java | 40 +++++++-- .../org/openapitools/client/ApiClient.java | 25 ++++-- .../client/api/AnotherFakeApi.java | 5 +- .../openapitools/client/api/DefaultApi.java | 5 +- .../org/openapitools/client/api/FakeApi.java | 75 ++++++++++++---- .../client/api/FakeClassnameTags123Api.java | 5 +- .../org/openapitools/client/api/PetApi.java | 45 ++++++++-- .../org/openapitools/client/api/StoreApi.java | 20 ++++- .../org/openapitools/client/api/UserApi.java | 40 +++++++-- 17 files changed, 424 insertions(+), 96 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache index 1b2b63c9abe0..08d2ce28b902 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache @@ -832,7 +832,7 @@ public class ApiClient { * @return Entity * @throws ApiException API exception */ - public Entity serialize(Object obj, Map formParams, String contentType) throws ApiException { + public Entity serialize(BodyHolder obj, Map formParams, String contentType) throws ApiException { Entity entity; if (contentType.startsWith("multipart/form-data")) { MultiPart multiPart = new MultiPart(); @@ -856,7 +856,7 @@ public class ApiClient { entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE); } else { // We let jersey handle the serialization - entity = Entity.entity(obj == null ? Entity.text("") : obj, contentType); + entity = Entity.entity(obj == null ? Entity.text("") : obj.body, contentType); } return entity; } @@ -870,7 +870,7 @@ public class ApiClient { * @return String * @throws ApiException API exception */ - public String serializeToString(Object obj, Map formParams, String contentType) throws ApiException { + public String serializeToString(BodyHolder obj, Map formParams, String contentType) throws ApiException { try { if (contentType.startsWith("multipart/form-data")) { throw new ApiException("multipart/form-data not yet supported for serializeToString (http signature authentication)"); @@ -886,7 +886,7 @@ public class ApiClient { return formString.substring(0, formString.length() - 1); } } else { - return json.getMapper().writeValueAsString(obj); + return obj == null ? "" : json.getMapper().writeValueAsString(obj.body); } } catch (Exception ex) { throw new ApiException("Failed to perform serializeToString: " + ex.toString()); @@ -984,6 +984,19 @@ public class ApiClient { return File.createTempFile(prefix, suffix, new File(tempFolderPath)); } + /** + * A wrapper class for the request body. + * + * This is needed to differentiate between a request without body (e.g. HTTP GET) + * versus a request with the 'null' value in the body. + */ + public static class BodyHolder { + private Object body; + public BodyHolder(Object body) { + this.body = body; + } + } + /** * Invoke API by sending HTTP request with the given options. * @@ -1008,7 +1021,7 @@ public class ApiClient { String path, String method, List queryParams, - Object body, + BodyHolder body, Map headerParams, Map cookieParams, Map formParams, @@ -1163,7 +1176,7 @@ public class ApiClient { * @deprecated Add qualified name of the operation as a first parameter. */ @Deprecated - public ApiResponse invokeAPI(String path, String method, List queryParams, Object body, Map headerParams, Map cookieParams, Map formParams, String accept, String contentType, String[] authNames, GenericType returnType) throws ApiException { + public ApiResponse invokeAPI(String path, String method, List queryParams, BodyHolder body, Map headerParams, Map cookieParams, Map formParams, String accept, String contentType, String[] authNames, GenericType returnType) throws ApiException { return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType); } diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/api.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/api.mustache index 16f7ef45b8c0..74305821c1ad 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/api.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/api.mustache @@ -116,7 +116,10 @@ public class {{classname}} { @Deprecated {{/isDeprecated}} public{{/vendorExtensions.x-group-parameters}}{{#vendorExtensions.x-group-parameters}}private{{/vendorExtensions.x-group-parameters}} ApiResponse<{{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}Void{{/returnType}}> {{operationId}}WithHttpInfo({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws ApiException { - Object localVarPostBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = {{#bodyParam}}new ApiClient.BodyHolder({{paramName}}){{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; {{#allParams}}{{#required}} // verify the required parameter '{{paramName}}' is set if ({{paramName}} == null) { diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java index 988593ca6657..2cab1b120459 100644 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java @@ -25,6 +25,13 @@ import java.io.InputStream; import java.net.URI; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import java.security.cert.X509Certificate; +import java.security.KeyManagementException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import org.glassfish.jersey.logging.LoggingFeature; @@ -749,7 +756,7 @@ public String escapeString(String str) { * @return Entity * @throws ApiException API exception */ - public Entity serialize(Object obj, Map formParams, String contentType) throws ApiException { + public Entity serialize(BodyHolder obj, Map formParams, String contentType) throws ApiException { Entity entity; if (contentType.startsWith("multipart/form-data")) { MultiPart multiPart = new MultiPart(); @@ -773,7 +780,7 @@ public Entity serialize(Object obj, Map formParams, String co entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE); } else { // We let jersey handle the serialization - entity = Entity.entity(obj == null ? Entity.text("") : obj, contentType); + entity = Entity.entity(obj == null ? Entity.text("") : obj.body, contentType); } return entity; } @@ -787,7 +794,7 @@ public Entity serialize(Object obj, Map formParams, String co * @return String * @throws ApiException API exception */ - public String serializeToString(Object obj, Map formParams, String contentType) throws ApiException { + public String serializeToString(BodyHolder obj, Map formParams, String contentType) throws ApiException { try { if (contentType.startsWith("multipart/form-data")) { throw new ApiException("multipart/form-data not yet supported for serializeToString (http signature authentication)"); @@ -803,7 +810,7 @@ public String serializeToString(Object obj, Map formParams, Stri return formString.substring(0, formString.length() - 1); } } else { - return json.getMapper().writeValueAsString(obj); + return obj == null ? "" : json.getMapper().writeValueAsString(obj.body); } } catch (Exception ex) { throw new ApiException("Failed to perform serializeToString: " + ex.toString()); @@ -895,6 +902,19 @@ public File prepareDownloadFile(Response response) throws IOException { return File.createTempFile(prefix, suffix, new File(tempFolderPath)); } + /** + * A wrapper class for the request body. + * + * This is needed to differentiate between a request without body (e.g. HTTP GET) + * versus a request with the 'null' value in the body. + */ + public static class BodyHolder { + private Object body; + public BodyHolder(Object body) { + this.body = body; + } + } + /** * Invoke API by sending HTTP request with the given options. * @@ -919,7 +939,7 @@ public ApiResponse invokeAPI( String path, String method, List queryParams, - Object body, + BodyHolder body, Map headerParams, Map cookieParams, Map formParams, @@ -1072,7 +1092,7 @@ private Response sendRequest(String method, Invocation.Builder invocationBuilder * @deprecated Add qualified name of the operation as a first parameter. */ @Deprecated - public ApiResponse invokeAPI(String path, String method, List queryParams, Object body, Map headerParams, Map cookieParams, Map formParams, String accept, String contentType, String[] authNames, GenericType returnType) throws ApiException { + public ApiResponse invokeAPI(String path, String method, List queryParams, BodyHolder body, Map headerParams, Map cookieParams, Map formParams, String accept, String contentType, String[] authNames, GenericType returnType) throws ApiException { return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType); } @@ -1099,13 +1119,64 @@ protected Client buildHttpClient(boolean debugging) { java.util.logging.Logger.getLogger("org.glassfish.jersey.client").setLevel(java.util.logging.Level.SEVERE); } performAdditionalClientConfiguration(clientConfig); - return ClientBuilder.newClient(clientConfig); + ClientBuilder clientBuilder = ClientBuilder.newBuilder(); + customizeClientBuilder(clientBuilder); + clientBuilder = clientBuilder.withConfig(clientConfig); + return clientBuilder.build(); } + /** + * Perform additional configuration of the API client. + * This method can be overriden to customize the API client. + */ protected void performAdditionalClientConfiguration(ClientConfig clientConfig) { // No-op extension point } + /** + * Customize the client builder. + * + * This method can be overriden to customize the API client. For example, this can be used to: + * 1. Set the hostname verifier to be used by the client to verify the endpoint's hostname + * against its identification information. + * 2. Set the client-side key store. + * 3. Set the SSL context that will be used when creating secured transport connections to + * server endpoints from web targets created by the client instance that is using this SSL context. + * 4. Set the client-side trust store. + * + * To completely disable certificate validation (at your own risk), you can + * override this method and invoke disableCertificateValidation(clientBuilder). + */ + protected void customizeClientBuilder(ClientBuilder clientBuilder) { + // No-op extension point + } + + /** + * Disable X.509 certificate validation in TLS connections. + * + * Please note that trusting all certificates is extremely risky. + * This may be useful in a development environment with self-signed certificates. + */ + protected void disableCertificateValidation(ClientBuilder clientBuilder) throws KeyManagementException, NoSuchAlgorithmException { + TrustManager[] trustAllCerts = new X509TrustManager[] { + new X509TrustManager() { + @Override + public X509Certificate[] getAcceptedIssuers() { + return null; + } + @Override + public void checkClientTrusted(X509Certificate[] certs, String authType) { + } + @Override + public void checkServerTrusted(X509Certificate[] certs, String authType) { + } + } + }; + SSLContext sslContext = SSLContext.getInstance("TLS"); + sslContext.init(null, trustAllCerts, new SecureRandom()); + clientBuilder.sslContext(sslContext); + } + protected Map> buildResponseHeaders(Response response) { Map> responseHeaders = new HashMap>(); for (Entry> entry: response.getHeaders().entrySet()) { diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/AnotherFakeApi.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/AnotherFakeApi.java index 1576efe930f3..496e79e276b7 100644 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/AnotherFakeApi.java +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/AnotherFakeApi.java @@ -74,7 +74,10 @@ public Client call123testSpecialTags(Client body) throws ApiException { */ public ApiResponse call123testSpecialTagsWithHttpInfo(Client body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // verify the required parameter 'body' is set if (body == null) { diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeApi.java index ded3b3ede9ae..85f240573440 100644 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeApi.java @@ -81,7 +81,10 @@ public void createXmlItem(XmlItem xmlItem) throws ApiException { */ public ApiResponse createXmlItemWithHttpInfo(XmlItem xmlItem) throws ApiException { - Object localVarPostBody = xmlItem; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(xmlItem); // verify the required parameter 'xmlItem' is set if (xmlItem == null) { @@ -146,7 +149,10 @@ public Boolean fakeOuterBooleanSerialize(Boolean body) throws ApiException { */ public ApiResponse fakeOuterBooleanSerializeWithHttpInfo(Boolean body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // create path and map variables String localVarPath = "/fake/outer/boolean"; @@ -208,7 +214,10 @@ public OuterComposite fakeOuterCompositeSerialize(OuterComposite body) throws Ap */ public ApiResponse fakeOuterCompositeSerializeWithHttpInfo(OuterComposite body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // create path and map variables String localVarPath = "/fake/outer/composite"; @@ -270,7 +279,10 @@ public BigDecimal fakeOuterNumberSerialize(BigDecimal body) throws ApiException */ public ApiResponse fakeOuterNumberSerializeWithHttpInfo(BigDecimal body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // create path and map variables String localVarPath = "/fake/outer/number"; @@ -332,7 +344,10 @@ public String fakeOuterStringSerialize(String body) throws ApiException { */ public ApiResponse fakeOuterStringSerializeWithHttpInfo(String body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // create path and map variables String localVarPath = "/fake/outer/string"; @@ -393,7 +408,10 @@ public void testBodyWithFileSchema(FileSchemaTestClass body) throws ApiException */ public ApiResponse testBodyWithFileSchemaWithHttpInfo(FileSchemaTestClass body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // verify the required parameter 'body' is set if (body == null) { @@ -459,7 +477,10 @@ public void testBodyWithQueryParams(String query, User body) throws ApiException */ public ApiResponse testBodyWithQueryParamsWithHttpInfo(String query, User body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // verify the required parameter 'query' is set if (query == null) { @@ -530,7 +551,10 @@ public Client testClientModel(Client body) throws ApiException { */ public ApiResponse testClientModelWithHttpInfo(Client body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // verify the required parameter 'body' is set if (body == null) { @@ -624,7 +648,10 @@ public void testEndpointParameters(BigDecimal number, Double _double, String pat */ public ApiResponse testEndpointParametersWithHttpInfo(BigDecimal number, Double _double, String patternWithoutDelimiter, byte[] _byte, Integer integer, Integer int32, Long int64, Float _float, String string, File binary, LocalDate date, OffsetDateTime dateTime, String password, String paramCallback) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'number' is set if (number == null) { @@ -747,7 +774,10 @@ public void testEnumParameters(List enumHeaderStringArray, String enumHe */ public ApiResponse testEnumParametersWithHttpInfo(List enumHeaderStringArray, String enumHeaderString, List enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, List enumFormStringArray, String enumFormString) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // create path and map variables String localVarPath = "/fake"; @@ -792,7 +822,10 @@ public ApiResponse testEnumParametersWithHttpInfo(List enumHeaderS } private ApiResponse testGroupParametersWithHttpInfo(Integer requiredStringGroup, Boolean requiredBooleanGroup, Long requiredInt64Group, Integer stringGroup, Boolean booleanGroup, Long int64Group) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'requiredStringGroup' is set if (requiredStringGroup == null) { @@ -989,7 +1022,10 @@ public void testInlineAdditionalProperties(Map param) throws Api */ public ApiResponse testInlineAdditionalPropertiesWithHttpInfo(Map param) throws ApiException { - Object localVarPostBody = param; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(param); // verify the required parameter 'param' is set if (param == null) { @@ -1055,7 +1091,10 @@ public void testJsonFormData(String param, String param2) throws ApiException { */ public ApiResponse testJsonFormDataWithHttpInfo(String param, String param2) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'param' is set if (param == null) { @@ -1136,7 +1175,10 @@ public void testQueryParameterCollectionFormat(List pipe, List i */ public ApiResponse testQueryParameterCollectionFormatWithHttpInfo(List pipe, List ioutil, List http, List url, List context) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'pipe' is set if (pipe == null) { diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java index bd0474e830f9..b6a379ef4dfa 100644 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java @@ -74,7 +74,10 @@ public Client testClassname(Client body) throws ApiException { */ public ApiResponse testClassnameWithHttpInfo(Client body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // verify the required parameter 'body' is set if (body == null) { diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/PetApi.java index 59754440ec22..824fa19ac817 100644 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/PetApi.java @@ -78,7 +78,10 @@ public void addPet(Pet body) throws ApiException { */ public ApiResponse addPetWithHttpInfo(Pet body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // verify the required parameter 'body' is set if (body == null) { @@ -146,7 +149,10 @@ public void deletePet(Long petId, String apiKey) throws ApiException { */ public ApiResponse deletePetWithHttpInfo(Long petId, String apiKey) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -216,7 +222,10 @@ public List findPetsByStatus(List status) throws ApiException { */ public ApiResponse> findPetsByStatusWithHttpInfo(List status) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'status' is set if (status == null) { @@ -290,7 +299,10 @@ public Set findPetsByTags(Set tags) throws ApiException { */ @Deprecated public ApiResponse> findPetsByTagsWithHttpInfo(Set tags) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'tags' is set if (tags == null) { @@ -362,7 +374,10 @@ public Pet getPetById(Long petId) throws ApiException { */ public ApiResponse getPetByIdWithHttpInfo(Long petId) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -435,7 +450,10 @@ public void updatePet(Pet body) throws ApiException { */ public ApiResponse updatePetWithHttpInfo(Pet body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // verify the required parameter 'body' is set if (body == null) { @@ -503,7 +521,10 @@ public void updatePetWithForm(Long petId, String name, String status) throws Api */ public ApiResponse updatePetWithFormWithHttpInfo(Long petId, String name, String status) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -577,7 +598,10 @@ public ModelApiResponse uploadFile(Long petId, String additionalMetadata, File f */ public ApiResponse uploadFileWithHttpInfo(Long petId, String additionalMetadata, File file) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -653,7 +677,10 @@ public ModelApiResponse uploadFileWithRequiredFile(Long petId, File requiredFile */ public ApiResponse uploadFileWithRequiredFileWithHttpInfo(Long petId, File requiredFile, String additionalMetadata) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'petId' is set if (petId == null) { diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/StoreApi.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/StoreApi.java index c7e049118e22..96c5885301c3 100644 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/StoreApi.java +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/StoreApi.java @@ -75,7 +75,10 @@ public void deleteOrder(String orderId) throws ApiException { */ public ApiResponse deleteOrderWithHttpInfo(String orderId) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'orderId' is set if (orderId == null) { @@ -139,7 +142,10 @@ public Map getInventory() throws ApiException { */ public ApiResponse> getInventoryWithHttpInfo() throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // create path and map variables String localVarPath = "/store/inventory"; @@ -205,7 +211,10 @@ public Order getOrderById(Long orderId) throws ApiException { */ public ApiResponse getOrderByIdWithHttpInfo(Long orderId) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'orderId' is set if (orderId == null) { @@ -275,7 +284,10 @@ public Order placeOrder(Order body) throws ApiException { */ public ApiResponse placeOrderWithHttpInfo(Order body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // verify the required parameter 'body' is set if (body == null) { diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/UserApi.java index ae830b498492..8a7057c3230d 100644 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/UserApi.java @@ -73,7 +73,10 @@ public void createUser(User body) throws ApiException { */ public ApiResponse createUserWithHttpInfo(User body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // verify the required parameter 'body' is set if (body == null) { @@ -137,7 +140,10 @@ public void createUsersWithArrayInput(List body) throws ApiException { */ public ApiResponse createUsersWithArrayInputWithHttpInfo(List body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // verify the required parameter 'body' is set if (body == null) { @@ -201,7 +207,10 @@ public void createUsersWithListInput(List body) throws ApiException { */ public ApiResponse createUsersWithListInputWithHttpInfo(List body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // verify the required parameter 'body' is set if (body == null) { @@ -267,7 +276,10 @@ public void deleteUser(String username) throws ApiException { */ public ApiResponse deleteUserWithHttpInfo(String username) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'username' is set if (username == null) { @@ -337,7 +349,10 @@ public User getUserByName(String username) throws ApiException { */ public ApiResponse getUserByNameWithHttpInfo(String username) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'username' is set if (username == null) { @@ -409,7 +424,10 @@ public String loginUser(String username, String password) throws ApiException { */ public ApiResponse loginUserWithHttpInfo(String username, String password) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'username' is set if (username == null) { @@ -480,7 +498,10 @@ public void logoutUser() throws ApiException { */ public ApiResponse logoutUserWithHttpInfo() throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // create path and map variables String localVarPath = "/user/logout"; @@ -543,7 +564,10 @@ public void updateUser(String username, User body) throws ApiException { */ public ApiResponse updateUserWithHttpInfo(String username, User body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // verify the required parameter 'username' is set if (username == null) { diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java index e08de1a0cd72..f6c2bdd5ef3e 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java @@ -828,7 +828,7 @@ public String escapeString(String str) { * @return Entity * @throws ApiException API exception */ - public Entity serialize(Object obj, Map formParams, String contentType) throws ApiException { + public Entity serialize(BodyHolder obj, Map formParams, String contentType) throws ApiException { Entity entity; if (contentType.startsWith("multipart/form-data")) { MultiPart multiPart = new MultiPart(); @@ -852,7 +852,7 @@ public Entity serialize(Object obj, Map formParams, String co entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE); } else { // We let jersey handle the serialization - entity = Entity.entity(obj == null ? Entity.text("") : obj, contentType); + entity = Entity.entity(obj == null ? Entity.text("") : obj.body, contentType); } return entity; } @@ -866,7 +866,7 @@ public Entity serialize(Object obj, Map formParams, String co * @return String * @throws ApiException API exception */ - public String serializeToString(Object obj, Map formParams, String contentType) throws ApiException { + public String serializeToString(BodyHolder obj, Map formParams, String contentType) throws ApiException { try { if (contentType.startsWith("multipart/form-data")) { throw new ApiException("multipart/form-data not yet supported for serializeToString (http signature authentication)"); @@ -882,7 +882,7 @@ public String serializeToString(Object obj, Map formParams, Stri return formString.substring(0, formString.length() - 1); } } else { - return json.getMapper().writeValueAsString(obj); + return obj == null ? "" : json.getMapper().writeValueAsString(obj.body); } } catch (Exception ex) { throw new ApiException("Failed to perform serializeToString: " + ex.toString()); @@ -974,6 +974,19 @@ public File prepareDownloadFile(Response response) throws IOException { return File.createTempFile(prefix, suffix, new File(tempFolderPath)); } + /** + * A wrapper class for the request body. + * + * This is needed to differentiate between a request without body (e.g. HTTP GET) + * versus a request with the 'null' value in the body. + */ + public static class BodyHolder { + private Object body; + public BodyHolder(Object body) { + this.body = body; + } + } + /** * Invoke API by sending HTTP request with the given options. * @@ -998,7 +1011,7 @@ public ApiResponse invokeAPI( String path, String method, List queryParams, - Object body, + BodyHolder body, Map headerParams, Map cookieParams, Map formParams, @@ -1151,7 +1164,7 @@ private Response sendRequest(String method, Invocation.Builder invocationBuilder * @deprecated Add qualified name of the operation as a first parameter. */ @Deprecated - public ApiResponse invokeAPI(String path, String method, List queryParams, Object body, Map headerParams, Map cookieParams, Map formParams, String accept, String contentType, String[] authNames, GenericType returnType) throws ApiException { + public ApiResponse invokeAPI(String path, String method, List queryParams, BodyHolder body, Map headerParams, Map cookieParams, Map formParams, String accept, String contentType, String[] authNames, GenericType returnType) throws ApiException { return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType); } diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/AnotherFakeApi.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/AnotherFakeApi.java index af53f8e31840..34e4e216e71e 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/AnotherFakeApi.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/AnotherFakeApi.java @@ -74,7 +74,10 @@ public Client call123testSpecialTags(Client client) throws ApiException { */ public ApiResponse call123testSpecialTagsWithHttpInfo(Client client) throws ApiException { - Object localVarPostBody = client; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(client); // verify the required parameter 'client' is set if (client == null) { diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/DefaultApi.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/DefaultApi.java index 4ecffa6999c3..f87dd5676c5e 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/DefaultApi.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/DefaultApi.java @@ -72,7 +72,10 @@ public InlineResponseDefault fooGet() throws ApiException { */ public ApiResponse fooGetWithHttpInfo() throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // create path and map variables String localVarPath = "/foo"; diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeApi.java index 9ff2998820b3..4f5663d52f08 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeApi.java @@ -81,7 +81,10 @@ public HealthCheckResult fakeHealthGet() throws ApiException { */ public ApiResponse fakeHealthGetWithHttpInfo() throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // create path and map variables String localVarPath = "/fake/health"; @@ -143,7 +146,10 @@ public Boolean fakeOuterBooleanSerialize(Boolean body) throws ApiException { */ public ApiResponse fakeOuterBooleanSerializeWithHttpInfo(Boolean body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // create path and map variables String localVarPath = "/fake/outer/boolean"; @@ -205,7 +211,10 @@ public OuterComposite fakeOuterCompositeSerialize(OuterComposite outerComposite) */ public ApiResponse fakeOuterCompositeSerializeWithHttpInfo(OuterComposite outerComposite) throws ApiException { - Object localVarPostBody = outerComposite; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(outerComposite); // create path and map variables String localVarPath = "/fake/outer/composite"; @@ -267,7 +276,10 @@ public BigDecimal fakeOuterNumberSerialize(BigDecimal body) throws ApiException */ public ApiResponse fakeOuterNumberSerializeWithHttpInfo(BigDecimal body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // create path and map variables String localVarPath = "/fake/outer/number"; @@ -329,7 +341,10 @@ public String fakeOuterStringSerialize(String body) throws ApiException { */ public ApiResponse fakeOuterStringSerializeWithHttpInfo(String body) throws ApiException { - Object localVarPostBody = body; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(body); // create path and map variables String localVarPath = "/fake/outer/string"; @@ -389,7 +404,10 @@ public List getArrayOfEnums() throws ApiException { */ public ApiResponse> getArrayOfEnumsWithHttpInfo() throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // create path and map variables String localVarPath = "/fake/array-of-enums"; @@ -450,7 +468,10 @@ public void testBodyWithFileSchema(FileSchemaTestClass fileSchemaTestClass) thro */ public ApiResponse testBodyWithFileSchemaWithHttpInfo(FileSchemaTestClass fileSchemaTestClass) throws ApiException { - Object localVarPostBody = fileSchemaTestClass; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(fileSchemaTestClass); // verify the required parameter 'fileSchemaTestClass' is set if (fileSchemaTestClass == null) { @@ -516,7 +537,10 @@ public void testBodyWithQueryParams(String query, User user) throws ApiException */ public ApiResponse testBodyWithQueryParamsWithHttpInfo(String query, User user) throws ApiException { - Object localVarPostBody = user; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(user); // verify the required parameter 'query' is set if (query == null) { @@ -587,7 +611,10 @@ public Client testClientModel(Client client) throws ApiException { */ public ApiResponse testClientModelWithHttpInfo(Client client) throws ApiException { - Object localVarPostBody = client; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(client); // verify the required parameter 'client' is set if (client == null) { @@ -681,7 +708,10 @@ public void testEndpointParameters(BigDecimal number, Double _double, String pat */ public ApiResponse testEndpointParametersWithHttpInfo(BigDecimal number, Double _double, String patternWithoutDelimiter, byte[] _byte, Integer integer, Integer int32, Long int64, Float _float, String string, File binary, LocalDate date, OffsetDateTime dateTime, String password, String paramCallback) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'number' is set if (number == null) { @@ -804,7 +834,10 @@ public void testEnumParameters(List enumHeaderStringArray, String enumHe */ public ApiResponse testEnumParametersWithHttpInfo(List enumHeaderStringArray, String enumHeaderString, List enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, List enumFormStringArray, String enumFormString) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // create path and map variables String localVarPath = "/fake"; @@ -849,7 +882,10 @@ public ApiResponse testEnumParametersWithHttpInfo(List enumHeaderS } private ApiResponse testGroupParametersWithHttpInfo(Integer requiredStringGroup, Boolean requiredBooleanGroup, Long requiredInt64Group, Integer stringGroup, Boolean booleanGroup, Long int64Group) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'requiredStringGroup' is set if (requiredStringGroup == null) { @@ -1046,7 +1082,10 @@ public void testInlineAdditionalProperties(Map requestBody) thro */ public ApiResponse testInlineAdditionalPropertiesWithHttpInfo(Map requestBody) throws ApiException { - Object localVarPostBody = requestBody; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(requestBody); // verify the required parameter 'requestBody' is set if (requestBody == null) { @@ -1112,7 +1151,10 @@ public void testJsonFormData(String param, String param2) throws ApiException { */ public ApiResponse testJsonFormDataWithHttpInfo(String param, String param2) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'param' is set if (param == null) { @@ -1193,7 +1235,10 @@ public void testQueryParameterCollectionFormat(List pipe, List i */ public ApiResponse testQueryParameterCollectionFormatWithHttpInfo(List pipe, List ioutil, List http, List url, List context) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'pipe' is set if (pipe == null) { diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java index 9c00cc7d535d..481d447cd00d 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/FakeClassnameTags123Api.java @@ -74,7 +74,10 @@ public Client testClassname(Client client) throws ApiException { */ public ApiResponse testClassnameWithHttpInfo(Client client) throws ApiException { - Object localVarPostBody = client; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(client); // verify the required parameter 'client' is set if (client == null) { diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/PetApi.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/PetApi.java index aa3da5319154..ca79d0a80dd3 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/PetApi.java @@ -75,7 +75,10 @@ public void addPet(Pet pet) throws ApiException { */ public ApiResponse addPetWithHttpInfo(Pet pet) throws ApiException { - Object localVarPostBody = pet; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(pet); // verify the required parameter 'pet' is set if (pet == null) { @@ -141,7 +144,10 @@ public void deletePet(Long petId, String apiKey) throws ApiException { */ public ApiResponse deletePetWithHttpInfo(Long petId, String apiKey) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -211,7 +217,10 @@ public List findPetsByStatus(List status) throws ApiException { */ public ApiResponse> findPetsByStatusWithHttpInfo(List status) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'status' is set if (status == null) { @@ -285,7 +294,10 @@ public List findPetsByTags(List tags) throws ApiException { */ @Deprecated public ApiResponse> findPetsByTagsWithHttpInfo(List tags) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'tags' is set if (tags == null) { @@ -357,7 +369,10 @@ public Pet getPetById(Long petId) throws ApiException { */ public ApiResponse getPetByIdWithHttpInfo(Long petId) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -428,7 +443,10 @@ public void updatePet(Pet pet) throws ApiException { */ public ApiResponse updatePetWithHttpInfo(Pet pet) throws ApiException { - Object localVarPostBody = pet; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(pet); // verify the required parameter 'pet' is set if (pet == null) { @@ -496,7 +514,10 @@ public void updatePetWithForm(Long petId, String name, String status) throws Api */ public ApiResponse updatePetWithFormWithHttpInfo(Long petId, String name, String status) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -570,7 +591,10 @@ public ModelApiResponse uploadFile(Long petId, String additionalMetadata, File f */ public ApiResponse uploadFileWithHttpInfo(Long petId, String additionalMetadata, File file) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -646,7 +670,10 @@ public ModelApiResponse uploadFileWithRequiredFile(Long petId, File requiredFile */ public ApiResponse uploadFileWithRequiredFileWithHttpInfo(Long petId, File requiredFile, String additionalMetadata) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'petId' is set if (petId == null) { diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/StoreApi.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/StoreApi.java index 8057722e2990..da9cd6f50c9d 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/StoreApi.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/StoreApi.java @@ -75,7 +75,10 @@ public void deleteOrder(String orderId) throws ApiException { */ public ApiResponse deleteOrderWithHttpInfo(String orderId) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'orderId' is set if (orderId == null) { @@ -139,7 +142,10 @@ public Map getInventory() throws ApiException { */ public ApiResponse> getInventoryWithHttpInfo() throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // create path and map variables String localVarPath = "/store/inventory"; @@ -205,7 +211,10 @@ public Order getOrderById(Long orderId) throws ApiException { */ public ApiResponse getOrderByIdWithHttpInfo(Long orderId) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'orderId' is set if (orderId == null) { @@ -275,7 +284,10 @@ public Order placeOrder(Order order) throws ApiException { */ public ApiResponse placeOrderWithHttpInfo(Order order) throws ApiException { - Object localVarPostBody = order; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(order); // verify the required parameter 'order' is set if (order == null) { diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/UserApi.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/UserApi.java index 9d4759c2f896..b4b85a0b70fd 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/api/UserApi.java @@ -73,7 +73,10 @@ public void createUser(User user) throws ApiException { */ public ApiResponse createUserWithHttpInfo(User user) throws ApiException { - Object localVarPostBody = user; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(user); // verify the required parameter 'user' is set if (user == null) { @@ -137,7 +140,10 @@ public void createUsersWithArrayInput(List user) throws ApiException { */ public ApiResponse createUsersWithArrayInputWithHttpInfo(List user) throws ApiException { - Object localVarPostBody = user; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(user); // verify the required parameter 'user' is set if (user == null) { @@ -201,7 +207,10 @@ public void createUsersWithListInput(List user) throws ApiException { */ public ApiResponse createUsersWithListInputWithHttpInfo(List user) throws ApiException { - Object localVarPostBody = user; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(user); // verify the required parameter 'user' is set if (user == null) { @@ -267,7 +276,10 @@ public void deleteUser(String username) throws ApiException { */ public ApiResponse deleteUserWithHttpInfo(String username) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'username' is set if (username == null) { @@ -337,7 +349,10 @@ public User getUserByName(String username) throws ApiException { */ public ApiResponse getUserByNameWithHttpInfo(String username) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'username' is set if (username == null) { @@ -409,7 +424,10 @@ public String loginUser(String username, String password) throws ApiException { */ public ApiResponse loginUserWithHttpInfo(String username, String password) throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // verify the required parameter 'username' is set if (username == null) { @@ -480,7 +498,10 @@ public void logoutUser() throws ApiException { */ public ApiResponse logoutUserWithHttpInfo() throws ApiException { - Object localVarPostBody = null; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = null; // create path and map variables String localVarPath = "/user/logout"; @@ -543,7 +564,10 @@ public void updateUser(String username, User user) throws ApiException { */ public ApiResponse updateUserWithHttpInfo(String username, User user) throws ApiException { - Object localVarPostBody = user; + // BodyHolder is set to null when the HTTP request body is not set (e.g. for HTTP GET requests). + // When the body is set (e.g HTTP POST request), the object maybe have a null value which needs + // to be serialized as the 'null' value. + ApiClient.BodyHolder localVarPostBody = new ApiClient.BodyHolder(user); // verify the required parameter 'username' is set if (username == null) { From da44088255f5876d46c650753b8d6e3008a81c34 Mon Sep 17 00:00:00 2001 From: "Sebastien Rosset (serosset)" Date: Thu, 11 Jun 2020 18:16:40 -0700 Subject: [PATCH 2/2] Differentiate request with no body vs request that contains the null value --- .../Java/libraries/jersey2/ApiClient.mustache | 1 + .../org/openapitools/client/ApiClient.java | 61 +------------------ .../org/openapitools/client/ApiClient.java | 1 + 3 files changed, 4 insertions(+), 59 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache index 08d2ce28b902..11312d8e2417 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache @@ -886,6 +886,7 @@ public class ApiClient { return formString.substring(0, formString.length() - 1); } } else { + // The BodyHolder obj is null if the request has an empty body. return obj == null ? "" : json.getMapper().writeValueAsString(obj.body); } } catch (Exception ex) { diff --git a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java index 2cab1b120459..b1e7715b81f4 100644 --- a/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java @@ -25,13 +25,6 @@ import java.io.InputStream; import java.net.URI; -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; -import java.security.cert.X509Certificate; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; import java.nio.file.Files; import java.nio.file.StandardCopyOption; import org.glassfish.jersey.logging.LoggingFeature; @@ -810,6 +803,7 @@ public String serializeToString(BodyHolder obj, Map formParams, return formString.substring(0, formString.length() - 1); } } else { + // The BodyHolder obj is null if the request has an empty body. return obj == null ? "" : json.getMapper().writeValueAsString(obj.body); } } catch (Exception ex) { @@ -1119,64 +1113,13 @@ protected Client buildHttpClient(boolean debugging) { java.util.logging.Logger.getLogger("org.glassfish.jersey.client").setLevel(java.util.logging.Level.SEVERE); } performAdditionalClientConfiguration(clientConfig); - ClientBuilder clientBuilder = ClientBuilder.newBuilder(); - customizeClientBuilder(clientBuilder); - clientBuilder = clientBuilder.withConfig(clientConfig); - return clientBuilder.build(); + return ClientBuilder.newClient(clientConfig); } - /** - * Perform additional configuration of the API client. - * This method can be overriden to customize the API client. - */ protected void performAdditionalClientConfiguration(ClientConfig clientConfig) { // No-op extension point } - /** - * Customize the client builder. - * - * This method can be overriden to customize the API client. For example, this can be used to: - * 1. Set the hostname verifier to be used by the client to verify the endpoint's hostname - * against its identification information. - * 2. Set the client-side key store. - * 3. Set the SSL context that will be used when creating secured transport connections to - * server endpoints from web targets created by the client instance that is using this SSL context. - * 4. Set the client-side trust store. - * - * To completely disable certificate validation (at your own risk), you can - * override this method and invoke disableCertificateValidation(clientBuilder). - */ - protected void customizeClientBuilder(ClientBuilder clientBuilder) { - // No-op extension point - } - - /** - * Disable X.509 certificate validation in TLS connections. - * - * Please note that trusting all certificates is extremely risky. - * This may be useful in a development environment with self-signed certificates. - */ - protected void disableCertificateValidation(ClientBuilder clientBuilder) throws KeyManagementException, NoSuchAlgorithmException { - TrustManager[] trustAllCerts = new X509TrustManager[] { - new X509TrustManager() { - @Override - public X509Certificate[] getAcceptedIssuers() { - return null; - } - @Override - public void checkClientTrusted(X509Certificate[] certs, String authType) { - } - @Override - public void checkServerTrusted(X509Certificate[] certs, String authType) { - } - } - }; - SSLContext sslContext = SSLContext.getInstance("TLS"); - sslContext.init(null, trustAllCerts, new SecureRandom()); - clientBuilder.sslContext(sslContext); - } - protected Map> buildResponseHeaders(Response response) { Map> responseHeaders = new HashMap>(); for (Entry> entry: response.getHeaders().entrySet()) { diff --git a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java index f6c2bdd5ef3e..3a6f7af4f717 100644 --- a/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java +++ b/samples/openapi3/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java @@ -882,6 +882,7 @@ public String serializeToString(BodyHolder obj, Map formParams, return formString.substring(0, formString.length() - 1); } } else { + // The BodyHolder obj is null if the request has an empty body. return obj == null ? "" : json.getMapper().writeValueAsString(obj.body); } } catch (Exception ex) {