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 582b58938df..0f63601e063 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 @@ -4656,18 +4656,19 @@ public class DefaultCodegen implements CodegenConfig { bodyParameterName = (String) op.vendorExtensions.get("x-codegen-request-body-name"); } bodyParam = fromRequestBody(requestBody, imports, bodyParameterName); - bodyParam.description = escapeText(requestBody.getDescription()); - postProcessParameter(bodyParam); - bodyParams.add(bodyParam); + if (bodyParam != null) { + bodyParam.description = escapeText(requestBody.getDescription()); + postProcessParameter(bodyParam); + bodyParams.add(bodyParam); + if (prependFormOrBodyParameters) { + allParams.add(bodyParam); + } - if (prependFormOrBodyParameters) { - allParams.add(bodyParam); - } - - // add example - if (schemas != null && !isSkipOperationExample()) { - op.requestBodyExamples = new ExampleGenerator(schemas, this.openAPI).generate(null, new ArrayList<>(getConsumesInfo(this.openAPI, operation)), bodyParam.baseType); + // add example + if (schemas != null && !isSkipOperationExample()) { + op.requestBodyExamples = new ExampleGenerator(schemas, this.openAPI).generate(null, new ArrayList<>(getConsumesInfo(this.openAPI, operation)), bodyParam.baseType); + } } } } @@ -7596,6 +7597,10 @@ public class DefaultCodegen implements CodegenConfig { String name = null; LOGGER.debug("Request body = {}", body); Schema schema = ModelUtils.getSchemaFromRequestBody(body); + if (schema == null) { + LOGGER.error("Schema cannot be null in the request body: {}", body); + return null; + } Schema original = null; // check if it's allOf (only 1 sub schema) with or without default/nullable/etc set in the top level if (ModelUtils.isAllOf(schema) && schema.getAllOf().size() == 1 && diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index 06c645a5254..58830aced88 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -1848,7 +1848,7 @@ public class ModelUtils { * @return true if allOf is not empty */ public static boolean hasAllOf(Schema schema) { - if (schema.getAllOf() != null && !schema.getAllOf().isEmpty()) { + if (schema != null && schema.getAllOf() != null && !schema.getAllOf().isEmpty()) { return true; } diff --git a/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml b/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml index f4332f5fe3b..2e02cb701a0 100644 --- a/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml @@ -1196,6 +1196,52 @@ paths: responses: 200: description: OK + /fake/null-request-body: + get: + tags: + - another_fake + summary: null request body + operationId: null-request-body + parameters: + - name: Accept-Language + in: header + schema: + type: string + example: application/json + requestBody: + content: + text/plain: + examples: + Generar Orden por External ID: + value: |- + { + "external_reference": "{{external_order_ref}}", + "notification_url": "www.yourserver.com/yourendpoint", + "sponsor_id": 446566691, + "items": [ + { + "title": "Papas frita", + "currency_id": "{{currency_id}}", + "unit_price": 6000, + "quantity": 1 + }, + { + "title": "Gaseosa", + "currency_id": "{{currency_id}}", + "unit_price": 3000, + "quantity": 1 + } + ]/*, + "taxes": [ + { + "value": 0, + "type": "IVA" + } + ]*/ + } + responses: + '200': + description: '' /values: get: tags: diff --git a/samples/client/petstore/java/okhttp-gson/README.md b/samples/client/petstore/java/okhttp-gson/README.md index 4b2ed954eaf..5bb2a315d28 100644 --- a/samples/client/petstore/java/okhttp-gson/README.md +++ b/samples/client/petstore/java/okhttp-gson/README.md @@ -116,6 +116,7 @@ Class | Method | HTTP request | Description *AnotherFakeApi* | [**call123testSpecialTags**](docs/AnotherFakeApi.md#call123testSpecialTags) | **PATCH** /another-fake/dummy | To test special tags *AnotherFakeApi* | [**getParameterArrayNumber**](docs/AnotherFakeApi.md#getParameterArrayNumber) | **GET** /fake/parameter-array-number | parameter array number default value *AnotherFakeApi* | [**getParameterStringNumber**](docs/AnotherFakeApi.md#getParameterStringNumber) | **GET** /fake/parameter-string-number | parameter string number +*AnotherFakeApi* | [**nullRequestBody**](docs/AnotherFakeApi.md#nullRequestBody) | **GET** /fake/null-request-body | null request body *DefaultApi* | [**fooGet**](docs/DefaultApi.md#fooGet) | **GET** /foo | *FakeApi* | [**fakeHealthGet**](docs/FakeApi.md#fakeHealthGet) | **GET** /fake/health | Health check endpoint *FakeApi* | [**fakeOuterBooleanSerialize**](docs/FakeApi.md#fakeOuterBooleanSerialize) | **POST** /fake/outer/boolean | diff --git a/samples/client/petstore/java/okhttp-gson/api/openapi.yaml b/samples/client/petstore/java/okhttp-gson/api/openapi.yaml index 37b2af48ba6..d8888a185e4 100644 --- a/samples/client/petstore/java/okhttp-gson/api/openapi.yaml +++ b/samples/client/petstore/java/okhttp-gson/api/openapi.yaml @@ -1230,6 +1230,56 @@ paths: tags: - another_fake x-accepts: application/json + /fake/null-request-body: + get: + operationId: null-request-body + parameters: + - explode: false + in: header + name: Accept-Language + required: false + schema: + example: application/json + type: string + style: simple + requestBody: + content: + text/plain: + examples: + Generar Orden por External ID: + value: |- + { + "external_reference": "{{external_order_ref}}", + "notification_url": "www.yourserver.com/yourendpoint", + "sponsor_id": 446566691, + "items": [ + { + "title": "Papas frita", + "currency_id": "{{currency_id}}", + "unit_price": 6000, + "quantity": 1 + }, + { + "title": "Gaseosa", + "currency_id": "{{currency_id}}", + "unit_price": 3000, + "quantity": 1 + } + ]/*, + "taxes": [ + { + "value": 0, + "type": "IVA" + } + ]*/ + } + responses: + "200": + description: "" + summary: null request body + tags: + - another_fake + x-accepts: application/json /values: get: description: "" diff --git a/samples/client/petstore/java/okhttp-gson/docs/AnotherFakeApi.md b/samples/client/petstore/java/okhttp-gson/docs/AnotherFakeApi.md index 6cca209c72b..d21e29ab092 100644 --- a/samples/client/petstore/java/okhttp-gson/docs/AnotherFakeApi.md +++ b/samples/client/petstore/java/okhttp-gson/docs/AnotherFakeApi.md @@ -7,6 +7,7 @@ All URIs are relative to *http://petstore.swagger.io:80/v2* | [**call123testSpecialTags**](AnotherFakeApi.md#call123testSpecialTags) | **PATCH** /another-fake/dummy | To test special tags | | [**getParameterArrayNumber**](AnotherFakeApi.md#getParameterArrayNumber) | **GET** /fake/parameter-array-number | parameter array number default value | | [**getParameterStringNumber**](AnotherFakeApi.md#getParameterStringNumber) | **GET** /fake/parameter-string-number | parameter string number | +| [**nullRequestBody**](AnotherFakeApi.md#nullRequestBody) | **GET** /fake/null-request-body | null request body | @@ -189,3 +190,62 @@ No authorization required |-------------|-------------|------------------| | **200** | OK | - | + +# **nullRequestBody** +> nullRequestBody(acceptLanguage) + +null request body + +### Example +```java +// Import classes: +import org.openapitools.client.ApiClient; +import org.openapitools.client.ApiException; +import org.openapitools.client.Configuration; +import org.openapitools.client.models.*; +import org.openapitools.client.api.AnotherFakeApi; + +public class Example { + public static void main(String[] args) { + ApiClient defaultClient = Configuration.getDefaultApiClient(); + defaultClient.setBasePath("http://petstore.swagger.io:80/v2"); + + AnotherFakeApi apiInstance = new AnotherFakeApi(defaultClient); + String acceptLanguage = "application/json"; // String | + try { + apiInstance.nullRequestBody(acceptLanguage); + } catch (ApiException e) { + System.err.println("Exception when calling AnotherFakeApi#nullRequestBody"); + System.err.println("Status code: " + e.getCode()); + System.err.println("Reason: " + e.getResponseBody()); + System.err.println("Response headers: " + e.getResponseHeaders()); + e.printStackTrace(); + } + } +} +``` + +### Parameters + +| Name | Type | Description | Notes | +|------------- | ------------- | ------------- | -------------| +| **acceptLanguage** | **String**| | [optional] | + +### Return type + +null (empty response body) + +### Authorization + +No authorization required + +### HTTP request headers + + - **Content-Type**: text/plain + - **Accept**: Not defined + +### HTTP response details +| Status code | Description | Response headers | +|-------------|-------------|------------------| +| **200** | | - | + diff --git a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/api/AnotherFakeApi.java b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/api/AnotherFakeApi.java index f7529bc7476..a58d4bf127b 100644 --- a/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/api/AnotherFakeApi.java +++ b/samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/api/AnotherFakeApi.java @@ -438,4 +438,121 @@ public class AnotherFakeApi { localVarApiClient.executeAsync(localVarCall, _callback); return localVarCall; } + /** + * Build call for nullRequestBody + * @param acceptLanguage (optional) + * @param _callback Callback for upload/download progress + * @return Call to execute + * @throws ApiException If fail to serialize the request body object + * @http.response.details + + + +
Status Code Description Response Headers
200 -
+ */ + public okhttp3.Call nullRequestBodyCall(String acceptLanguage, final ApiCallback _callback) throws ApiException { + String basePath = null; + // Operation Servers + String[] localBasePaths = new String[] { }; + + // Determine Base Path to Use + if (localCustomBaseUrl != null){ + basePath = localCustomBaseUrl; + } else if ( localBasePaths.length > 0 ) { + basePath = localBasePaths[localHostIndex]; + } else { + basePath = null; + } + + Object localVarPostBody = null; + + // create path and map variables + String localVarPath = "/fake/null-request-body"; + + List localVarQueryParams = new ArrayList(); + List localVarCollectionQueryParams = new ArrayList(); + Map localVarHeaderParams = new HashMap(); + Map localVarCookieParams = new HashMap(); + Map localVarFormParams = new HashMap(); + + if (acceptLanguage != null) { + localVarHeaderParams.put("Accept-Language", localVarApiClient.parameterToString(acceptLanguage)); + } + + final String[] localVarAccepts = { + }; + final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts); + if (localVarAccept != null) { + localVarHeaderParams.put("Accept", localVarAccept); + } + + final String[] localVarContentTypes = { + "text/plain" + }; + final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes); + if (localVarContentType != null) { + localVarHeaderParams.put("Content-Type", localVarContentType); + } + + String[] localVarAuthNames = new String[] { }; + return localVarApiClient.buildCall(basePath, localVarPath, "GET", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAuthNames, _callback); + } + + @SuppressWarnings("rawtypes") + private okhttp3.Call nullRequestBodyValidateBeforeCall(String acceptLanguage, final ApiCallback _callback) throws ApiException { + return nullRequestBodyCall(acceptLanguage, _callback); + + } + + /** + * null request body + * + * @param acceptLanguage (optional) + * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body + * @http.response.details + + + +
Status Code Description Response Headers
200 -
+ */ + public void nullRequestBody(String acceptLanguage) throws ApiException { + nullRequestBodyWithHttpInfo(acceptLanguage); + } + + /** + * null request body + * + * @param acceptLanguage (optional) + * @return ApiResponse<Void> + * @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body + * @http.response.details + + + +
Status Code Description Response Headers
200 -
+ */ + public ApiResponse nullRequestBodyWithHttpInfo(String acceptLanguage) throws ApiException { + okhttp3.Call localVarCall = nullRequestBodyValidateBeforeCall(acceptLanguage, null); + return localVarApiClient.execute(localVarCall); + } + + /** + * null request body (asynchronously) + * + * @param acceptLanguage (optional) + * @param _callback The callback to be executed when the API call finishes + * @return The request call + * @throws ApiException If fail to process the API call, e.g. serializing the request body object + * @http.response.details + + + +
Status Code Description Response Headers
200 -
+ */ + public okhttp3.Call nullRequestBodyAsync(String acceptLanguage, final ApiCallback _callback) throws ApiException { + + okhttp3.Call localVarCall = nullRequestBodyValidateBeforeCall(acceptLanguage, _callback); + localVarApiClient.executeAsync(localVarCall, _callback); + return localVarCall; + } }