Skip to content

Commit d88023f

Browse files
authored
fix ref to request bodies in form content type (#18216)
1 parent 8acf9b7 commit d88023f

File tree

6 files changed

+295
-3
lines changed

6 files changed

+295
-3
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4732,7 +4732,7 @@ public CodegenOperation fromOperation(String path,
47324732
List<CodegenParameter> notNullableParams = new ArrayList<>();
47334733

47344734
CodegenParameter bodyParam = null;
4735-
RequestBody requestBody = operation.getRequestBody();
4735+
RequestBody requestBody = ModelUtils.getReferencedRequestBody(this.openAPI, operation.getRequestBody());
47364736
if (requestBody != null) {
47374737
String contentType = getContentType(requestBody);
47384738
if (contentType != null) {
@@ -4756,8 +4756,6 @@ public CodegenOperation fromOperation(String path,
47564756
}
47574757
} else {
47584758
// process body parameter
4759-
requestBody = ModelUtils.getReferencedRequestBody(this.openAPI, requestBody);
4760-
47614759
String bodyParameterName = "";
47624760
if (op.vendorExtensions != null && op.vendorExtensions.containsKey("x-codegen-request-body-name")) {
47634761
bodyParameterName = (String) op.vendorExtensions.get("x-codegen-request-body-name");

modules/openapi-generator/src/test/resources/3_0/java/petstore-with-fake-endpoints-models-for-testing-okhttp-gson.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,35 @@ paths:
12771277
responses:
12781278
'200':
12791279
description: ''
1280+
'/fake/pet/{petId}/uploadImage':
1281+
post:
1282+
tags:
1283+
- fake
1284+
summary: fake uploads an image with ref request bodies
1285+
description: ''
1286+
operationId: fake-upload-ref-request-bodies
1287+
parameters:
1288+
- name: petId
1289+
in: path
1290+
description: ID of pet to update
1291+
required: true
1292+
schema:
1293+
type: integer
1294+
format: int64
1295+
responses:
1296+
'200':
1297+
description: successful operation
1298+
content:
1299+
application/json:
1300+
schema:
1301+
$ref: '#/components/schemas/ApiResponse'
1302+
security:
1303+
- petstore_auth:
1304+
- 'write:pets'
1305+
- 'read:pets'
1306+
requestBody:
1307+
$ref: '#/components/requestBodies/upload_body'
1308+
12801309
/values:
12811310
get:
12821311
tags:
@@ -1336,6 +1365,19 @@ servers:
13361365
description: The local server without variables
13371366
components:
13381367
requestBodies:
1368+
upload_body:
1369+
content:
1370+
multipart/form-data:
1371+
schema:
1372+
type: object
1373+
properties:
1374+
additionalMetadata:
1375+
description: Additional data to pass to server
1376+
type: string
1377+
file:
1378+
description: file to upload
1379+
type: string
1380+
format: binary
13391381
UserArray:
13401382
content:
13411383
application/json:

samples/client/petstore/java/okhttp-gson/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ Class | Method | HTTP request | Description
123123
*FakeApi* | [**fakeOuterCompositeSerialize**](docs/FakeApi.md#fakeOuterCompositeSerialize) | **POST** /fake/outer/composite |
124124
*FakeApi* | [**fakeOuterNumberSerialize**](docs/FakeApi.md#fakeOuterNumberSerialize) | **POST** /fake/outer/number |
125125
*FakeApi* | [**fakeOuterStringSerialize**](docs/FakeApi.md#fakeOuterStringSerialize) | **POST** /fake/outer/string |
126+
*FakeApi* | [**fakeUploadRefRequestBodies**](docs/FakeApi.md#fakeUploadRefRequestBodies) | **POST** /fake/pet/{petId}/uploadImage | fake uploads an image with ref request bodies
126127
*FakeApi* | [**getFakeArrayofenums**](docs/FakeApi.md#getFakeArrayofenums) | **GET** /fake/array-of-enums | Array of Enums
127128
*FakeApi* | [**getFakeHealth**](docs/FakeApi.md#getFakeHealth) | **GET** /fake/health | Health check endpoint
128129
*FakeApi* | [**getParameterNameMapping**](docs/FakeApi.md#getParameterNameMapping) | **GET** /fake/parameter-name-mapping | parameter name mapping test

samples/client/petstore/java/okhttp-gson/api/openapi.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,6 +1372,39 @@ paths:
13721372
- another_fake
13731373
x-accepts:
13741374
- application/json
1375+
/fake/pet/{petId}/uploadImage:
1376+
post:
1377+
description: ""
1378+
operationId: fake-upload-ref-request-bodies
1379+
parameters:
1380+
- description: ID of pet to update
1381+
explode: false
1382+
in: path
1383+
name: petId
1384+
required: true
1385+
schema:
1386+
format: int64
1387+
type: integer
1388+
style: simple
1389+
requestBody:
1390+
$ref: '#/components/requestBodies/upload_body'
1391+
responses:
1392+
"200":
1393+
content:
1394+
application/json:
1395+
schema:
1396+
$ref: '#/components/schemas/ApiResponse'
1397+
description: successful operation
1398+
security:
1399+
- petstore_auth:
1400+
- write:pets
1401+
- read:pets
1402+
summary: fake uploads an image with ref request bodies
1403+
tags:
1404+
- fake
1405+
x-content-type: multipart/form-data
1406+
x-accepts:
1407+
- application/json
13751408
/values:
13761409
get:
13771410
description: ""
@@ -1410,6 +1443,11 @@ paths:
14101443
- application/json
14111444
components:
14121445
requestBodies:
1446+
upload_body:
1447+
content:
1448+
multipart/form-data:
1449+
schema:
1450+
$ref: '#/components/schemas/uploadFile_request'
14131451
UserArray:
14141452
content:
14151453
application/json:

samples/client/petstore/java/okhttp-gson/docs/FakeApi.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ All URIs are relative to *http://petstore.swagger.io:80/v2*
88
| [**fakeOuterCompositeSerialize**](FakeApi.md#fakeOuterCompositeSerialize) | **POST** /fake/outer/composite | |
99
| [**fakeOuterNumberSerialize**](FakeApi.md#fakeOuterNumberSerialize) | **POST** /fake/outer/number | |
1010
| [**fakeOuterStringSerialize**](FakeApi.md#fakeOuterStringSerialize) | **POST** /fake/outer/string | |
11+
| [**fakeUploadRefRequestBodies**](FakeApi.md#fakeUploadRefRequestBodies) | **POST** /fake/pet/{petId}/uploadImage | fake uploads an image with ref request bodies |
1112
| [**getFakeArrayofenums**](FakeApi.md#getFakeArrayofenums) | **GET** /fake/array-of-enums | Array of Enums |
1213
| [**getFakeHealth**](FakeApi.md#getFakeHealth) | **GET** /fake/health | Health check endpoint |
1314
| [**getParameterNameMapping**](FakeApi.md#getParameterNameMapping) | **GET** /fake/parameter-name-mapping | parameter name mapping test |
@@ -273,6 +274,77 @@ No authorization required
273274
|-------------|-------------|------------------|
274275
| **200** | Output string | - |
275276

277+
<a id="fakeUploadRefRequestBodies"></a>
278+
# **fakeUploadRefRequestBodies**
279+
> ModelApiResponse fakeUploadRefRequestBodies(petId, additionalMetadata, _file)
280+
281+
fake uploads an image with ref request bodies
282+
283+
284+
285+
### Example
286+
```java
287+
// Import classes:
288+
import org.openapitools.client.ApiClient;
289+
import org.openapitools.client.ApiException;
290+
import org.openapitools.client.Configuration;
291+
import org.openapitools.client.auth.*;
292+
import org.openapitools.client.models.*;
293+
import org.openapitools.client.api.FakeApi;
294+
295+
public class Example {
296+
public static void main(String[] args) {
297+
ApiClient defaultClient = Configuration.getDefaultApiClient();
298+
defaultClient.setBasePath("http://petstore.swagger.io:80/v2");
299+
300+
// Configure OAuth2 access token for authorization: petstore_auth
301+
OAuth petstore_auth = (OAuth) defaultClient.getAuthentication("petstore_auth");
302+
petstore_auth.setAccessToken("YOUR ACCESS TOKEN");
303+
304+
FakeApi apiInstance = new FakeApi(defaultClient);
305+
Long petId = 56L; // Long | ID of pet to update
306+
String additionalMetadata = "additionalMetadata_example"; // String | Additional data to pass to server
307+
File _file = new File("/path/to/file"); // File | file to upload
308+
try {
309+
ModelApiResponse result = apiInstance.fakeUploadRefRequestBodies(petId, additionalMetadata, _file);
310+
System.out.println(result);
311+
} catch (ApiException e) {
312+
System.err.println("Exception when calling FakeApi#fakeUploadRefRequestBodies");
313+
System.err.println("Status code: " + e.getCode());
314+
System.err.println("Reason: " + e.getResponseBody());
315+
System.err.println("Response headers: " + e.getResponseHeaders());
316+
e.printStackTrace();
317+
}
318+
}
319+
}
320+
```
321+
322+
### Parameters
323+
324+
| Name | Type | Description | Notes |
325+
|------------- | ------------- | ------------- | -------------|
326+
| **petId** | **Long**| ID of pet to update | |
327+
| **additionalMetadata** | **String**| Additional data to pass to server | [optional] |
328+
| **_file** | **File**| file to upload | [optional] |
329+
330+
### Return type
331+
332+
[**ModelApiResponse**](ModelApiResponse.md)
333+
334+
### Authorization
335+
336+
[petstore_auth](../README.md#petstore_auth)
337+
338+
### HTTP request headers
339+
340+
- **Content-Type**: multipart/form-data
341+
- **Accept**: application/json
342+
343+
### HTTP response details
344+
| Status code | Description | Response headers |
345+
|-------------|-------------|------------------|
346+
| **200** | successful operation | - |
347+
276348
<a id="getFakeArrayofenums"></a>
277349
# **getFakeArrayofenums**
278350
> List&lt;OuterEnum&gt; getFakeArrayofenums()

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/api/FakeApi.java

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.openapitools.client.model.FileSchemaTestClass;
3434
import org.openapitools.client.model.HealthCheckResult;
3535
import java.time.LocalDate;
36+
import org.openapitools.client.model.ModelApiResponse;
3637
import java.time.OffsetDateTime;
3738
import org.openapitools.client.model.OuterComposite;
3839
import org.openapitools.client.model.OuterEnum;
@@ -554,6 +555,146 @@ public okhttp3.Call fakeOuterStringSerializeAsync(String body, final ApiCallback
554555
localVarApiClient.executeAsync(localVarCall, localVarReturnType, _callback);
555556
return localVarCall;
556557
}
558+
/**
559+
* Build call for fakeUploadRefRequestBodies
560+
* @param petId ID of pet to update (required)
561+
* @param additionalMetadata Additional data to pass to server (optional)
562+
* @param _file file to upload (optional)
563+
* @param _callback Callback for upload/download progress
564+
* @return Call to execute
565+
* @throws ApiException If fail to serialize the request body object
566+
* @http.response.details
567+
<table summary="Response Details" border="1">
568+
<tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
569+
<tr><td> 200 </td><td> successful operation </td><td> - </td></tr>
570+
</table>
571+
*/
572+
public okhttp3.Call fakeUploadRefRequestBodiesCall(Long petId, String additionalMetadata, File _file, final ApiCallback _callback) throws ApiException {
573+
String basePath = null;
574+
// Operation Servers
575+
String[] localBasePaths = new String[] { };
576+
577+
// Determine Base Path to Use
578+
if (localCustomBaseUrl != null){
579+
basePath = localCustomBaseUrl;
580+
} else if ( localBasePaths.length > 0 ) {
581+
basePath = localBasePaths[localHostIndex];
582+
} else {
583+
basePath = null;
584+
}
585+
586+
Object localVarPostBody = null;
587+
588+
// create path and map variables
589+
String localVarPath = "/fake/pet/{petId}/uploadImage"
590+
.replace("{" + "petId" + "}", localVarApiClient.escapeString(petId.toString()));
591+
592+
List<Pair> localVarQueryParams = new ArrayList<Pair>();
593+
List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
594+
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
595+
Map<String, String> localVarCookieParams = new HashMap<String, String>();
596+
Map<String, Object> localVarFormParams = new HashMap<String, Object>();
597+
598+
if (additionalMetadata != null) {
599+
localVarFormParams.put("additionalMetadata", additionalMetadata);
600+
}
601+
602+
if (_file != null) {
603+
localVarFormParams.put("file", _file);
604+
}
605+
606+
final String[] localVarAccepts = {
607+
"application/json"
608+
};
609+
final String localVarAccept = localVarApiClient.selectHeaderAccept(localVarAccepts);
610+
if (localVarAccept != null) {
611+
localVarHeaderParams.put("Accept", localVarAccept);
612+
}
613+
614+
final String[] localVarContentTypes = {
615+
"multipart/form-data"
616+
};
617+
final String localVarContentType = localVarApiClient.selectHeaderContentType(localVarContentTypes);
618+
if (localVarContentType != null) {
619+
localVarHeaderParams.put("Content-Type", localVarContentType);
620+
}
621+
622+
String[] localVarAuthNames = new String[] { "petstore_auth" };
623+
return localVarApiClient.buildCall(basePath, localVarPath, "POST", localVarQueryParams, localVarCollectionQueryParams, localVarPostBody, localVarHeaderParams, localVarCookieParams, localVarFormParams, localVarAuthNames, _callback);
624+
}
625+
626+
@SuppressWarnings("rawtypes")
627+
private okhttp3.Call fakeUploadRefRequestBodiesValidateBeforeCall(Long petId, String additionalMetadata, File _file, final ApiCallback _callback) throws ApiException {
628+
// verify the required parameter 'petId' is set
629+
if (petId == null) {
630+
throw new ApiException("Missing the required parameter 'petId' when calling fakeUploadRefRequestBodies(Async)");
631+
}
632+
633+
return fakeUploadRefRequestBodiesCall(petId, additionalMetadata, _file, _callback);
634+
635+
}
636+
637+
/**
638+
* fake uploads an image with ref request bodies
639+
*
640+
* @param petId ID of pet to update (required)
641+
* @param additionalMetadata Additional data to pass to server (optional)
642+
* @param _file file to upload (optional)
643+
* @return ModelApiResponse
644+
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
645+
* @http.response.details
646+
<table summary="Response Details" border="1">
647+
<tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
648+
<tr><td> 200 </td><td> successful operation </td><td> - </td></tr>
649+
</table>
650+
*/
651+
public ModelApiResponse fakeUploadRefRequestBodies(Long petId, String additionalMetadata, File _file) throws ApiException {
652+
ApiResponse<ModelApiResponse> localVarResp = fakeUploadRefRequestBodiesWithHttpInfo(petId, additionalMetadata, _file);
653+
return localVarResp.getData();
654+
}
655+
656+
/**
657+
* fake uploads an image with ref request bodies
658+
*
659+
* @param petId ID of pet to update (required)
660+
* @param additionalMetadata Additional data to pass to server (optional)
661+
* @param _file file to upload (optional)
662+
* @return ApiResponse&lt;ModelApiResponse&gt;
663+
* @throws ApiException If fail to call the API, e.g. server error or cannot deserialize the response body
664+
* @http.response.details
665+
<table summary="Response Details" border="1">
666+
<tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
667+
<tr><td> 200 </td><td> successful operation </td><td> - </td></tr>
668+
</table>
669+
*/
670+
public ApiResponse<ModelApiResponse> fakeUploadRefRequestBodiesWithHttpInfo(Long petId, String additionalMetadata, File _file) throws ApiException {
671+
okhttp3.Call localVarCall = fakeUploadRefRequestBodiesValidateBeforeCall(petId, additionalMetadata, _file, null);
672+
Type localVarReturnType = new TypeToken<ModelApiResponse>(){}.getType();
673+
return localVarApiClient.execute(localVarCall, localVarReturnType);
674+
}
675+
676+
/**
677+
* fake uploads an image with ref request bodies (asynchronously)
678+
*
679+
* @param petId ID of pet to update (required)
680+
* @param additionalMetadata Additional data to pass to server (optional)
681+
* @param _file file to upload (optional)
682+
* @param _callback The callback to be executed when the API call finishes
683+
* @return The request call
684+
* @throws ApiException If fail to process the API call, e.g. serializing the request body object
685+
* @http.response.details
686+
<table summary="Response Details" border="1">
687+
<tr><td> Status Code </td><td> Description </td><td> Response Headers </td></tr>
688+
<tr><td> 200 </td><td> successful operation </td><td> - </td></tr>
689+
</table>
690+
*/
691+
public okhttp3.Call fakeUploadRefRequestBodiesAsync(Long petId, String additionalMetadata, File _file, final ApiCallback<ModelApiResponse> _callback) throws ApiException {
692+
693+
okhttp3.Call localVarCall = fakeUploadRefRequestBodiesValidateBeforeCall(petId, additionalMetadata, _file, _callback);
694+
Type localVarReturnType = new TypeToken<ModelApiResponse>(){}.getType();
695+
localVarApiClient.executeAsync(localVarCall, localVarReturnType, _callback);
696+
return localVarCall;
697+
}
557698
/**
558699
* Build call for getFakeArrayofenums
559700
* @param _callback Callback for upload/download progress

0 commit comments

Comments
 (0)