Skip to content

Commit

Permalink
[Python] deserialize enum json response (fix OpenAPITools#17789) (Ope…
Browse files Browse the repository at this point in the history
…nAPITools#17791)

* deserialize enum json response (python)

* adapt python samples: adding enum deserialization

* add echo test for enum json response deserialization (python)

* update samples
  • Loading branch information
joeka authored and kota65535 committed Feb 23, 2024
1 parent 3f81998 commit 5def63e
Show file tree
Hide file tree
Showing 65 changed files with 3,572 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import datetime
from dateutil.parser import parse
from enum import Enum
import json
import mimetypes
import os
Expand Down Expand Up @@ -437,6 +438,8 @@ class ApiClient:
return self.__deserialize_date(data)
elif klass == datetime.datetime:
return self.__deserialize_datetime(data)
elif issubclass(klass, Enum):
return self.__deserialize_enum(data, klass)
else:
return self.__deserialize_model(data, klass)

Expand Down Expand Up @@ -743,6 +746,24 @@ class ApiClient:
)
)

def __deserialize_enum(self, data, klass):
"""Deserializes primitive type to enum.

:param data: primitive type.
:param klass: class literal.
:return: enum value.
"""
try:
return klass(data)
except ValueError:
raise rest.ApiException(
status=0,
reason=(
"Failed to parse `{0}` as `{1}`"
.format(data, klass)
)
)

def __deserialize_model(self, data, klass):
"""Deserializes list or dict to model.

Expand Down
20 changes: 20 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/echo_api.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,26 @@ paths:
text/plain:
schema:
type: string
/echo/body/string_enum:
post:
tags:
- body
summary: Test string enum response body
description: Test string enum response body
operationId: test/echo/body/string_enum
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/StringEnumRef'
description: String enum
responses:
'200':
description: Successful operation
content:
application/json:
schema:
$ref: '#/components/schemas/StringEnumRef'
/binary/gif:
post:
tags:
Expand Down
1 change: 1 addition & 0 deletions samples/client/echo_api/csharp-restsharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ Class | Method | HTTP request | Description
*BodyApi* | [**TestEchoBodyFreeFormObjectResponseString**](docs/BodyApi.md#testechobodyfreeformobjectresponsestring) | **POST** /echo/body/FreeFormObject/response_string | Test free form object
*BodyApi* | [**TestEchoBodyPet**](docs/BodyApi.md#testechobodypet) | **POST** /echo/body/Pet | Test body parameter(s)
*BodyApi* | [**TestEchoBodyPetResponseString**](docs/BodyApi.md#testechobodypetresponsestring) | **POST** /echo/body/Pet/response_string | Test empty response body
*BodyApi* | [**TestEchoBodyStringEnum**](docs/BodyApi.md#testechobodystringenum) | **POST** /echo/body/string_enum | Test string enum response body
*BodyApi* | [**TestEchoBodyTagResponseString**](docs/BodyApi.md#testechobodytagresponsestring) | **POST** /echo/body/Tag/response_string | Test empty json (request body)
*FormApi* | [**TestFormIntegerBooleanString**](docs/FormApi.md#testformintegerbooleanstring) | **POST** /form/integer/boolean/string | Test form parameter(s)
*FormApi* | [**TestFormOneof**](docs/FormApi.md#testformoneof) | **POST** /form/oneof | Test form parameter(s) for oneOf schema
Expand Down
20 changes: 20 additions & 0 deletions samples/client/echo_api/csharp-restsharp/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,26 @@ paths:
summary: Test free form object
tags:
- body
/echo/body/string_enum:
post:
description: Test string enum response body
operationId: test/echo/body/string_enum
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/StringEnumRef'
description: String enum
responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/StringEnumRef'
description: Successful operation
summary: Test string enum response body
tags:
- body
/binary/gif:
post:
description: Test binary (gif) response body
Expand Down
92 changes: 92 additions & 0 deletions samples/client/echo_api/csharp-restsharp/docs/BodyApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ All URIs are relative to *http://localhost:3000*
| [**TestEchoBodyFreeFormObjectResponseString**](BodyApi.md#testechobodyfreeformobjectresponsestring) | **POST** /echo/body/FreeFormObject/response_string | Test free form object |
| [**TestEchoBodyPet**](BodyApi.md#testechobodypet) | **POST** /echo/body/Pet | Test body parameter(s) |
| [**TestEchoBodyPetResponseString**](BodyApi.md#testechobodypetresponsestring) | **POST** /echo/body/Pet/response_string | Test empty response body |
| [**TestEchoBodyStringEnum**](BodyApi.md#testechobodystringenum) | **POST** /echo/body/string_enum | Test string enum response body |
| [**TestEchoBodyTagResponseString**](BodyApi.md#testechobodytagresponsestring) | **POST** /echo/body/Tag/response_string | Test empty json (request body) |

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


### HTTP response details
| Status code | Description | Response headers |
|-------------|-------------|------------------|
| **200** | Successful operation | - |

[[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)

<a id="testechobodystringenum"></a>
# **TestEchoBodyStringEnum**
> StringEnumRef TestEchoBodyStringEnum (string? body = null)
Test string enum response body

Test string enum response body

### Example
```csharp
using System.Collections.Generic;
using System.Diagnostics;
using Org.OpenAPITools.Api;
using Org.OpenAPITools.Client;
using Org.OpenAPITools.Model;

namespace Example
{
public class TestEchoBodyStringEnumExample
{
public static void Main()
{
Configuration config = new Configuration();
config.BasePath = "http://localhost:3000";
var apiInstance = new BodyApi(config);
var body = null; // string? | String enum (optional)
try
{
// Test string enum response body
StringEnumRef result = apiInstance.TestEchoBodyStringEnum(body);
Debug.WriteLine(result);
}
catch (ApiException e)
{
Debug.Print("Exception when calling BodyApi.TestEchoBodyStringEnum: " + e.Message);
Debug.Print("Status Code: " + e.ErrorCode);
Debug.Print(e.StackTrace);
}
}
}
}
```

#### Using the TestEchoBodyStringEnumWithHttpInfo variant
This returns an ApiResponse object which contains the response data, status code and headers.

```csharp
try
{
// Test string enum response body
ApiResponse<StringEnumRef> response = apiInstance.TestEchoBodyStringEnumWithHttpInfo(body);
Debug.Write("Status Code: " + response.StatusCode);
Debug.Write("Response Headers: " + response.Headers);
Debug.Write("Response Body: " + response.Data);
}
catch (ApiException e)
{
Debug.Print("Exception when calling BodyApi.TestEchoBodyStringEnumWithHttpInfo: " + e.Message);
Debug.Print("Status Code: " + e.ErrorCode);
Debug.Print(e.StackTrace);
}
```

### Parameters

| Name | Type | Description | Notes |
|------|------|-------------|-------|
| **body** | **string?** | String enum | [optional] |

### Return type

[**StringEnumRef**](StringEnumRef.md)

### Authorization

No authorization required

### HTTP request headers

- **Content-Type**: application/json
- **Accept**: application/json


### HTTP response details
| Status code | Description | Response headers |
|-------------|-------------|------------------|
Expand Down
Loading

0 comments on commit 5def63e

Please sign in to comment.