Skip to content

Commit b59d535

Browse files
authored
[python-nextgen] Fix binary response (#15076)
* fix binary response in python nextgen client * update samples
1 parent 05fa560 commit b59d535

36 files changed

Lines changed: 906 additions & 75 deletions

File tree

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@ public PythonNextgenClientCodegen() {
113113
typeMapping.put("array", "List");
114114
typeMapping.put("set", "List");
115115
typeMapping.put("map", "Dict");
116-
typeMapping.put("file", "str");
117116
typeMapping.put("decimal", "decimal.Decimal");
117+
typeMapping.put("file", "bytearray");
118+
typeMapping.put("binary", "bytearray");
119+
typeMapping.put("ByteArray", "bytearray");
118120

119121
languageSpecificPrimitives.remove("file");
120122
languageSpecificPrimitives.add("decimal.Decimal");

modules/openapi-generator/src/main/resources/python-nextgen/api_client.mustache

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,9 @@ class ApiClient(object):
241241

242242
response_type = response_types_map.get(str(response_data.status), None)
243243

244-
if response_type not in ["file", "bytes"]:
244+
if response_type == "bytearray":
245+
response_data.data = response_data.data
246+
else:
245247
match = None
246248
content_type = response_data.getheader('content-type')
247249
if content_type is not None:
@@ -250,8 +252,9 @@ class ApiClient(object):
250252
response_data.data = response_data.data.decode(encoding)
251253

252254
# deserialize response data
253-
254-
if response_type:
255+
if response_type == "bytearray":
256+
return_data = response_data.data
257+
elif response_type:
255258
return_data = self.deserialize(response_data, response_type)
256259
else:
257260
return_data = None

modules/openapi-generator/src/test/resources/3_0/echo_api.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,21 @@ paths:
330330
text/plain:
331331
schema:
332332
type: string
333+
/binary/gif:
334+
post:
335+
tags:
336+
- body
337+
summary: Test binary (gif) response body
338+
description: Test binary (gif) response body
339+
operationId: test/binary/gif
340+
responses:
341+
'200':
342+
description: Successful operation
343+
content:
344+
image/gif:
345+
schema:
346+
type: string
347+
format: binary
333348

334349
components:
335350
requestBodies:

samples/client/echo_api/java/apache-httpclient/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,11 @@ public class BodyApiExample {
8484
defaultClient.setBasePath("http://localhost:3000");
8585

8686
BodyApi apiInstance = new BodyApi(defaultClient);
87-
Pet pet = new Pet(); // Pet | Pet object that needs to be added to the store
8887
try {
89-
Pet result = apiInstance.testEchoBodyPet(pet);
88+
File result = apiInstance.testBinaryGif();
9089
System.out.println(result);
9190
} catch (ApiException e) {
92-
System.err.println("Exception when calling BodyApi#testEchoBodyPet");
91+
System.err.println("Exception when calling BodyApi#testBinaryGif");
9392
System.err.println("Status code: " + e.getCode());
9493
System.err.println("Reason: " + e.getResponseBody());
9594
System.err.println("Response headers: " + e.getResponseHeaders());
@@ -106,6 +105,7 @@ All URIs are relative to *http://localhost:3000*
106105

107106
Class | Method | HTTP request | Description
108107
------------ | ------------- | ------------- | -------------
108+
*BodyApi* | [**testBinaryGif**](docs/BodyApi.md#testBinaryGif) | **POST** /binary/gif | Test binary (gif) response body
109109
*BodyApi* | [**testEchoBodyPet**](docs/BodyApi.md#testEchoBodyPet) | **POST** /echo/body/Pet | Test body parameter(s)
110110
*BodyApi* | [**testEchoBodyPetResponseString**](docs/BodyApi.md#testEchoBodyPetResponseString) | **POST** /echo/body/Pet/response_string | Test empty response body
111111
*FormApi* | [**testFormIntegerBooleanString**](docs/FormApi.md#testFormIntegerBooleanString) | **POST** /form/integer/boolean/string | Test form parameter(s)

samples/client/echo_api/java/apache-httpclient/api/openapi.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,22 @@ paths:
326326
- body
327327
x-content-type: application/json
328328
x-accepts: text/plain
329+
/binary/gif:
330+
post:
331+
description: Test binary (gif) response body
332+
operationId: test/binary/gif
333+
responses:
334+
"200":
335+
content:
336+
image/gif:
337+
schema:
338+
format: binary
339+
type: string
340+
description: Successful operation
341+
summary: Test binary (gif) response body
342+
tags:
343+
- body
344+
x-accepts: image/gif
329345
components:
330346
requestBodies:
331347
Pet:

samples/client/echo_api/java/apache-httpclient/docs/BodyApi.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,74 @@ All URIs are relative to *http://localhost:3000*
44

55
| Method | HTTP request | Description |
66
|------------- | ------------- | -------------|
7+
| [**testBinaryGif**](BodyApi.md#testBinaryGif) | **POST** /binary/gif | Test binary (gif) response body |
78
| [**testEchoBodyPet**](BodyApi.md#testEchoBodyPet) | **POST** /echo/body/Pet | Test body parameter(s) |
89
| [**testEchoBodyPetResponseString**](BodyApi.md#testEchoBodyPetResponseString) | **POST** /echo/body/Pet/response_string | Test empty response body |
910

1011

1112

13+
## testBinaryGif
14+
15+
> File testBinaryGif()
16+
17+
Test binary (gif) response body
18+
19+
Test binary (gif) response body
20+
21+
### Example
22+
23+
```java
24+
// Import classes:
25+
import org.openapitools.client.ApiClient;
26+
import org.openapitools.client.ApiException;
27+
import org.openapitools.client.Configuration;
28+
import org.openapitools.client.models.*;
29+
import org.openapitools.client.api.BodyApi;
30+
31+
public class Example {
32+
public static void main(String[] args) {
33+
ApiClient defaultClient = Configuration.getDefaultApiClient();
34+
defaultClient.setBasePath("http://localhost:3000");
35+
36+
BodyApi apiInstance = new BodyApi(defaultClient);
37+
try {
38+
File result = apiInstance.testBinaryGif();
39+
System.out.println(result);
40+
} catch (ApiException e) {
41+
System.err.println("Exception when calling BodyApi#testBinaryGif");
42+
System.err.println("Status code: " + e.getCode());
43+
System.err.println("Reason: " + e.getResponseBody());
44+
System.err.println("Response headers: " + e.getResponseHeaders());
45+
e.printStackTrace();
46+
}
47+
}
48+
}
49+
```
50+
51+
### Parameters
52+
53+
This endpoint does not need any parameter.
54+
55+
### Return type
56+
57+
[**File**](File.md)
58+
59+
### Authorization
60+
61+
No authorization required
62+
63+
### HTTP request headers
64+
65+
- **Content-Type**: Not defined
66+
- **Accept**: image/gif
67+
68+
69+
### HTTP response details
70+
| Status code | Description | Response headers |
71+
|-------------|-------------|------------------|
72+
| **200** | Successful operation | - |
73+
74+
1275
## testEchoBodyPet
1376

1477
> Pet testEchoBodyPet(pet)

samples/client/echo_api/java/apache-httpclient/src/main/java/org/openapitools/client/api/BodyApi.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.openapitools.client.model.*;
2121
import org.openapitools.client.Pair;
2222

23+
import java.io.File;
2324
import org.openapitools.client.model.Pet;
2425

2526

@@ -52,6 +53,73 @@ public void setApiClient(ApiClient apiClient) {
5253
this.apiClient = apiClient;
5354
}
5455

56+
/**
57+
* Test binary (gif) response body
58+
* Test binary (gif) response body
59+
* @return File
60+
* @throws ApiException if fails to make API call
61+
*/
62+
public File testBinaryGif() throws ApiException {
63+
return this.testBinaryGif(Collections.emptyMap());
64+
}
65+
66+
67+
/**
68+
* Test binary (gif) response body
69+
* Test binary (gif) response body
70+
* @param additionalHeaders additionalHeaders for this call
71+
* @return File
72+
* @throws ApiException if fails to make API call
73+
*/
74+
public File testBinaryGif(Map<String, String> additionalHeaders) throws ApiException {
75+
Object localVarPostBody = null;
76+
77+
// create path and map variables
78+
String localVarPath = "/binary/gif";
79+
80+
StringJoiner localVarQueryStringJoiner = new StringJoiner("&");
81+
String localVarQueryParameterBaseName;
82+
List<Pair> localVarQueryParams = new ArrayList<Pair>();
83+
List<Pair> localVarCollectionQueryParams = new ArrayList<Pair>();
84+
Map<String, String> localVarHeaderParams = new HashMap<String, String>();
85+
Map<String, String> localVarCookieParams = new HashMap<String, String>();
86+
Map<String, Object> localVarFormParams = new HashMap<String, Object>();
87+
88+
89+
localVarHeaderParams.putAll(additionalHeaders);
90+
91+
92+
93+
final String[] localVarAccepts = {
94+
"image/gif"
95+
};
96+
final String localVarAccept = apiClient.selectHeaderAccept(localVarAccepts);
97+
98+
final String[] localVarContentTypes = {
99+
100+
};
101+
final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
102+
103+
String[] localVarAuthNames = new String[] { };
104+
105+
TypeReference<File> localVarReturnType = new TypeReference<File>() {};
106+
return apiClient.invokeAPI(
107+
localVarPath,
108+
"POST",
109+
localVarQueryParams,
110+
localVarCollectionQueryParams,
111+
localVarQueryStringJoiner.toString(),
112+
localVarPostBody,
113+
localVarHeaderParams,
114+
localVarCookieParams,
115+
localVarFormParams,
116+
localVarAccept,
117+
localVarContentType,
118+
localVarAuthNames,
119+
localVarReturnType
120+
);
121+
}
122+
55123
/**
56124
* Test body parameter(s)
57125
* Test body parameter(s)

samples/client/echo_api/java/feign-gson/api/openapi.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,22 @@ paths:
326326
- body
327327
x-content-type: application/json
328328
x-accepts: text/plain
329+
/binary/gif:
330+
post:
331+
description: Test binary (gif) response body
332+
operationId: test/binary/gif
333+
responses:
334+
"200":
335+
content:
336+
image/gif:
337+
schema:
338+
format: binary
339+
type: string
340+
description: Successful operation
341+
summary: Test binary (gif) response body
342+
tags:
343+
- body
344+
x-accepts: image/gif
329345
components:
330346
requestBodies:
331347
Pet:

samples/client/echo_api/java/feign-gson/src/main/java/org/openapitools/client/api/BodyApi.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.openapitools.client.EncodingUtils;
55
import org.openapitools.client.model.ApiResponse;
66

7+
import java.io.File;
78
import org.openapitools.client.model.Pet;
89

910
import java.util.ArrayList;
@@ -16,6 +17,31 @@
1617
public interface BodyApi extends ApiClient.Api {
1718

1819

20+
/**
21+
* Test binary (gif) response body
22+
* Test binary (gif) response body
23+
* @return File
24+
*/
25+
@RequestLine("POST /binary/gif")
26+
@Headers({
27+
"Accept: image/gif",
28+
})
29+
File testBinaryGif();
30+
31+
/**
32+
* Test binary (gif) response body
33+
* Similar to <code>testBinaryGif</code> but it also returns the http response headers .
34+
* Test binary (gif) response body
35+
* @return A ApiResponse that wraps the response boyd and the http headers.
36+
*/
37+
@RequestLine("POST /binary/gif")
38+
@Headers({
39+
"Accept: image/gif",
40+
})
41+
ApiResponse<File> testBinaryGifWithHttpInfo();
42+
43+
44+
1945
/**
2046
* Test body parameter(s)
2147
* Test body parameter(s)

samples/client/echo_api/java/native/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,11 @@ public class BodyApiExample {
8383
// Configure clients using the `defaultClient` object, such as
8484
// overriding the host and port, timeout, etc.
8585
BodyApi apiInstance = new BodyApi(defaultClient);
86-
Pet pet = new Pet(); // Pet | Pet object that needs to be added to the store
8786
try {
88-
Pet result = apiInstance.testEchoBodyPet(pet);
87+
File result = apiInstance.testBinaryGif();
8988
System.out.println(result);
9089
} catch (ApiException e) {
91-
System.err.println("Exception when calling BodyApi#testEchoBodyPet");
90+
System.err.println("Exception when calling BodyApi#testBinaryGif");
9291
System.err.println("Status code: " + e.getCode());
9392
System.err.println("Reason: " + e.getResponseBody());
9493
System.err.println("Response headers: " + e.getResponseHeaders());
@@ -105,6 +104,8 @@ All URIs are relative to *http://localhost:3000*
105104

106105
Class | Method | HTTP request | Description
107106
------------ | ------------- | ------------- | -------------
107+
*BodyApi* | [**testBinaryGif**](docs/BodyApi.md#testBinaryGif) | **POST** /binary/gif | Test binary (gif) response body
108+
*BodyApi* | [**testBinaryGifWithHttpInfo**](docs/BodyApi.md#testBinaryGifWithHttpInfo) | **POST** /binary/gif | Test binary (gif) response body
108109
*BodyApi* | [**testEchoBodyPet**](docs/BodyApi.md#testEchoBodyPet) | **POST** /echo/body/Pet | Test body parameter(s)
109110
*BodyApi* | [**testEchoBodyPetWithHttpInfo**](docs/BodyApi.md#testEchoBodyPetWithHttpInfo) | **POST** /echo/body/Pet | Test body parameter(s)
110111
*BodyApi* | [**testEchoBodyPetResponseString**](docs/BodyApi.md#testEchoBodyPetResponseString) | **POST** /echo/body/Pet/response_string | Test empty response body

0 commit comments

Comments
 (0)