From 7be916d046fdf5bd60168e089a4de5f423b568d2 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Fri, 26 Jun 2020 00:40:15 +0800 Subject: [PATCH 1/6] add nullable body support --- bin/configs/java-jersey2-8.yaml | 1 + .../Java/libraries/jersey2/ApiClient.mustache | 18 +++++++---- .../Java/libraries/jersey2/api.mustache | 2 +- .../org/openapitools/client/ApiClient.java | 18 +++++++---- .../client/api/AnotherFakeApi.java | 2 +- .../org/openapitools/client/api/FakeApi.java | 28 ++++++++--------- .../client/api/FakeClassnameTags123Api.java | 2 +- .../org/openapitools/client/api/PetApi.java | 18 +++++------ .../org/openapitools/client/api/StoreApi.java | 8 ++--- .../org/openapitools/client/api/UserApi.java | 16 +++++----- .../org/openapitools/client/ApiClient.java | 18 +++++++---- .../client/api/AnotherFakeApi.java | 2 +- .../openapitools/client/api/DefaultApi.java | 2 +- .../org/openapitools/client/api/FakeApi.java | 30 +++++++++---------- .../client/api/FakeClassnameTags123Api.java | 2 +- .../org/openapitools/client/api/PetApi.java | 18 +++++------ .../org/openapitools/client/api/StoreApi.java | 8 ++--- .../org/openapitools/client/api/UserApi.java | 16 +++++----- 18 files changed, 114 insertions(+), 95 deletions(-) diff --git a/bin/configs/java-jersey2-8.yaml b/bin/configs/java-jersey2-8.yaml index c10332368de3..f968aac62307 100644 --- a/bin/configs/java-jersey2-8.yaml +++ b/bin/configs/java-jersey2-8.yaml @@ -2,6 +2,7 @@ generatorName: java outputDir: samples/openapi3/client/petstore/java/jersey2-java8 library: jersey2 inputSpec: modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-with-http-signature.yaml +templateDir: modules/openapi-generator/src/main/resources/Java additionalProperties: artifactId: petstore-openapi3-jersey2-java8 hideGenerationTimestamp: true 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 d0773177c76b..6af3c69c45a2 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 @@ -839,7 +839,7 @@ public class ApiClient { * @return Entity * @throws ApiException API exception */ - public Entity serialize(Object obj, Map formParams, String contentType) throws ApiException { + public Entity serialize(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { Entity entity; if (contentType.startsWith("multipart/form-data")) { MultiPart multiPart = new MultiPart(); @@ -863,7 +863,11 @@ 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); + if (isBodyNullable) { // payload is nullable + entity = Entity.entity(obj == null ? Entity.text("null") : obj, contentType); + } else { + entity = Entity.entity(obj == null ? Entity.text("") : obj, contentType); + } } return entity; } @@ -1007,6 +1011,7 @@ public class ApiClient { * @param contentType The request's Content-Type header * @param authNames The authentications to apply * @param returnType The return type into which to deserialize the response + * @param isBodyNullable True if the body is nullable * @return The response body in type of string * @throws ApiException API exception */ @@ -1022,7 +1027,8 @@ public class ApiClient { String accept, String contentType, String[] authNames, - GenericType returnType) + GenericType returnType, + boolean isBodyNullable) throws ApiException { // Not using `.target(targetURL).path(path)` below, @@ -1069,7 +1075,7 @@ public class ApiClient { } } - Entity entity = serialize(body, formParams, contentType); + Entity entity = serialize(body, formParams, contentType, isBodyNullable); // put all headers in one place Map allHeaderParams = new HashMap<>(defaultHeaderMap); @@ -1170,8 +1176,8 @@ 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 { - return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType); + 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, boolean isBodyNullable) throws ApiException { + return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType, isBodyNullable); } /** 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..5fcfb9b19bb3 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 @@ -167,7 +167,7 @@ public class {{classname}} { {{/returnType}} return apiClient.invokeAPI("{{classname}}.{{operationId}}", localVarPath, "{{httpMethod}}", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, {{#returnType}}localVarReturnType{{/returnType}}{{^returnType}}null{{/returnType}}); + localVarAuthNames, {{#returnType}}localVarReturnType{{/returnType}}{{^returnType}}null{{/returnType}}, {{#bodyParam}}{{#isNullable}}true{{/isNullable}}{{^isNullable}}false{{/isNullable}}{{/bodyParam}}{{^bodyParam}}false{{/bodyParam}}); } {{#vendorExtensions.x-group-parameters}} 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 a556ac6536bf..05acce295f6d 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 @@ -756,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(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { Entity entity; if (contentType.startsWith("multipart/form-data")) { MultiPart multiPart = new MultiPart(); @@ -780,7 +780,11 @@ 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); + if (isBodyNullable) { // payload is nullable + entity = Entity.entity(obj == null ? Entity.text("null") : obj, contentType); + } else { + entity = Entity.entity(obj == null ? Entity.text("") : obj, contentType); + } } return entity; } @@ -918,6 +922,7 @@ public File prepareDownloadFile(Response response) throws IOException { * @param contentType The request's Content-Type header * @param authNames The authentications to apply * @param returnType The return type into which to deserialize the response + * @param isBodyNullable True if the body is nullable * @return The response body in type of string * @throws ApiException API exception */ @@ -933,7 +938,8 @@ public ApiResponse invokeAPI( String accept, String contentType, String[] authNames, - GenericType returnType) + GenericType returnType, + boolean isBodyNullable) throws ApiException { // Not using `.target(targetURL).path(path)` below, @@ -980,7 +986,7 @@ public ApiResponse invokeAPI( } } - Entity entity = serialize(body, formParams, contentType); + Entity entity = serialize(body, formParams, contentType, isBodyNullable); // put all headers in one place Map allHeaderParams = new HashMap<>(defaultHeaderMap); @@ -1079,8 +1085,8 @@ 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 { - return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType); + 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, boolean isBodyNullable) throws ApiException { + return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType, isBodyNullable); } /** 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 71f267cfb539..00a2e3fcd4b4 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 @@ -110,6 +110,6 @@ public ApiResponse call123testSpecialTagsWithHttpInfo(Client body) throw return apiClient.invokeAPI("AnotherFakeApi.call123testSpecialTags", localVarPath, "PATCH", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } } 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 d722d3f1a0fd..170ca91a961f 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 @@ -115,7 +115,7 @@ public ApiResponse createXmlItemWithHttpInfo(XmlItem xmlItem) throws ApiEx return apiClient.invokeAPI("FakeApi.createXmlItem", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * @@ -177,7 +177,7 @@ public ApiResponse fakeOuterBooleanSerializeWithHttpInfo(Boolean body) return apiClient.invokeAPI("FakeApi.fakeOuterBooleanSerialize", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * @@ -239,7 +239,7 @@ public ApiResponse fakeOuterCompositeSerializeWithHttpInfo(Outer return apiClient.invokeAPI("FakeApi.fakeOuterCompositeSerialize", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * @@ -301,7 +301,7 @@ public ApiResponse fakeOuterNumberSerializeWithHttpInfo(BigDecimal b return apiClient.invokeAPI("FakeApi.fakeOuterNumberSerialize", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * @@ -363,7 +363,7 @@ public ApiResponse fakeOuterStringSerializeWithHttpInfo(String body) thr return apiClient.invokeAPI("FakeApi.fakeOuterStringSerialize", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * @@ -427,7 +427,7 @@ public ApiResponse testBodyWithFileSchemaWithHttpInfo(FileSchemaTestClass return apiClient.invokeAPI("FakeApi.testBodyWithFileSchema", localVarPath, "PUT", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * @@ -499,7 +499,7 @@ public ApiResponse testBodyWithQueryParamsWithHttpInfo(String query, User return apiClient.invokeAPI("FakeApi.testBodyWithQueryParams", localVarPath, "PUT", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * To test \"client\" model @@ -566,7 +566,7 @@ public ApiResponse testClientModelWithHttpInfo(Client body) throws ApiEx return apiClient.invokeAPI("FakeApi.testClientModel", localVarPath, "PATCH", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트 @@ -701,7 +701,7 @@ public ApiResponse testEndpointParametersWithHttpInfo(BigDecimal number, D return apiClient.invokeAPI("FakeApi.testEndpointParameters", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * To test enum parameters @@ -788,7 +788,7 @@ public ApiResponse testEnumParametersWithHttpInfo(List enumHeaderS return apiClient.invokeAPI("FakeApi.testEnumParameters", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } private ApiResponse testGroupParametersWithHttpInfo(Integer requiredStringGroup, Boolean requiredBooleanGroup, Long requiredInt64Group, Integer stringGroup, Boolean booleanGroup, Long int64Group) throws ApiException { @@ -844,7 +844,7 @@ private ApiResponse testGroupParametersWithHttpInfo(Integer requiredString return apiClient.invokeAPI("FakeApi.testGroupParameters", localVarPath, "DELETE", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } public class APItestGroupParametersRequest { @@ -1023,7 +1023,7 @@ public ApiResponse testInlineAdditionalPropertiesWithHttpInfo(Map testJsonFormDataWithHttpInfo(String param, String param return apiClient.invokeAPI("FakeApi.testJsonFormData", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * @@ -1195,6 +1195,6 @@ public ApiResponse testQueryParameterCollectionFormatWithHttpInfo(List testClassnameWithHttpInfo(Client body) throws ApiExce return apiClient.invokeAPI("FakeClassnameTags123Api.testClassname", localVarPath, "PATCH", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } } 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 601ccd197048..71fe54b109ea 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 @@ -112,7 +112,7 @@ public ApiResponse addPetWithHttpInfo(Pet body) throws ApiException { return apiClient.invokeAPI("PetApi.addPet", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Deletes a pet @@ -183,7 +183,7 @@ public ApiResponse deletePetWithHttpInfo(Long petId, String apiKey) throws return apiClient.invokeAPI("PetApi.deletePet", localVarPath, "DELETE", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Finds Pets by status @@ -253,7 +253,7 @@ public ApiResponse> findPetsByStatusWithHttpInfo(List status) return apiClient.invokeAPI("PetApi.findPetsByStatus", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Finds Pets by tags @@ -327,7 +327,7 @@ public ApiResponse> findPetsByTagsWithHttpInfo(Set tags) throws return apiClient.invokeAPI("PetApi.findPetsByTags", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Find pet by ID @@ -399,7 +399,7 @@ public ApiResponse getPetByIdWithHttpInfo(Long petId) throws ApiException { return apiClient.invokeAPI("PetApi.getPetById", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Update an existing pet @@ -469,7 +469,7 @@ public ApiResponse updatePetWithHttpInfo(Pet body) throws ApiException { return apiClient.invokeAPI("PetApi.updatePet", localVarPath, "PUT", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Updates a pet in the store with form data @@ -542,7 +542,7 @@ public ApiResponse updatePetWithFormWithHttpInfo(Long petId, String name, return apiClient.invokeAPI("PetApi.updatePetWithForm", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * uploads an image @@ -618,7 +618,7 @@ public ApiResponse uploadFileWithHttpInfo(Long petId, String a return apiClient.invokeAPI("PetApi.uploadFile", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * uploads an image (required) @@ -699,6 +699,6 @@ public ApiResponse uploadFileWithRequiredFileWithHttpInfo(Long return apiClient.invokeAPI("PetApi.uploadFileWithRequiredFile", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } } 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 31f441e5e1ba..0d259914167b 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 @@ -110,7 +110,7 @@ public ApiResponse deleteOrderWithHttpInfo(String orderId) throws ApiExcep return apiClient.invokeAPI("StoreApi.deleteOrder", localVarPath, "DELETE", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Returns pet inventories by status @@ -170,7 +170,7 @@ public ApiResponse> getInventoryWithHttpInfo() throws ApiEx return apiClient.invokeAPI("StoreApi.getInventory", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Find purchase order by ID @@ -242,7 +242,7 @@ public ApiResponse getOrderByIdWithHttpInfo(Long orderId) throws ApiExcep return apiClient.invokeAPI("StoreApi.getOrderById", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Place an order for a pet @@ -311,6 +311,6 @@ public ApiResponse placeOrderWithHttpInfo(Order body) throws ApiException return apiClient.invokeAPI("StoreApi.placeOrder", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } } 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 5dc1dd791c39..cf1f8c794073 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 @@ -107,7 +107,7 @@ public ApiResponse createUserWithHttpInfo(User body) throws ApiException { return apiClient.invokeAPI("UserApi.createUser", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Creates list of users with given input array @@ -171,7 +171,7 @@ public ApiResponse createUsersWithArrayInputWithHttpInfo(List body) return apiClient.invokeAPI("UserApi.createUsersWithArrayInput", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Creates list of users with given input array @@ -235,7 +235,7 @@ public ApiResponse createUsersWithListInputWithHttpInfo(List body) t return apiClient.invokeAPI("UserApi.createUsersWithListInput", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Delete user @@ -302,7 +302,7 @@ public ApiResponse deleteUserWithHttpInfo(String username) throws ApiExcep return apiClient.invokeAPI("UserApi.deleteUser", localVarPath, "DELETE", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Get user by user name @@ -374,7 +374,7 @@ public ApiResponse getUserByNameWithHttpInfo(String username) throws ApiEx return apiClient.invokeAPI("UserApi.getUserByName", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Logs user into the system @@ -452,7 +452,7 @@ public ApiResponse loginUserWithHttpInfo(String username, String passwor return apiClient.invokeAPI("UserApi.loginUser", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Logs out current logged in user session @@ -509,7 +509,7 @@ public ApiResponse logoutUserWithHttpInfo() throws ApiException { return apiClient.invokeAPI("UserApi.logoutUser", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Updated user @@ -583,6 +583,6 @@ public ApiResponse updateUserWithHttpInfo(String username, User body) thro return apiClient.invokeAPI("UserApi.updateUser", localVarPath, "PUT", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } } 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 ae632d4ebe81..8b5f0c4473cc 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 @@ -835,7 +835,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(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { Entity entity; if (contentType.startsWith("multipart/form-data")) { MultiPart multiPart = new MultiPart(); @@ -859,7 +859,11 @@ 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); + if (isBodyNullable) { // payload is nullable + entity = Entity.entity(obj == null ? Entity.text("null") : obj, contentType); + } else { + entity = Entity.entity(obj == null ? Entity.text("") : obj, contentType); + } } return entity; } @@ -997,6 +1001,7 @@ public File prepareDownloadFile(Response response) throws IOException { * @param contentType The request's Content-Type header * @param authNames The authentications to apply * @param returnType The return type into which to deserialize the response + * @param isBodyNullable True if the body is nullable * @return The response body in type of string * @throws ApiException API exception */ @@ -1012,7 +1017,8 @@ public ApiResponse invokeAPI( String accept, String contentType, String[] authNames, - GenericType returnType) + GenericType returnType, + boolean isBodyNullable) throws ApiException { // Not using `.target(targetURL).path(path)` below, @@ -1059,7 +1065,7 @@ public ApiResponse invokeAPI( } } - Entity entity = serialize(body, formParams, contentType); + Entity entity = serialize(body, formParams, contentType, isBodyNullable); // put all headers in one place Map allHeaderParams = new HashMap<>(defaultHeaderMap); @@ -1158,8 +1164,8 @@ 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 { - return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType); + 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, boolean isBodyNullable) throws ApiException { + return invokeAPI(null, path, method, queryParams, body, headerParams, cookieParams, formParams, accept, contentType, authNames, returnType, isBodyNullable); } /** 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 2f2160899f82..7a9f7b08b63e 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 @@ -110,6 +110,6 @@ public ApiResponse call123testSpecialTagsWithHttpInfo(Client client) thr return apiClient.invokeAPI("AnotherFakeApi.call123testSpecialTags", localVarPath, "PATCH", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } } 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 4444a7191f88..a49fdc11d859 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 @@ -103,6 +103,6 @@ public ApiResponse fooGetWithHttpInfo() throws ApiExcepti return apiClient.invokeAPI("DefaultApi.fooGet", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } } 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 dacb046a1dfc..b8893ee704f7 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 @@ -112,7 +112,7 @@ public ApiResponse fakeHealthGetWithHttpInfo() throws ApiExce return apiClient.invokeAPI("FakeApi.fakeHealthGet", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * @@ -174,7 +174,7 @@ public ApiResponse fakeOuterBooleanSerializeWithHttpInfo(Boolean body) return apiClient.invokeAPI("FakeApi.fakeOuterBooleanSerialize", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * @@ -236,7 +236,7 @@ public ApiResponse fakeOuterCompositeSerializeWithHttpInfo(Outer return apiClient.invokeAPI("FakeApi.fakeOuterCompositeSerialize", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * @@ -298,7 +298,7 @@ public ApiResponse fakeOuterNumberSerializeWithHttpInfo(BigDecimal b return apiClient.invokeAPI("FakeApi.fakeOuterNumberSerialize", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * @@ -360,7 +360,7 @@ public ApiResponse fakeOuterStringSerializeWithHttpInfo(String body) thr return apiClient.invokeAPI("FakeApi.fakeOuterStringSerialize", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Array of Enums @@ -420,7 +420,7 @@ public ApiResponse> getArrayOfEnumsWithHttpInfo() throws ApiExce return apiClient.invokeAPI("FakeApi.getArrayOfEnums", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * @@ -484,7 +484,7 @@ public ApiResponse testBodyWithFileSchemaWithHttpInfo(FileSchemaTestClass return apiClient.invokeAPI("FakeApi.testBodyWithFileSchema", localVarPath, "PUT", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * @@ -556,7 +556,7 @@ public ApiResponse testBodyWithQueryParamsWithHttpInfo(String query, User return apiClient.invokeAPI("FakeApi.testBodyWithQueryParams", localVarPath, "PUT", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * To test \"client\" model @@ -623,7 +623,7 @@ public ApiResponse testClientModelWithHttpInfo(Client client) throws Api return apiClient.invokeAPI("FakeApi.testClientModel", localVarPath, "PATCH", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Fake endpoint for testing various parameters 假端點 偽のエンドポイント 가짜 엔드 포인트 @@ -758,7 +758,7 @@ public ApiResponse testEndpointParametersWithHttpInfo(BigDecimal number, D return apiClient.invokeAPI("FakeApi.testEndpointParameters", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * To test enum parameters @@ -845,7 +845,7 @@ public ApiResponse testEnumParametersWithHttpInfo(List enumHeaderS return apiClient.invokeAPI("FakeApi.testEnumParameters", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } private ApiResponse testGroupParametersWithHttpInfo(Integer requiredStringGroup, Boolean requiredBooleanGroup, Long requiredInt64Group, Integer stringGroup, Boolean booleanGroup, Long int64Group) throws ApiException { @@ -901,7 +901,7 @@ private ApiResponse testGroupParametersWithHttpInfo(Integer requiredString return apiClient.invokeAPI("FakeApi.testGroupParameters", localVarPath, "DELETE", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } public class APItestGroupParametersRequest { @@ -1080,7 +1080,7 @@ public ApiResponse testInlineAdditionalPropertiesWithHttpInfo(Map testJsonFormDataWithHttpInfo(String param, String param return apiClient.invokeAPI("FakeApi.testJsonFormData", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * @@ -1252,6 +1252,6 @@ public ApiResponse testQueryParameterCollectionFormatWithHttpInfo(List testClassnameWithHttpInfo(Client client) throws ApiEx return apiClient.invokeAPI("FakeClassnameTags123Api.testClassname", localVarPath, "PATCH", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } } 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 6cd72b9701ea..3637b9e61a5a 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 @@ -109,7 +109,7 @@ public ApiResponse addPetWithHttpInfo(Pet pet) throws ApiException { return apiClient.invokeAPI("PetApi.addPet", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Deletes a pet @@ -178,7 +178,7 @@ public ApiResponse deletePetWithHttpInfo(Long petId, String apiKey) throws return apiClient.invokeAPI("PetApi.deletePet", localVarPath, "DELETE", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Finds Pets by status @@ -248,7 +248,7 @@ public ApiResponse> findPetsByStatusWithHttpInfo(List status) return apiClient.invokeAPI("PetApi.findPetsByStatus", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Finds Pets by tags @@ -322,7 +322,7 @@ public ApiResponse> findPetsByTagsWithHttpInfo(List tags) thro return apiClient.invokeAPI("PetApi.findPetsByTags", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Find pet by ID @@ -394,7 +394,7 @@ public ApiResponse getPetByIdWithHttpInfo(Long petId) throws ApiException { return apiClient.invokeAPI("PetApi.getPetById", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Update an existing pet @@ -462,7 +462,7 @@ public ApiResponse updatePetWithHttpInfo(Pet pet) throws ApiException { return apiClient.invokeAPI("PetApi.updatePet", localVarPath, "PUT", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Updates a pet in the store with form data @@ -535,7 +535,7 @@ public ApiResponse updatePetWithFormWithHttpInfo(Long petId, String name, return apiClient.invokeAPI("PetApi.updatePetWithForm", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * uploads an image @@ -611,7 +611,7 @@ public ApiResponse uploadFileWithHttpInfo(Long petId, String a return apiClient.invokeAPI("PetApi.uploadFile", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * uploads an image (required) @@ -692,6 +692,6 @@ public ApiResponse uploadFileWithRequiredFileWithHttpInfo(Long return apiClient.invokeAPI("PetApi.uploadFileWithRequiredFile", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } } 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 eb4f5d3db86e..75fa95592c05 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 @@ -110,7 +110,7 @@ public ApiResponse deleteOrderWithHttpInfo(String orderId) throws ApiExcep return apiClient.invokeAPI("StoreApi.deleteOrder", localVarPath, "DELETE", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Returns pet inventories by status @@ -170,7 +170,7 @@ public ApiResponse> getInventoryWithHttpInfo() throws ApiEx return apiClient.invokeAPI("StoreApi.getInventory", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Find purchase order by ID @@ -242,7 +242,7 @@ public ApiResponse getOrderByIdWithHttpInfo(Long orderId) throws ApiExcep return apiClient.invokeAPI("StoreApi.getOrderById", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Place an order for a pet @@ -311,6 +311,6 @@ public ApiResponse placeOrderWithHttpInfo(Order order) throws ApiExceptio return apiClient.invokeAPI("StoreApi.placeOrder", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } } 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 0e0f07dca9fd..bdf2c62fcf0a 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 @@ -107,7 +107,7 @@ public ApiResponse createUserWithHttpInfo(User user) throws ApiException { return apiClient.invokeAPI("UserApi.createUser", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Creates list of users with given input array @@ -171,7 +171,7 @@ public ApiResponse createUsersWithArrayInputWithHttpInfo(List user) return apiClient.invokeAPI("UserApi.createUsersWithArrayInput", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Creates list of users with given input array @@ -235,7 +235,7 @@ public ApiResponse createUsersWithListInputWithHttpInfo(List user) t return apiClient.invokeAPI("UserApi.createUsersWithListInput", localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Delete user @@ -302,7 +302,7 @@ public ApiResponse deleteUserWithHttpInfo(String username) throws ApiExcep return apiClient.invokeAPI("UserApi.deleteUser", localVarPath, "DELETE", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Get user by user name @@ -374,7 +374,7 @@ public ApiResponse getUserByNameWithHttpInfo(String username) throws ApiEx return apiClient.invokeAPI("UserApi.getUserByName", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Logs user into the system @@ -452,7 +452,7 @@ public ApiResponse loginUserWithHttpInfo(String username, String passwor return apiClient.invokeAPI("UserApi.loginUser", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, localVarReturnType); + localVarAuthNames, localVarReturnType, false); } /** * Logs out current logged in user session @@ -509,7 +509,7 @@ public ApiResponse logoutUserWithHttpInfo() throws ApiException { return apiClient.invokeAPI("UserApi.logoutUser", localVarPath, "GET", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } /** * Updated user @@ -583,6 +583,6 @@ public ApiResponse updateUserWithHttpInfo(String username, User user) thro return apiClient.invokeAPI("UserApi.updateUser", localVarPath, "PUT", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAccept, localVarContentType, - localVarAuthNames, null); + localVarAuthNames, null, false); } } From 3e82babceef7e9ece42fa89036660d9a8a6134e4 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Fri, 26 Jun 2020 10:46:28 +0800 Subject: [PATCH 2/6] update serializeToString with nullable body --- .../Java/libraries/jersey2/ApiClient.mustache | 11 ++++++++--- .../main/java/org/openapitools/client/ApiClient.java | 11 ++++++++--- .../main/java/org/openapitools/client/ApiClient.java | 11 ++++++++--- 3 files changed, 24 insertions(+), 9 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 6af3c69c45a2..508f730445d9 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 @@ -878,10 +878,11 @@ public class ApiClient { * @param obj Object * @param formParams Form parameters * @param contentType Context type + * @param isBodyNulalble True if the body is nullable * @return String * @throws ApiException API exception */ - public String serializeToString(Object obj, Map formParams, String contentType) throws ApiException { + public String serializeToString(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { try { if (contentType.startsWith("multipart/form-data")) { throw new ApiException("multipart/form-data not yet supported for serializeToString (http signature authentication)"); @@ -897,7 +898,11 @@ public class ApiClient { return formString.substring(0, formString.length() - 1); } } else { - return json.getMapper().writeValueAsString(obj); + if (isBodyNullable) { + return obj == null ? "null" : json.getMapper().writeValueAsString(obj); + } else { + return json.getMapper().writeValueAsString(obj); + } } } catch (Exception ex) { throw new ApiException("Failed to perform serializeToString: " + ex.toString()); @@ -1087,7 +1092,7 @@ public class ApiClient { queryParams, allHeaderParams, cookieParams, - serializeToString(body, formParams, contentType), + serializeToString(body, formParams, contentType, isBodyNullable), method, target.getUri()); 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 05acce295f6d..c4f41c36e077 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 @@ -795,10 +795,11 @@ public Entity serialize(Object obj, Map formParams, String co * @param obj Object * @param formParams Form parameters * @param contentType Context type + * @param isBodyNulalble True if the body is nullable * @return String * @throws ApiException API exception */ - public String serializeToString(Object obj, Map formParams, String contentType) throws ApiException { + public String serializeToString(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { try { if (contentType.startsWith("multipart/form-data")) { throw new ApiException("multipart/form-data not yet supported for serializeToString (http signature authentication)"); @@ -814,7 +815,11 @@ public String serializeToString(Object obj, Map formParams, Stri return formString.substring(0, formString.length() - 1); } } else { - return json.getMapper().writeValueAsString(obj); + if (isBodyNullable) { + return obj == null ? "null" : json.getMapper().writeValueAsString(obj); + } else { + return json.getMapper().writeValueAsString(obj); + } } } catch (Exception ex) { throw new ApiException("Failed to perform serializeToString: " + ex.toString()); @@ -998,7 +1003,7 @@ public ApiResponse invokeAPI( queryParams, allHeaderParams, cookieParams, - serializeToString(body, formParams, contentType), + serializeToString(body, formParams, contentType, isBodyNullable), method, target.getUri()); 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 8b5f0c4473cc..c8672427e8a2 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 @@ -874,10 +874,11 @@ public Entity serialize(Object obj, Map formParams, String co * @param obj Object * @param formParams Form parameters * @param contentType Context type + * @param isBodyNulalble True if the body is nullable * @return String * @throws ApiException API exception */ - public String serializeToString(Object obj, Map formParams, String contentType) throws ApiException { + public String serializeToString(Object obj, Map formParams, String contentType, boolean isBodyNullable) throws ApiException { try { if (contentType.startsWith("multipart/form-data")) { throw new ApiException("multipart/form-data not yet supported for serializeToString (http signature authentication)"); @@ -893,7 +894,11 @@ public String serializeToString(Object obj, Map formParams, Stri return formString.substring(0, formString.length() - 1); } } else { - return json.getMapper().writeValueAsString(obj); + if (isBodyNullable) { + return obj == null ? "null" : json.getMapper().writeValueAsString(obj); + } else { + return json.getMapper().writeValueAsString(obj); + } } } catch (Exception ex) { throw new ApiException("Failed to perform serializeToString: " + ex.toString()); @@ -1077,7 +1082,7 @@ public ApiResponse invokeAPI( queryParams, allHeaderParams, cookieParams, - serializeToString(body, formParams, contentType), + serializeToString(body, formParams, contentType, isBodyNullable), method, target.getUri()); From 9efcee6544a6789b20ca4a4a57f66cfa396a558a Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sat, 27 Jun 2020 15:42:29 +0800 Subject: [PATCH 3/6] add nullable support to body parameter --- .../main/java/org/openapitools/codegen/DefaultCodegen.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 50e35cf40d11..9e1b659fd41e 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -5795,6 +5795,7 @@ private void addBodyModelSchema(CodegenParameter codegenParameter, String name, codegenParameter.baseType = codegenModel.classname; codegenParameter.dataType = getTypeDeclaration(codegenModel.classname); codegenParameter.description = codegenModel.description; + codegenParameter.isNullable = codegenModel.isNullable; imports.add(codegenParameter.baseType); } else { CodegenProperty codegenProperty = fromProperty("property", schema); @@ -5808,6 +5809,7 @@ private void addBodyModelSchema(CodegenParameter codegenParameter, String name, codegenParameter.baseType = codegenParameter.baseName; codegenParameter.dataType = getTypeDeclaration(codegenModelName); codegenParameter.description = codegenProperty.getDescription(); + codegenParameter.isNullable = codegenProperty.isNullable; } else { if (ModelUtils.isMapSchema(schema)) {// http body is map LOGGER.error("Map should be supported. Please report to openapi-generator github repo about the issue."); @@ -5916,6 +5918,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set imports, S codegenParameter.baseType = getSchemaType(inner); codegenParameter.isContainer = Boolean.TRUE; codegenParameter.isMapContainer = Boolean.TRUE; + codegenParameter.isNullable = codegenProperty.isNullable; setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty); @@ -5958,6 +5961,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set imports, S codegenParameter.baseType = getSchemaType(inner); codegenParameter.isContainer = Boolean.TRUE; codegenParameter.isListContainer = Boolean.TRUE; + codegenParameter.isNullable = codegenProperty.isNullable; setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty); // set nullable @@ -5981,6 +5985,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set imports, S codegenParameter.baseType = codegenProperty.baseType; codegenParameter.dataType = codegenProperty.dataType; codegenParameter.description = codegenProperty.description; + codegenParameter.isNullable = codegenProperty.isNullable; codegenParameter.paramName = toParamName(codegenParameter.baseName); } setParameterBooleanFlagWithCodegenProperty(codegenParameter, codegenProperty); @@ -6010,6 +6015,7 @@ public CodegenParameter fromRequestBody(RequestBody body, Set imports, S codegenParameter.minLength = codegenProperty.minLength; codegenParameter.maxLength = codegenProperty.maxLength; codegenParameter.pattern = codegenProperty.pattern; + codegenParameter.isNullable = codegenProperty.isNullable; if (codegenProperty.complexType != null) { imports.add(codegenProperty.complexType); From 3af1838a9bf421e633165dbd8e62bb7740b539ef Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sat, 27 Jun 2020 23:31:59 +0800 Subject: [PATCH 4/6] minor code format change --- .../openapitools/codegen/DefaultCodegen.java | 144 +++++++++--------- 1 file changed, 71 insertions(+), 73 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 9e1b659fd41e..e5a07d45cabc 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -25,7 +25,23 @@ import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Mustache.Compiler; import com.samskivert.mustache.Mustache.Lambda; - +import io.swagger.v3.core.util.Json; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.PathItem; +import io.swagger.v3.oas.models.callbacks.Callback; +import io.swagger.v3.oas.models.examples.Example; +import io.swagger.v3.oas.models.headers.Header; +import io.swagger.v3.oas.models.media.*; +import io.swagger.v3.oas.models.parameters.*; +import io.swagger.v3.oas.models.responses.ApiResponse; +import io.swagger.v3.oas.models.responses.ApiResponses; +import io.swagger.v3.oas.models.security.OAuthFlow; +import io.swagger.v3.oas.models.security.OAuthFlows; +import io.swagger.v3.oas.models.security.SecurityScheme; +import io.swagger.v3.oas.models.servers.Server; +import io.swagger.v3.oas.models.servers.ServerVariable; +import io.swagger.v3.parser.util.SchemaTypeUtil; import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; @@ -56,24 +72,6 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import io.swagger.v3.core.util.Json; -import io.swagger.v3.oas.models.OpenAPI; -import io.swagger.v3.oas.models.Operation; -import io.swagger.v3.oas.models.PathItem; -import io.swagger.v3.oas.models.callbacks.Callback; -import io.swagger.v3.oas.models.examples.Example; -import io.swagger.v3.oas.models.headers.Header; -import io.swagger.v3.oas.models.media.*; -import io.swagger.v3.oas.models.parameters.*; -import io.swagger.v3.oas.models.responses.ApiResponse; -import io.swagger.v3.oas.models.responses.ApiResponses; -import io.swagger.v3.oas.models.security.OAuthFlow; -import io.swagger.v3.oas.models.security.OAuthFlows; -import io.swagger.v3.oas.models.security.SecurityScheme; -import io.swagger.v3.oas.models.servers.Server; -import io.swagger.v3.oas.models.servers.ServerVariable; -import io.swagger.v3.parser.util.SchemaTypeUtil; - import static org.openapitools.codegen.utils.OnceLogger.once; import static org.openapitools.codegen.utils.StringUtils.*; @@ -1486,20 +1484,20 @@ public DefaultCodegen() { // option to change how we process + set the data in the 'additionalProperties' keyword. CliOption disallowAdditionalPropertiesIfNotPresentOpt = CliOption.newBoolean( - CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, - CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_DESC).defaultValue(Boolean.TRUE.toString()); + CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, + CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_DESC).defaultValue(Boolean.TRUE.toString()); Map disallowAdditionalPropertiesIfNotPresentOpts = new HashMap<>(); disallowAdditionalPropertiesIfNotPresentOpts.put("false", - "The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications."); + "The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications."); disallowAdditionalPropertiesIfNotPresentOpts.put("true", - "when the 'additionalProperties' keyword is not present in a schema, " + - "the value of 'additionalProperties' is automatically set to false, i.e. no additional properties are allowed. " + - "Note: this mode is not compliant with the JSON schema specification. " + - "This is the original openapi-generator behavior."); + "when the 'additionalProperties' keyword is not present in a schema, " + + "the value of 'additionalProperties' is automatically set to false, i.e. no additional properties are allowed. " + + "Note: this mode is not compliant with the JSON schema specification. " + + "This is the original openapi-generator behavior."); disallowAdditionalPropertiesIfNotPresentOpt.setEnum(disallowAdditionalPropertiesIfNotPresentOpts); cliOptions.add(disallowAdditionalPropertiesIfNotPresentOpt); this.setDisallowAdditionalPropertiesIfNotPresent(true); - + // initialize special character mapping initalizeSpecialCharacterMapping(); @@ -2640,7 +2638,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) { Integer hasDiscriminatorCnt = 0; Integer hasNullTypeCnt = 0; Set discriminatorsPropNames = new HashSet<>(); - for (Schema oneOf: composedSchema.getOneOf()) { + for (Schema oneOf : composedSchema.getOneOf()) { if (ModelUtils.isNullType(oneOf)) { // The null type does not have a discriminator. Skip. hasNullTypeCnt++; @@ -2654,7 +2652,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) { } if (discriminatorsPropNames.size() > 1) { throw new RuntimeException("The oneOf schemas have conflicting discriminator property names. " + - "oneOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames)); + "oneOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames)); } if ((hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getOneOf().size() && discriminatorsPropNames.size() == 1) { disc.setPropertyName(foundDisc.getPropertyName()); @@ -2669,7 +2667,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) { Integer hasDiscriminatorCnt = 0; Integer hasNullTypeCnt = 0; Set discriminatorsPropNames = new HashSet<>(); - for (Schema anyOf: composedSchema.getAnyOf()) { + for (Schema anyOf : composedSchema.getAnyOf()) { if (ModelUtils.isNullType(anyOf)) { // The null type does not have a discriminator. Skip. hasNullTypeCnt++; @@ -2683,7 +2681,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) { } if (discriminatorsPropNames.size() > 1) { throw new RuntimeException("The anyOf schemas have conflicting discriminator property names. " + - "anyOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames)); + "anyOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames)); } if ((hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getAnyOf().size() && discriminatorsPropNames.size() == 1) { disc.setPropertyName(foundDisc.getPropertyName()); @@ -2720,7 +2718,7 @@ protected List getOneOfAnyOfDescendants(String composedSchemaName, if (schemaList == null) { continue; } - for (Schema sc: schemaList) { + for (Schema sc : schemaList) { if (ModelUtils.isNullType(sc)) { continue; } @@ -2875,7 +2873,7 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch * Handle the model for the 'additionalProperties' keyword in the OAS schema. * * @param codegenModel The codegen representation of the schema. - * @param schema the input OAS schema. + * @param schema the input OAS schema. */ protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) { addParentContainer(codegenModel, codegenModel.name, schema); @@ -3503,7 +3501,7 @@ protected void handleMethodResponse(Operation operation, op.returnType = cm.dataType; op.returnFormat = cm.dataFormat; op.hasReference = schemas != null && schemas.containsKey(op.returnBaseType); - + // lookup discriminator Schema schema = schemas.get(op.returnBaseType); if (schema != null) { @@ -3965,8 +3963,8 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) { r.containerType = cp.containerType; r.isMapContainer = "map".equals(cp.containerType); r.isListContainer = "list".equalsIgnoreCase(cp.containerType) || - "array".equalsIgnoreCase(cp.containerType) || - "set".equalsIgnoreCase(cp.containerType); + "array".equalsIgnoreCase(cp.containerType) || + "set".equalsIgnoreCase(cp.containerType); } else { r.simpleType = true; } @@ -4317,7 +4315,7 @@ public boolean isDataTypeBinary(String dataType) { return false; } } - + // TODO revise below as it should be replaced by ModelUtils.isFileSchema(parameterSchema) public boolean isDataTypeFile(String dataType) { if (dataType != null) { @@ -4589,15 +4587,15 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera * of the 'additionalProperties' keyword. Some language generator use class inheritance * to implement additional properties. For example, in Java the generated model class * has 'extends HashMap' to represent the additional properties. - * + *

* TODO: it's not a good idea to use single class inheritance to implement * additionalProperties. That may work for non-composed schemas, but that does not * work for composed 'allOf' schemas. For example, in Java, if additionalProperties * is set to true (which it should be by default, per OAS spec), then the generated * code has extends HashMap. That wouldn't work for composed 'allOf' schemas. - * - * @param model the codegen representation of the OAS schema. - * @param name the name of the model. + * + * @param model the codegen representation of the OAS schema. + * @param name the name of the model. * @param schema the input OAS schema. */ protected void addParentContainer(CodegenModel model, String name, Schema schema) { @@ -6376,18 +6374,18 @@ public int hashCode() { * Return true if the schema value can be any type, i.e. it can be * the null value, integer, number, string, object or array. * One use case is when the "type" attribute in the OAS schema is unspecified. - * + *

* Examples: - * - * arbitraryTypeValue: - * description: This is an arbitrary type schema. - * It is not a free-form object. - * The value can be any type except the 'null' value. - * arbitraryTypeNullableValue: - * description: This is an arbitrary type schema. - * It is not a free-form object. - * The value can be any type, including the 'null' value. - * nullable: true + *

+ * arbitraryTypeValue: + * description: This is an arbitrary type schema. + * It is not a free-form object. + * The value can be any type except the 'null' value. + * arbitraryTypeNullableValue: + * description: This is an arbitrary type schema. + * It is not a free-form object. + * The value can be any type, including the 'null' value. + * nullable: true * * @param schema the OAS schema. * @return true if the schema value can be an arbitrary type. @@ -6416,30 +6414,30 @@ public boolean isAnyTypeSchema(Schema schema) { /** * Check to see if the schema is a free form object. - * + *

* A free form object is an object (i.e. 'type: object' in a OAS document) that: * 1) Does not define properties, and * 2) Is not a composed schema (no anyOf, oneOf, allOf), and * 3) additionalproperties is not defined, or additionalproperties: true, or additionalproperties: {}. - * + *

* Examples: - * + *

* components: - * schemas: - * arbitraryObject: - * type: object - * description: This is a free-form object. - * The value must be a map of strings to values. The value cannot be 'null'. - * It cannot be array, string, integer, number. - * arbitraryNullableObject: - * type: object - * description: This is a free-form object. - * The value must be a map of strings to values. The value can be 'null', - * It cannot be array, string, integer, number. - * nullable: true - * arbitraryTypeValue: - * description: This is NOT a free-form object. - * The value can be any type except the 'null' value. + * schemas: + * arbitraryObject: + * type: object + * description: This is a free-form object. + * The value must be a map of strings to values. The value cannot be 'null'. + * It cannot be array, string, integer, number. + * arbitraryNullableObject: + * type: object + * description: This is a free-form object. + * The value must be a map of strings to values. The value can be 'null', + * It cannot be array, string, integer, number. + * nullable: true + * arbitraryTypeValue: + * description: This is NOT a free-form object. + * The value can be any type except the 'null' value. * * @param schema potentially containing a '$ref' * @return true if it's a free-form object @@ -6450,7 +6448,7 @@ protected boolean isFreeFormObject(Schema schema) { /** * Returns the additionalProperties Schema for the specified input schema. - * + *

* The additionalProperties keyword is used to control the handling of additional, undeclared * properties, that is, properties whose names are not listed in the properties keyword. * The additionalProperties keyword may be either a boolean or an object. @@ -6458,10 +6456,10 @@ protected boolean isFreeFormObject(Schema schema) { * By default when the additionalProperties keyword is not specified in the input schema, * any additional properties are allowed. This is equivalent to setting additionalProperties * to the boolean value True or setting additionalProperties: {} - * + * * @param schema the input schema that may or may not have the additionalProperties keyword. * @return the Schema of the additionalProperties. The null value is returned if no additional - * properties are allowed. + * properties are allowed. */ protected Schema getAdditionalProperties(Schema schema) { return ModelUtils.getAdditionalProperties(openAPI, schema); From 67ad2bdf8bb516a7f854f18ccc8b116d1ccb41f8 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sat, 27 Jun 2020 23:32:25 +0800 Subject: [PATCH 5/6] Revert "minor code format change" This reverts commit 3af1838a9bf421e633165dbd8e62bb7740b539ef. --- .../openapitools/codegen/DefaultCodegen.java | 144 +++++++++--------- 1 file changed, 73 insertions(+), 71 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index e5a07d45cabc..9e1b659fd41e 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -25,23 +25,7 @@ import com.samskivert.mustache.Mustache; import com.samskivert.mustache.Mustache.Compiler; import com.samskivert.mustache.Mustache.Lambda; -import io.swagger.v3.core.util.Json; -import io.swagger.v3.oas.models.OpenAPI; -import io.swagger.v3.oas.models.Operation; -import io.swagger.v3.oas.models.PathItem; -import io.swagger.v3.oas.models.callbacks.Callback; -import io.swagger.v3.oas.models.examples.Example; -import io.swagger.v3.oas.models.headers.Header; -import io.swagger.v3.oas.models.media.*; -import io.swagger.v3.oas.models.parameters.*; -import io.swagger.v3.oas.models.responses.ApiResponse; -import io.swagger.v3.oas.models.responses.ApiResponses; -import io.swagger.v3.oas.models.security.OAuthFlow; -import io.swagger.v3.oas.models.security.OAuthFlows; -import io.swagger.v3.oas.models.security.SecurityScheme; -import io.swagger.v3.oas.models.servers.Server; -import io.swagger.v3.oas.models.servers.ServerVariable; -import io.swagger.v3.parser.util.SchemaTypeUtil; + import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.StringEscapeUtils; import org.apache.commons.lang3.StringUtils; @@ -72,6 +56,24 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import io.swagger.v3.core.util.Json; +import io.swagger.v3.oas.models.OpenAPI; +import io.swagger.v3.oas.models.Operation; +import io.swagger.v3.oas.models.PathItem; +import io.swagger.v3.oas.models.callbacks.Callback; +import io.swagger.v3.oas.models.examples.Example; +import io.swagger.v3.oas.models.headers.Header; +import io.swagger.v3.oas.models.media.*; +import io.swagger.v3.oas.models.parameters.*; +import io.swagger.v3.oas.models.responses.ApiResponse; +import io.swagger.v3.oas.models.responses.ApiResponses; +import io.swagger.v3.oas.models.security.OAuthFlow; +import io.swagger.v3.oas.models.security.OAuthFlows; +import io.swagger.v3.oas.models.security.SecurityScheme; +import io.swagger.v3.oas.models.servers.Server; +import io.swagger.v3.oas.models.servers.ServerVariable; +import io.swagger.v3.parser.util.SchemaTypeUtil; + import static org.openapitools.codegen.utils.OnceLogger.once; import static org.openapitools.codegen.utils.StringUtils.*; @@ -1484,20 +1486,20 @@ public DefaultCodegen() { // option to change how we process + set the data in the 'additionalProperties' keyword. CliOption disallowAdditionalPropertiesIfNotPresentOpt = CliOption.newBoolean( - CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, - CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_DESC).defaultValue(Boolean.TRUE.toString()); + CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, + CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_DESC).defaultValue(Boolean.TRUE.toString()); Map disallowAdditionalPropertiesIfNotPresentOpts = new HashMap<>(); disallowAdditionalPropertiesIfNotPresentOpts.put("false", - "The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications."); + "The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications."); disallowAdditionalPropertiesIfNotPresentOpts.put("true", - "when the 'additionalProperties' keyword is not present in a schema, " + - "the value of 'additionalProperties' is automatically set to false, i.e. no additional properties are allowed. " + - "Note: this mode is not compliant with the JSON schema specification. " + - "This is the original openapi-generator behavior."); + "when the 'additionalProperties' keyword is not present in a schema, " + + "the value of 'additionalProperties' is automatically set to false, i.e. no additional properties are allowed. " + + "Note: this mode is not compliant with the JSON schema specification. " + + "This is the original openapi-generator behavior."); disallowAdditionalPropertiesIfNotPresentOpt.setEnum(disallowAdditionalPropertiesIfNotPresentOpts); cliOptions.add(disallowAdditionalPropertiesIfNotPresentOpt); this.setDisallowAdditionalPropertiesIfNotPresent(true); - + // initialize special character mapping initalizeSpecialCharacterMapping(); @@ -2638,7 +2640,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) { Integer hasDiscriminatorCnt = 0; Integer hasNullTypeCnt = 0; Set discriminatorsPropNames = new HashSet<>(); - for (Schema oneOf : composedSchema.getOneOf()) { + for (Schema oneOf: composedSchema.getOneOf()) { if (ModelUtils.isNullType(oneOf)) { // The null type does not have a discriminator. Skip. hasNullTypeCnt++; @@ -2652,7 +2654,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) { } if (discriminatorsPropNames.size() > 1) { throw new RuntimeException("The oneOf schemas have conflicting discriminator property names. " + - "oneOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames)); + "oneOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames)); } if ((hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getOneOf().size() && discriminatorsPropNames.size() == 1) { disc.setPropertyName(foundDisc.getPropertyName()); @@ -2667,7 +2669,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) { Integer hasDiscriminatorCnt = 0; Integer hasNullTypeCnt = 0; Set discriminatorsPropNames = new HashSet<>(); - for (Schema anyOf : composedSchema.getAnyOf()) { + for (Schema anyOf: composedSchema.getAnyOf()) { if (ModelUtils.isNullType(anyOf)) { // The null type does not have a discriminator. Skip. hasNullTypeCnt++; @@ -2681,7 +2683,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) { } if (discriminatorsPropNames.size() > 1) { throw new RuntimeException("The anyOf schemas have conflicting discriminator property names. " + - "anyOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames)); + "anyOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames)); } if ((hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getAnyOf().size() && discriminatorsPropNames.size() == 1) { disc.setPropertyName(foundDisc.getPropertyName()); @@ -2718,7 +2720,7 @@ protected List getOneOfAnyOfDescendants(String composedSchemaName, if (schemaList == null) { continue; } - for (Schema sc : schemaList) { + for (Schema sc: schemaList) { if (ModelUtils.isNullType(sc)) { continue; } @@ -2873,7 +2875,7 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch * Handle the model for the 'additionalProperties' keyword in the OAS schema. * * @param codegenModel The codegen representation of the schema. - * @param schema the input OAS schema. + * @param schema the input OAS schema. */ protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) { addParentContainer(codegenModel, codegenModel.name, schema); @@ -3501,7 +3503,7 @@ protected void handleMethodResponse(Operation operation, op.returnType = cm.dataType; op.returnFormat = cm.dataFormat; op.hasReference = schemas != null && schemas.containsKey(op.returnBaseType); - + // lookup discriminator Schema schema = schemas.get(op.returnBaseType); if (schema != null) { @@ -3963,8 +3965,8 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) { r.containerType = cp.containerType; r.isMapContainer = "map".equals(cp.containerType); r.isListContainer = "list".equalsIgnoreCase(cp.containerType) || - "array".equalsIgnoreCase(cp.containerType) || - "set".equalsIgnoreCase(cp.containerType); + "array".equalsIgnoreCase(cp.containerType) || + "set".equalsIgnoreCase(cp.containerType); } else { r.simpleType = true; } @@ -4315,7 +4317,7 @@ public boolean isDataTypeBinary(String dataType) { return false; } } - + // TODO revise below as it should be replaced by ModelUtils.isFileSchema(parameterSchema) public boolean isDataTypeFile(String dataType) { if (dataType != null) { @@ -4587,15 +4589,15 @@ public void addOperationToGroup(String tag, String resourcePath, Operation opera * of the 'additionalProperties' keyword. Some language generator use class inheritance * to implement additional properties. For example, in Java the generated model class * has 'extends HashMap' to represent the additional properties. - *

+ * * TODO: it's not a good idea to use single class inheritance to implement * additionalProperties. That may work for non-composed schemas, but that does not * work for composed 'allOf' schemas. For example, in Java, if additionalProperties * is set to true (which it should be by default, per OAS spec), then the generated * code has extends HashMap. That wouldn't work for composed 'allOf' schemas. - * - * @param model the codegen representation of the OAS schema. - * @param name the name of the model. + * + * @param model the codegen representation of the OAS schema. + * @param name the name of the model. * @param schema the input OAS schema. */ protected void addParentContainer(CodegenModel model, String name, Schema schema) { @@ -6374,18 +6376,18 @@ public int hashCode() { * Return true if the schema value can be any type, i.e. it can be * the null value, integer, number, string, object or array. * One use case is when the "type" attribute in the OAS schema is unspecified. - *

+ * * Examples: - *

- * arbitraryTypeValue: - * description: This is an arbitrary type schema. - * It is not a free-form object. - * The value can be any type except the 'null' value. - * arbitraryTypeNullableValue: - * description: This is an arbitrary type schema. - * It is not a free-form object. - * The value can be any type, including the 'null' value. - * nullable: true + * + * arbitraryTypeValue: + * description: This is an arbitrary type schema. + * It is not a free-form object. + * The value can be any type except the 'null' value. + * arbitraryTypeNullableValue: + * description: This is an arbitrary type schema. + * It is not a free-form object. + * The value can be any type, including the 'null' value. + * nullable: true * * @param schema the OAS schema. * @return true if the schema value can be an arbitrary type. @@ -6414,30 +6416,30 @@ public boolean isAnyTypeSchema(Schema schema) { /** * Check to see if the schema is a free form object. - *

+ * * A free form object is an object (i.e. 'type: object' in a OAS document) that: * 1) Does not define properties, and * 2) Is not a composed schema (no anyOf, oneOf, allOf), and * 3) additionalproperties is not defined, or additionalproperties: true, or additionalproperties: {}. - *

+ * * Examples: - *

+ * * components: - * schemas: - * arbitraryObject: - * type: object - * description: This is a free-form object. - * The value must be a map of strings to values. The value cannot be 'null'. - * It cannot be array, string, integer, number. - * arbitraryNullableObject: - * type: object - * description: This is a free-form object. - * The value must be a map of strings to values. The value can be 'null', - * It cannot be array, string, integer, number. - * nullable: true - * arbitraryTypeValue: - * description: This is NOT a free-form object. - * The value can be any type except the 'null' value. + * schemas: + * arbitraryObject: + * type: object + * description: This is a free-form object. + * The value must be a map of strings to values. The value cannot be 'null'. + * It cannot be array, string, integer, number. + * arbitraryNullableObject: + * type: object + * description: This is a free-form object. + * The value must be a map of strings to values. The value can be 'null', + * It cannot be array, string, integer, number. + * nullable: true + * arbitraryTypeValue: + * description: This is NOT a free-form object. + * The value can be any type except the 'null' value. * * @param schema potentially containing a '$ref' * @return true if it's a free-form object @@ -6448,7 +6450,7 @@ protected boolean isFreeFormObject(Schema schema) { /** * Returns the additionalProperties Schema for the specified input schema. - *

+ * * The additionalProperties keyword is used to control the handling of additional, undeclared * properties, that is, properties whose names are not listed in the properties keyword. * The additionalProperties keyword may be either a boolean or an object. @@ -6456,10 +6458,10 @@ protected boolean isFreeFormObject(Schema schema) { * By default when the additionalProperties keyword is not specified in the input schema, * any additional properties are allowed. This is equivalent to setting additionalProperties * to the boolean value True or setting additionalProperties: {} - * + * * @param schema the input schema that may or may not have the additionalProperties keyword. * @return the Schema of the additionalProperties. The null value is returned if no additional - * properties are allowed. + * properties are allowed. */ protected Schema getAdditionalProperties(Schema schema) { return ModelUtils.getAdditionalProperties(openAPI, schema); From 3955f0a97785426219f775ce93dc7217ac43dd2d Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sat, 27 Jun 2020 23:41:16 +0800 Subject: [PATCH 6/6] code format fix --- .../openapitools/codegen/DefaultCodegen.java | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 9e1b659fd41e..7e18a8a393cb 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -1486,20 +1486,20 @@ public DefaultCodegen() { // option to change how we process + set the data in the 'additionalProperties' keyword. CliOption disallowAdditionalPropertiesIfNotPresentOpt = CliOption.newBoolean( - CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, - CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_DESC).defaultValue(Boolean.TRUE.toString()); + CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT, + CodegenConstants.DISALLOW_ADDITIONAL_PROPERTIES_IF_NOT_PRESENT_DESC).defaultValue(Boolean.TRUE.toString()); Map disallowAdditionalPropertiesIfNotPresentOpts = new HashMap<>(); disallowAdditionalPropertiesIfNotPresentOpts.put("false", - "The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications."); + "The 'additionalProperties' implementation is compliant with the OAS and JSON schema specifications."); disallowAdditionalPropertiesIfNotPresentOpts.put("true", - "when the 'additionalProperties' keyword is not present in a schema, " + - "the value of 'additionalProperties' is automatically set to false, i.e. no additional properties are allowed. " + - "Note: this mode is not compliant with the JSON schema specification. " + - "This is the original openapi-generator behavior."); + "when the 'additionalProperties' keyword is not present in a schema, " + + "the value of 'additionalProperties' is automatically set to false, i.e. no additional properties are allowed. " + + "Note: this mode is not compliant with the JSON schema specification. " + + "This is the original openapi-generator behavior."); disallowAdditionalPropertiesIfNotPresentOpt.setEnum(disallowAdditionalPropertiesIfNotPresentOpts); cliOptions.add(disallowAdditionalPropertiesIfNotPresentOpt); this.setDisallowAdditionalPropertiesIfNotPresent(true); - + // initialize special character mapping initalizeSpecialCharacterMapping(); @@ -2640,7 +2640,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) { Integer hasDiscriminatorCnt = 0; Integer hasNullTypeCnt = 0; Set discriminatorsPropNames = new HashSet<>(); - for (Schema oneOf: composedSchema.getOneOf()) { + for (Schema oneOf : composedSchema.getOneOf()) { if (ModelUtils.isNullType(oneOf)) { // The null type does not have a discriminator. Skip. hasNullTypeCnt++; @@ -2654,7 +2654,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) { } if (discriminatorsPropNames.size() > 1) { throw new RuntimeException("The oneOf schemas have conflicting discriminator property names. " + - "oneOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames)); + "oneOf schemas must have the same property name, but found " + String.join(", ", discriminatorsPropNames)); } if ((hasDiscriminatorCnt + hasNullTypeCnt) == composedSchema.getOneOf().size() && discriminatorsPropNames.size() == 1) { disc.setPropertyName(foundDisc.getPropertyName()); @@ -2669,7 +2669,7 @@ private Discriminator recursiveGetDiscriminator(Schema sc, OpenAPI openAPI) { Integer hasDiscriminatorCnt = 0; Integer hasNullTypeCnt = 0; Set discriminatorsPropNames = new HashSet<>(); - for (Schema anyOf: composedSchema.getAnyOf()) { + for (Schema anyOf : composedSchema.getAnyOf()) { if (ModelUtils.isNullType(anyOf)) { // The null type does not have a discriminator. Skip. hasNullTypeCnt++; @@ -2720,7 +2720,7 @@ protected List getOneOfAnyOfDescendants(String composedSchemaName, if (schemaList == null) { continue; } - for (Schema sc: schemaList) { + for (Schema sc : schemaList) { if (ModelUtils.isNullType(sc)) { continue; } @@ -2875,7 +2875,7 @@ protected CodegenDiscriminator createDiscriminator(String schemaName, Schema sch * Handle the model for the 'additionalProperties' keyword in the OAS schema. * * @param codegenModel The codegen representation of the schema. - * @param schema the input OAS schema. + * @param schema The input OAS schema. */ protected void addAdditionPropertiesToCodeGenModel(CodegenModel codegenModel, Schema schema) { addParentContainer(codegenModel, codegenModel.name, schema); @@ -3503,7 +3503,7 @@ protected void handleMethodResponse(Operation operation, op.returnType = cm.dataType; op.returnFormat = cm.dataFormat; op.hasReference = schemas != null && schemas.containsKey(op.returnBaseType); - + // lookup discriminator Schema schema = schemas.get(op.returnBaseType); if (schema != null) { @@ -3965,8 +3965,8 @@ public CodegenResponse fromResponse(String responseCode, ApiResponse response) { r.containerType = cp.containerType; r.isMapContainer = "map".equals(cp.containerType); r.isListContainer = "list".equalsIgnoreCase(cp.containerType) || - "array".equalsIgnoreCase(cp.containerType) || - "set".equalsIgnoreCase(cp.containerType); + "array".equalsIgnoreCase(cp.containerType) || + "set".equalsIgnoreCase(cp.containerType); } else { r.simpleType = true; } @@ -4317,7 +4317,7 @@ public boolean isDataTypeBinary(String dataType) { return false; } } - + // TODO revise below as it should be replaced by ModelUtils.isFileSchema(parameterSchema) public boolean isDataTypeFile(String dataType) { if (dataType != null) {