Skip to content

Commit 5def63e

Browse files
joekakota65535
authored andcommitted
[Python] deserialize enum json response (fix OpenAPITools#17789) (OpenAPITools#17791)
* deserialize enum json response (python) * adapt python samples: adding enum deserialization * add echo test for enum json response deserialization (python) * update samples
1 parent 3f81998 commit 5def63e

File tree

65 files changed

+3572
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+3572
-0
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import datetime
66
from dateutil.parser import parse
7+
from enum import Enum
78
import json
89
import mimetypes
910
import os
@@ -437,6 +438,8 @@ class ApiClient:
437438
return self.__deserialize_date(data)
438439
elif klass == datetime.datetime:
439440
return self.__deserialize_datetime(data)
441+
elif issubclass(klass, Enum):
442+
return self.__deserialize_enum(data, klass)
440443
else:
441444
return self.__deserialize_model(data, klass)
442445

@@ -743,6 +746,24 @@ class ApiClient:
743746
)
744747
)
745748

749+
def __deserialize_enum(self, data, klass):
750+
"""Deserializes primitive type to enum.
751+
752+
:param data: primitive type.
753+
:param klass: class literal.
754+
:return: enum value.
755+
"""
756+
try:
757+
return klass(data)
758+
except ValueError:
759+
raise rest.ApiException(
760+
status=0,
761+
reason=(
762+
"Failed to parse `{0}` as `{1}`"
763+
.format(data, klass)
764+
)
765+
)
766+
746767
def __deserialize_model(self, data, klass):
747768
"""Deserializes list or dict to model.
748769

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

+20
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,26 @@ paths:
544544
text/plain:
545545
schema:
546546
type: string
547+
/echo/body/string_enum:
548+
post:
549+
tags:
550+
- body
551+
summary: Test string enum response body
552+
description: Test string enum response body
553+
operationId: test/echo/body/string_enum
554+
requestBody:
555+
content:
556+
application/json:
557+
schema:
558+
$ref: '#/components/schemas/StringEnumRef'
559+
description: String enum
560+
responses:
561+
'200':
562+
description: Successful operation
563+
content:
564+
application/json:
565+
schema:
566+
$ref: '#/components/schemas/StringEnumRef'
547567
/binary/gif:
548568
post:
549569
tags:

samples/client/echo_api/csharp-restsharp/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ Class | Method | HTTP request | Description
126126
*BodyApi* | [**TestEchoBodyFreeFormObjectResponseString**](docs/BodyApi.md#testechobodyfreeformobjectresponsestring) | **POST** /echo/body/FreeFormObject/response_string | Test free form object
127127
*BodyApi* | [**TestEchoBodyPet**](docs/BodyApi.md#testechobodypet) | **POST** /echo/body/Pet | Test body parameter(s)
128128
*BodyApi* | [**TestEchoBodyPetResponseString**](docs/BodyApi.md#testechobodypetresponsestring) | **POST** /echo/body/Pet/response_string | Test empty response body
129+
*BodyApi* | [**TestEchoBodyStringEnum**](docs/BodyApi.md#testechobodystringenum) | **POST** /echo/body/string_enum | Test string enum response body
129130
*BodyApi* | [**TestEchoBodyTagResponseString**](docs/BodyApi.md#testechobodytagresponsestring) | **POST** /echo/body/Tag/response_string | Test empty json (request body)
130131
*FormApi* | [**TestFormIntegerBooleanString**](docs/FormApi.md#testformintegerbooleanstring) | **POST** /form/integer/boolean/string | Test form parameter(s)
131132
*FormApi* | [**TestFormOneof**](docs/FormApi.md#testformoneof) | **POST** /form/oneof | Test form parameter(s) for oneOf schema

samples/client/echo_api/csharp-restsharp/api/openapi.yaml

+20
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,26 @@ paths:
519519
summary: Test free form object
520520
tags:
521521
- body
522+
/echo/body/string_enum:
523+
post:
524+
description: Test string enum response body
525+
operationId: test/echo/body/string_enum
526+
requestBody:
527+
content:
528+
application/json:
529+
schema:
530+
$ref: '#/components/schemas/StringEnumRef'
531+
description: String enum
532+
responses:
533+
"200":
534+
content:
535+
application/json:
536+
schema:
537+
$ref: '#/components/schemas/StringEnumRef'
538+
description: Successful operation
539+
summary: Test string enum response body
540+
tags:
541+
- body
522542
/binary/gif:
523543
post:
524544
description: Test binary (gif) response body

samples/client/echo_api/csharp-restsharp/docs/BodyApi.md

+92
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ All URIs are relative to *http://localhost:3000*
1212
| [**TestEchoBodyFreeFormObjectResponseString**](BodyApi.md#testechobodyfreeformobjectresponsestring) | **POST** /echo/body/FreeFormObject/response_string | Test free form object |
1313
| [**TestEchoBodyPet**](BodyApi.md#testechobodypet) | **POST** /echo/body/Pet | Test body parameter(s) |
1414
| [**TestEchoBodyPetResponseString**](BodyApi.md#testechobodypetresponsestring) | **POST** /echo/body/Pet/response_string | Test empty response body |
15+
| [**TestEchoBodyStringEnum**](BodyApi.md#testechobodystringenum) | **POST** /echo/body/string_enum | Test string enum response body |
1516
| [**TestEchoBodyTagResponseString**](BodyApi.md#testechobodytagresponsestring) | **POST** /echo/body/Tag/response_string | Test empty json (request body) |
1617

1718
<a id="testbinarygif"></a>
@@ -730,6 +731,97 @@ No authorization required
730731
- **Accept**: text/plain
731732

732733

734+
### HTTP response details
735+
| Status code | Description | Response headers |
736+
|-------------|-------------|------------------|
737+
| **200** | Successful operation | - |
738+
739+
[[Back to top]](#) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to Model list]](../README.md#documentation-for-models) [[Back to README]](../README.md)
740+
741+
<a id="testechobodystringenum"></a>
742+
# **TestEchoBodyStringEnum**
743+
> StringEnumRef TestEchoBodyStringEnum (string? body = null)
744+
745+
Test string enum response body
746+
747+
Test string enum response body
748+
749+
### Example
750+
```csharp
751+
using System.Collections.Generic;
752+
using System.Diagnostics;
753+
using Org.OpenAPITools.Api;
754+
using Org.OpenAPITools.Client;
755+
using Org.OpenAPITools.Model;
756+
757+
namespace Example
758+
{
759+
public class TestEchoBodyStringEnumExample
760+
{
761+
public static void Main()
762+
{
763+
Configuration config = new Configuration();
764+
config.BasePath = "http://localhost:3000";
765+
var apiInstance = new BodyApi(config);
766+
var body = null; // string? | String enum (optional)
767+
768+
try
769+
{
770+
// Test string enum response body
771+
StringEnumRef result = apiInstance.TestEchoBodyStringEnum(body);
772+
Debug.WriteLine(result);
773+
}
774+
catch (ApiException e)
775+
{
776+
Debug.Print("Exception when calling BodyApi.TestEchoBodyStringEnum: " + e.Message);
777+
Debug.Print("Status Code: " + e.ErrorCode);
778+
Debug.Print(e.StackTrace);
779+
}
780+
}
781+
}
782+
}
783+
```
784+
785+
#### Using the TestEchoBodyStringEnumWithHttpInfo variant
786+
This returns an ApiResponse object which contains the response data, status code and headers.
787+
788+
```csharp
789+
try
790+
{
791+
// Test string enum response body
792+
ApiResponse<StringEnumRef> response = apiInstance.TestEchoBodyStringEnumWithHttpInfo(body);
793+
Debug.Write("Status Code: " + response.StatusCode);
794+
Debug.Write("Response Headers: " + response.Headers);
795+
Debug.Write("Response Body: " + response.Data);
796+
}
797+
catch (ApiException e)
798+
{
799+
Debug.Print("Exception when calling BodyApi.TestEchoBodyStringEnumWithHttpInfo: " + e.Message);
800+
Debug.Print("Status Code: " + e.ErrorCode);
801+
Debug.Print(e.StackTrace);
802+
}
803+
```
804+
805+
### Parameters
806+
807+
| Name | Type | Description | Notes |
808+
|------|------|-------------|-------|
809+
| **body** | **string?** | String enum | [optional] |
810+
811+
### Return type
812+
813+
[**StringEnumRef**](StringEnumRef.md)
814+
815+
### Authorization
816+
817+
No authorization required
818+
819+
### HTTP request headers
820+
821+
- **Content-Type**: application/json
822+
- **Accept**: application/json
823+
824+
733825
### HTTP response details
734826
| Status code | Description | Response headers |
735827
|-------------|-------------|------------------|

0 commit comments

Comments
 (0)