Skip to content

Commit 0da5508

Browse files
rubmsjmini
authored andcommitted
[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.
1 parent af9d57e commit 0da5508

9 files changed

Lines changed: 35 additions & 35 deletions

File tree

modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/api.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class {{classname}} {
6161
{{/externalDocs}}
6262
*/
6363
public {{#returnType}}{{{returnType}}} {{/returnType}}{{^returnType}}void {{/returnType}}{{operationId}}({{#allParams}}{{{dataType}}} {{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) throws RestClientException {
64-
Object {{localVariablePrefix}}postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}new Object(){{/bodyParam}};
64+
Object {{localVariablePrefix}}postBody = {{#bodyParam}}{{paramName}}{{/bodyParam}}{{^bodyParam}}null{{/bodyParam}};
6565
{{#allParams}}{{#required}}
6666
// verify the required parameter '{{paramName}}' is set
6767
if ({{paramName}} == null) {

samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/FakeApi.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public Client testClientModel(Client client) throws RestClientException {
300300
* @throws RestClientException if an error occurs while attempting to invoke the API
301301
*/
302302
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 {
303-
Object postBody = new Object();
303+
Object postBody = null;
304304

305305
// verify the required parameter 'number' is set
306306
if (number == null) {
@@ -385,7 +385,7 @@ public void testEndpointParameters(BigDecimal number, Double _double, String pat
385385
* @throws RestClientException if an error occurs while attempting to invoke the API
386386
*/
387387
public void testEnumParameters(List<String> enumHeaderStringArray, String enumHeaderString, List<String> enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, List<String> enumFormStringArray, String enumFormString) throws RestClientException {
388-
Object postBody = new Object();
388+
Object postBody = null;
389389

390390
String path = UriComponentsBuilder.fromPath("/fake").build().toUriString();
391391

@@ -462,7 +462,7 @@ public void testInlineAdditionalProperties(Map<String, String> requestBody) thro
462462
* @throws RestClientException if an error occurs while attempting to invoke the API
463463
*/
464464
public void testJsonFormData(String param, String param2) throws RestClientException {
465-
Object postBody = new Object();
465+
Object postBody = null;
466466

467467
// verify the required parameter 'param' is set
468468
if (param == null) {

samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/PetApi.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void addPet(Pet pet) throws RestClientException {
8989
* @throws RestClientException if an error occurs while attempting to invoke the API
9090
*/
9191
public void deletePet(Long petId, String apiKey) throws RestClientException {
92-
Object postBody = new Object();
92+
Object postBody = null;
9393

9494
// verify the required parameter 'petId' is set
9595
if (petId == null) {
@@ -128,7 +128,7 @@ public void deletePet(Long petId, String apiKey) throws RestClientException {
128128
* @throws RestClientException if an error occurs while attempting to invoke the API
129129
*/
130130
public List<Pet> findPetsByStatus(List<String> status) throws RestClientException {
131-
Object postBody = new Object();
131+
Object postBody = null;
132132

133133
// verify the required parameter 'status' is set
134134
if (status == null) {
@@ -165,7 +165,7 @@ public List<Pet> findPetsByStatus(List<String> status) throws RestClientExceptio
165165
* @throws RestClientException if an error occurs while attempting to invoke the API
166166
*/
167167
public List<Pet> findPetsByTags(List<String> tags) throws RestClientException {
168-
Object postBody = new Object();
168+
Object postBody = null;
169169

170170
// verify the required parameter 'tags' is set
171171
if (tags == null) {
@@ -203,7 +203,7 @@ public List<Pet> findPetsByTags(List<String> tags) throws RestClientException {
203203
* @throws RestClientException if an error occurs while attempting to invoke the API
204204
*/
205205
public Pet getPetById(Long petId) throws RestClientException {
206-
Object postBody = new Object();
206+
Object postBody = null;
207207

208208
// verify the required parameter 'petId' is set
209209
if (petId == null) {
@@ -276,7 +276,7 @@ public void updatePet(Pet pet) throws RestClientException {
276276
* @throws RestClientException if an error occurs while attempting to invoke the API
277277
*/
278278
public void updatePetWithForm(Long petId, String name, String status) throws RestClientException {
279-
Object postBody = new Object();
279+
Object postBody = null;
280280

281281
// verify the required parameter 'petId' is set
282282
if (petId == null) {
@@ -320,7 +320,7 @@ public void updatePetWithForm(Long petId, String name, String status) throws Res
320320
* @throws RestClientException if an error occurs while attempting to invoke the API
321321
*/
322322
public ModelApiResponse uploadFile(Long petId, String additionalMetadata, File file) throws RestClientException {
323-
Object postBody = new Object();
323+
Object postBody = null;
324324

325325
// verify the required parameter 'petId' is set
326326
if (petId == null) {
@@ -366,7 +366,7 @@ public ModelApiResponse uploadFile(Long petId, String additionalMetadata, File f
366366
* @throws RestClientException if an error occurs while attempting to invoke the API
367367
*/
368368
public ModelApiResponse uploadFileWithRequiredFile(Long petId, File requiredFile, String additionalMetadata) throws RestClientException {
369-
Object postBody = new Object();
369+
Object postBody = null;
370370

371371
// verify the required parameter 'petId' is set
372372
if (petId == null) {

samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/StoreApi.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void setApiClient(ApiClient apiClient) {
5454
* @throws RestClientException if an error occurs while attempting to invoke the API
5555
*/
5656
public void deleteOrder(String orderId) throws RestClientException {
57-
Object postBody = new Object();
57+
Object postBody = null;
5858

5959
// verify the required parameter 'orderId' is set
6060
if (orderId == null) {
@@ -88,7 +88,7 @@ public void deleteOrder(String orderId) throws RestClientException {
8888
* @throws RestClientException if an error occurs while attempting to invoke the API
8989
*/
9090
public Map<String, Integer> getInventory() throws RestClientException {
91-
Object postBody = new Object();
91+
Object postBody = null;
9292

9393
String path = UriComponentsBuilder.fromPath("/store/inventory").build().toUriString();
9494

@@ -119,7 +119,7 @@ public Map<String, Integer> getInventory() throws RestClientException {
119119
* @throws RestClientException if an error occurs while attempting to invoke the API
120120
*/
121121
public Order getOrderById(Long orderId) throws RestClientException {
122-
Object postBody = new Object();
122+
Object postBody = null;
123123

124124
// verify the required parameter 'orderId' is set
125125
if (orderId == null) {

samples/client/petstore/java/resttemplate-withXml/src/main/java/org/openapitools/client/api/UserApi.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void createUsersWithListInput(List<User> user) throws RestClientException
147147
* @throws RestClientException if an error occurs while attempting to invoke the API
148148
*/
149149
public void deleteUser(String username) throws RestClientException {
150-
Object postBody = new Object();
150+
Object postBody = null;
151151

152152
// verify the required parameter 'username' is set
153153
if (username == null) {
@@ -184,7 +184,7 @@ public void deleteUser(String username) throws RestClientException {
184184
* @throws RestClientException if an error occurs while attempting to invoke the API
185185
*/
186186
public User getUserByName(String username) throws RestClientException {
187-
Object postBody = new Object();
187+
Object postBody = null;
188188

189189
// verify the required parameter 'username' is set
190190
if (username == null) {
@@ -223,7 +223,7 @@ public User getUserByName(String username) throws RestClientException {
223223
* @throws RestClientException if an error occurs while attempting to invoke the API
224224
*/
225225
public String loginUser(String username, String password) throws RestClientException {
226-
Object postBody = new Object();
226+
Object postBody = null;
227227

228228
// verify the required parameter 'username' is set
229229
if (username == null) {
@@ -263,7 +263,7 @@ public String loginUser(String username, String password) throws RestClientExcep
263263
* @throws RestClientException if an error occurs while attempting to invoke the API
264264
*/
265265
public void logoutUser() throws RestClientException {
266-
Object postBody = new Object();
266+
Object postBody = null;
267267

268268
String path = UriComponentsBuilder.fromPath("/user/logout").build().toUriString();
269269

samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/FakeApi.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public Client testClientModel(Client client) throws RestClientException {
300300
* @throws RestClientException if an error occurs while attempting to invoke the API
301301
*/
302302
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 {
303-
Object postBody = new Object();
303+
Object postBody = null;
304304

305305
// verify the required parameter 'number' is set
306306
if (number == null) {
@@ -385,7 +385,7 @@ public void testEndpointParameters(BigDecimal number, Double _double, String pat
385385
* @throws RestClientException if an error occurs while attempting to invoke the API
386386
*/
387387
public void testEnumParameters(List<String> enumHeaderStringArray, String enumHeaderString, List<String> enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, List<String> enumFormStringArray, String enumFormString) throws RestClientException {
388-
Object postBody = new Object();
388+
Object postBody = null;
389389

390390
String path = UriComponentsBuilder.fromPath("/fake").build().toUriString();
391391

@@ -462,7 +462,7 @@ public void testInlineAdditionalProperties(Map<String, String> requestBody) thro
462462
* @throws RestClientException if an error occurs while attempting to invoke the API
463463
*/
464464
public void testJsonFormData(String param, String param2) throws RestClientException {
465-
Object postBody = new Object();
465+
Object postBody = null;
466466

467467
// verify the required parameter 'param' is set
468468
if (param == null) {

samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/PetApi.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public void addPet(Pet pet) throws RestClientException {
8989
* @throws RestClientException if an error occurs while attempting to invoke the API
9090
*/
9191
public void deletePet(Long petId, String apiKey) throws RestClientException {
92-
Object postBody = new Object();
92+
Object postBody = null;
9393

9494
// verify the required parameter 'petId' is set
9595
if (petId == null) {
@@ -128,7 +128,7 @@ public void deletePet(Long petId, String apiKey) throws RestClientException {
128128
* @throws RestClientException if an error occurs while attempting to invoke the API
129129
*/
130130
public List<Pet> findPetsByStatus(List<String> status) throws RestClientException {
131-
Object postBody = new Object();
131+
Object postBody = null;
132132

133133
// verify the required parameter 'status' is set
134134
if (status == null) {
@@ -165,7 +165,7 @@ public List<Pet> findPetsByStatus(List<String> status) throws RestClientExceptio
165165
* @throws RestClientException if an error occurs while attempting to invoke the API
166166
*/
167167
public List<Pet> findPetsByTags(List<String> tags) throws RestClientException {
168-
Object postBody = new Object();
168+
Object postBody = null;
169169

170170
// verify the required parameter 'tags' is set
171171
if (tags == null) {
@@ -203,7 +203,7 @@ public List<Pet> findPetsByTags(List<String> tags) throws RestClientException {
203203
* @throws RestClientException if an error occurs while attempting to invoke the API
204204
*/
205205
public Pet getPetById(Long petId) throws RestClientException {
206-
Object postBody = new Object();
206+
Object postBody = null;
207207

208208
// verify the required parameter 'petId' is set
209209
if (petId == null) {
@@ -276,7 +276,7 @@ public void updatePet(Pet pet) throws RestClientException {
276276
* @throws RestClientException if an error occurs while attempting to invoke the API
277277
*/
278278
public void updatePetWithForm(Long petId, String name, String status) throws RestClientException {
279-
Object postBody = new Object();
279+
Object postBody = null;
280280

281281
// verify the required parameter 'petId' is set
282282
if (petId == null) {
@@ -320,7 +320,7 @@ public void updatePetWithForm(Long petId, String name, String status) throws Res
320320
* @throws RestClientException if an error occurs while attempting to invoke the API
321321
*/
322322
public ModelApiResponse uploadFile(Long petId, String additionalMetadata, File file) throws RestClientException {
323-
Object postBody = new Object();
323+
Object postBody = null;
324324

325325
// verify the required parameter 'petId' is set
326326
if (petId == null) {
@@ -366,7 +366,7 @@ public ModelApiResponse uploadFile(Long petId, String additionalMetadata, File f
366366
* @throws RestClientException if an error occurs while attempting to invoke the API
367367
*/
368368
public ModelApiResponse uploadFileWithRequiredFile(Long petId, File requiredFile, String additionalMetadata) throws RestClientException {
369-
Object postBody = new Object();
369+
Object postBody = null;
370370

371371
// verify the required parameter 'petId' is set
372372
if (petId == null) {

samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/StoreApi.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void setApiClient(ApiClient apiClient) {
5454
* @throws RestClientException if an error occurs while attempting to invoke the API
5555
*/
5656
public void deleteOrder(String orderId) throws RestClientException {
57-
Object postBody = new Object();
57+
Object postBody = null;
5858

5959
// verify the required parameter 'orderId' is set
6060
if (orderId == null) {
@@ -88,7 +88,7 @@ public void deleteOrder(String orderId) throws RestClientException {
8888
* @throws RestClientException if an error occurs while attempting to invoke the API
8989
*/
9090
public Map<String, Integer> getInventory() throws RestClientException {
91-
Object postBody = new Object();
91+
Object postBody = null;
9292

9393
String path = UriComponentsBuilder.fromPath("/store/inventory").build().toUriString();
9494

@@ -119,7 +119,7 @@ public Map<String, Integer> getInventory() throws RestClientException {
119119
* @throws RestClientException if an error occurs while attempting to invoke the API
120120
*/
121121
public Order getOrderById(Long orderId) throws RestClientException {
122-
Object postBody = new Object();
122+
Object postBody = null;
123123

124124
// verify the required parameter 'orderId' is set
125125
if (orderId == null) {

samples/client/petstore/java/resttemplate/src/main/java/org/openapitools/client/api/UserApi.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public void createUsersWithListInput(List<User> user) throws RestClientException
147147
* @throws RestClientException if an error occurs while attempting to invoke the API
148148
*/
149149
public void deleteUser(String username) throws RestClientException {
150-
Object postBody = new Object();
150+
Object postBody = null;
151151

152152
// verify the required parameter 'username' is set
153153
if (username == null) {
@@ -184,7 +184,7 @@ public void deleteUser(String username) throws RestClientException {
184184
* @throws RestClientException if an error occurs while attempting to invoke the API
185185
*/
186186
public User getUserByName(String username) throws RestClientException {
187-
Object postBody = new Object();
187+
Object postBody = null;
188188

189189
// verify the required parameter 'username' is set
190190
if (username == null) {
@@ -223,7 +223,7 @@ public User getUserByName(String username) throws RestClientException {
223223
* @throws RestClientException if an error occurs while attempting to invoke the API
224224
*/
225225
public String loginUser(String username, String password) throws RestClientException {
226-
Object postBody = new Object();
226+
Object postBody = null;
227227

228228
// verify the required parameter 'username' is set
229229
if (username == null) {
@@ -263,7 +263,7 @@ public String loginUser(String username, String password) throws RestClientExcep
263263
* @throws RestClientException if an error occurs while attempting to invoke the API
264264
*/
265265
public void logoutUser() throws RestClientException {
266-
Object postBody = new Object();
266+
Object postBody = null;
267267

268268
String path = UriComponentsBuilder.fromPath("/user/logout").build().toUriString();
269269

0 commit comments

Comments
 (0)