From 0da55081c934704f24c702659e93e640fadf006c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Mart=C3=ADnez?= Date: Fri, 20 Jul 2018 17:49:12 +0200 Subject: [PATCH] [Java][Client][RestTemplate] Do not create new Object for empty body (#605) #513 Fixed error that causes exception when trying to perform HTTP requests without a body and an empty Object is used as body instead. In these cases an exception is thrown indicating that it is not possible to find a message converter for java.lang.Object and application/json. --- .../Java/libraries/resttemplate/api.mustache | 2 +- .../java/org/openapitools/client/api/FakeApi.java | 6 +++--- .../java/org/openapitools/client/api/PetApi.java | 14 +++++++------- .../java/org/openapitools/client/api/StoreApi.java | 6 +++--- .../java/org/openapitools/client/api/UserApi.java | 8 ++++---- .../java/org/openapitools/client/api/FakeApi.java | 6 +++--- .../java/org/openapitools/client/api/PetApi.java | 14 +++++++------- .../java/org/openapitools/client/api/StoreApi.java | 6 +++--- .../java/org/openapitools/client/api/UserApi.java | 8 ++++---- 9 files changed, 35 insertions(+), 35 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache index a906a80e7a8..f48180f4381 100644 --- a/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache +++ b/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache @@ -61,7 +61,7 @@ public class {{classname}} { {{/externalDocs}} */ public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws RestClientException { - Object {{localVariablePrefix}}postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}new Object(){{/bodyParam}}; + Object {{localVariablePrefix}}postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}}; {{#allParams}}{{#required}} // verify the required parameter '{{paramName}}' is set if ({{paramName}} == null) { diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java index c7adab4c3af..1a765af7da7 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java @@ -300,7 +300,7 @@ public class FakeApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void testEndpointParameters(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 RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'number' is set if (number == null) { @@ -385,7 +385,7 @@ public class FakeApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void testEnumParameters(List enumHeaderStringArray, String enumHeaderString, List enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, List enumFormStringArray, String enumFormString) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; String path = UriComponentsBuilder.fromPath("/fake").build().toUriString(); @@ -462,7 +462,7 @@ public class FakeApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void testJsonFormData(String param, String param2) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'param' is set if (param == null) { diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java index 8a9eeaa82c2..7a3897f689c 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java @@ -89,7 +89,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void deletePet(Long petId, String apiKey) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -128,7 +128,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public List findPetsByStatus(List status) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'status' is set if (status == null) { @@ -165,7 +165,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public List findPetsByTags(List tags) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'tags' is set if (tags == null) { @@ -203,7 +203,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public Pet getPetById(Long petId) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -276,7 +276,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void updatePetWithForm(Long petId, String name, String status) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -320,7 +320,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public ModelApiResponse uploadFile(Long petId, String additionalMetadata, File file) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -366,7 +366,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public ModelApiResponse uploadFileWithRequiredFile(Long petId, File requiredFile, String additionalMetadata) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'petId' is set if (petId == null) { diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/StoreApi.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/StoreApi.java index 2a5743dc74a..e417838bba4 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/StoreApi.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/StoreApi.java @@ -54,7 +54,7 @@ public class StoreApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void deleteOrder(String orderId) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'orderId' is set if (orderId == null) { @@ -88,7 +88,7 @@ public class StoreApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public Map getInventory() throws RestClientException { - Object postBody = new Object(); + Object postBody = null; String path = UriComponentsBuilder.fromPath("/store/inventory").build().toUriString(); @@ -119,7 +119,7 @@ public class StoreApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public Order getOrderById(Long orderId) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'orderId' is set if (orderId == null) { diff --git a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java index bd89c01b8aa..2949e2f57a0 100644 --- a/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java @@ -147,7 +147,7 @@ public class UserApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void deleteUser(String username) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'username' is set if (username == null) { @@ -184,7 +184,7 @@ public class UserApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public User getUserByName(String username) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'username' is set if (username == null) { @@ -223,7 +223,7 @@ public class UserApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public String loginUser(String username, String password) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'username' is set if (username == null) { @@ -263,7 +263,7 @@ public class UserApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void logoutUser() throws RestClientException { - Object postBody = new Object(); + Object postBody = null; String path = UriComponentsBuilder.fromPath("/user/logout").build().toUriString(); diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeApi.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeApi.java index c7adab4c3af..1a765af7da7 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeApi.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeApi.java @@ -300,7 +300,7 @@ public class FakeApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void testEndpointParameters(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 RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'number' is set if (number == null) { @@ -385,7 +385,7 @@ public class FakeApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void testEnumParameters(List enumHeaderStringArray, String enumHeaderString, List enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, List enumFormStringArray, String enumFormString) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; String path = UriComponentsBuilder.fromPath("/fake").build().toUriString(); @@ -462,7 +462,7 @@ public class FakeApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void testJsonFormData(String param, String param2) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'param' is set if (param == null) { diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/PetApi.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/PetApi.java index 8a9eeaa82c2..7a3897f689c 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/PetApi.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/PetApi.java @@ -89,7 +89,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void deletePet(Long petId, String apiKey) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -128,7 +128,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public List findPetsByStatus(List status) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'status' is set if (status == null) { @@ -165,7 +165,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public List findPetsByTags(List tags) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'tags' is set if (tags == null) { @@ -203,7 +203,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public Pet getPetById(Long petId) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -276,7 +276,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void updatePetWithForm(Long petId, String name, String status) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -320,7 +320,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public ModelApiResponse uploadFile(Long petId, String additionalMetadata, File file) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'petId' is set if (petId == null) { @@ -366,7 +366,7 @@ public class PetApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public ModelApiResponse uploadFileWithRequiredFile(Long petId, File requiredFile, String additionalMetadata) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'petId' is set if (petId == null) { diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/StoreApi.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/StoreApi.java index 2a5743dc74a..e417838bba4 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/StoreApi.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/StoreApi.java @@ -54,7 +54,7 @@ public class StoreApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void deleteOrder(String orderId) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'orderId' is set if (orderId == null) { @@ -88,7 +88,7 @@ public class StoreApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public Map getInventory() throws RestClientException { - Object postBody = new Object(); + Object postBody = null; String path = UriComponentsBuilder.fromPath("/store/inventory").build().toUriString(); @@ -119,7 +119,7 @@ public class StoreApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public Order getOrderById(Long orderId) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'orderId' is set if (orderId == null) { diff --git a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/UserApi.java b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/UserApi.java index bd89c01b8aa..2949e2f57a0 100644 --- a/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/UserApi.java +++ b/samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/UserApi.java @@ -147,7 +147,7 @@ public class UserApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void deleteUser(String username) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'username' is set if (username == null) { @@ -184,7 +184,7 @@ public class UserApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public User getUserByName(String username) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'username' is set if (username == null) { @@ -223,7 +223,7 @@ public class UserApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public String loginUser(String username, String password) throws RestClientException { - Object postBody = new Object(); + Object postBody = null; // verify the required parameter 'username' is set if (username == null) { @@ -263,7 +263,7 @@ public class UserApi { * @throws RestClientException if an error occurs while attempting to invoke the API */ public void logoutUser() throws RestClientException { - Object postBody = new Object(); + Object postBody = null; String path = UriComponentsBuilder.fromPath("/user/logout").build().toUriString();