[Python] deserialize enum json response (fix #17789) (#17791)

* deserialize enum json response (python)

* adapt python samples: adding enum deserialization

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

* update samples
This commit is contained in:
Jonathan
2024-02-08 03:52:43 +01:00
committed by GitHub
parent c71eb5dfe8
commit f323a3e788
65 changed files with 3572 additions and 0 deletions

View File

@@ -96,6 +96,9 @@ class BodyApi
'testEchoBodyPetResponseString' => [
'application/json',
],
'testEchoBodyStringEnum' => [
'application/json',
],
'testEchoBodyTagResponseString' => [
'application/json',
],
@@ -2665,6 +2668,321 @@ class BodyApi
);
}
/**
* Operation testEchoBodyStringEnum
*
* Test string enum response body
*
* @param string|null $body String enum (optional)
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testEchoBodyStringEnum'] to see the possible values for this operation
*
* @throws ApiException on non-2xx response or if the response body is not in the expected format
* @throws InvalidArgumentException
* @return \OpenAPI\Client\Model\StringEnumRef
*/
public function testEchoBodyStringEnum(
?string $body = null,
string $contentType = self::contentTypes['testEchoBodyStringEnum'][0]
): \OpenAPI\Client\Model\StringEnumRef
{
list($response) = $this->testEchoBodyStringEnumWithHttpInfo($body, $contentType);
return $response;
}
/**
* Operation testEchoBodyStringEnumWithHttpInfo
*
* Test string enum response body
*
* @param string|null $body String enum (optional)
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testEchoBodyStringEnum'] to see the possible values for this operation
*
* @throws ApiException on non-2xx response or if the response body is not in the expected format
* @throws InvalidArgumentException
* @return array of \OpenAPI\Client\Model\StringEnumRef, HTTP status code, HTTP response headers (array of strings)
*/
public function testEchoBodyStringEnumWithHttpInfo(
?string $body = null,
string $contentType = self::contentTypes['testEchoBodyStringEnum'][0]
): array
{
$request = $this->testEchoBodyStringEnumRequest($body, $contentType);
try {
$options = $this->createHttpClientOption();
try {
$response = $this->client->send($request, $options);
} catch (RequestException $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
$e->getResponse() ? $e->getResponse()->getHeaders() : null,
$e->getResponse() ? (string) $e->getResponse()->getBody() : null
);
} catch (ConnectException $e) {
throw new ApiException(
"[{$e->getCode()}] {$e->getMessage()}",
(int) $e->getCode(),
null,
null
);
}
$statusCode = $response->getStatusCode();
if ($statusCode < 200 || $statusCode > 299) {
throw new ApiException(
sprintf(
'[%d] Error connecting to the API (%s)',
$statusCode,
(string) $request->getUri()
),
$statusCode,
$response->getHeaders(),
(string) $response->getBody()
);
}
switch($statusCode) {
case 200:
if ('\OpenAPI\Client\Model\StringEnumRef' === '\SplFileObject') {
$content = $response->getBody(); //stream goes to serializer
} else {
$content = (string) $response->getBody();
if ('\OpenAPI\Client\Model\StringEnumRef' !== 'string') {
try {
$content = json_decode($content, false, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
throw new ApiException(
sprintf(
'Error JSON decoding server response (%s)',
$request->getUri()
),
$statusCode,
$response->getHeaders(),
$content
);
}
}
}
return [
ObjectSerializer::deserialize($content, '\OpenAPI\Client\Model\StringEnumRef', []),
$response->getStatusCode(),
$response->getHeaders()
];
}
$returnType = '\OpenAPI\Client\Model\StringEnumRef';
if ($returnType === '\SplFileObject') {
$content = $response->getBody(); //stream goes to serializer
} else {
$content = (string) $response->getBody();
if ($returnType !== 'string') {
try {
$content = json_decode($content, false, 512, JSON_THROW_ON_ERROR);
} catch (\JsonException $exception) {
throw new ApiException(
sprintf(
'Error JSON decoding server response (%s)',
$request->getUri()
),
$statusCode,
$response->getHeaders(),
$content
);
}
}
}
return [
ObjectSerializer::deserialize($content, $returnType, []),
$response->getStatusCode(),
$response->getHeaders()
];
} catch (ApiException $e) {
switch ($e->getCode()) {
case 200:
$data = ObjectSerializer::deserialize(
$e->getResponseBody(),
'\OpenAPI\Client\Model\StringEnumRef',
$e->getResponseHeaders()
);
$e->setResponseObject($data);
break;
}
throw $e;
}
}
/**
* Operation testEchoBodyStringEnumAsync
*
* Test string enum response body
*
* @param string|null $body String enum (optional)
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testEchoBodyStringEnum'] to see the possible values for this operation
*
* @throws InvalidArgumentException
* @return PromiseInterface
*/
public function testEchoBodyStringEnumAsync(
?string $body = null,
string $contentType = self::contentTypes['testEchoBodyStringEnum'][0]
): PromiseInterface
{
return $this->testEchoBodyStringEnumAsyncWithHttpInfo($body, $contentType)
->then(
function ($response) {
return $response[0];
}
);
}
/**
* Operation testEchoBodyStringEnumAsyncWithHttpInfo
*
* Test string enum response body
*
* @param string|null $body String enum (optional)
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testEchoBodyStringEnum'] to see the possible values for this operation
*
* @throws InvalidArgumentException
* @return PromiseInterface
*/
public function testEchoBodyStringEnumAsyncWithHttpInfo(
$body = null,
string $contentType = self::contentTypes['testEchoBodyStringEnum'][0]
): PromiseInterface
{
$returnType = '\OpenAPI\Client\Model\StringEnumRef';
$request = $this->testEchoBodyStringEnumRequest($body, $contentType);
return $this->client
->sendAsync($request, $this->createHttpClientOption())
->then(
function ($response) use ($returnType) {
if ($returnType === '\SplFileObject') {
$content = $response->getBody(); //stream goes to serializer
} else {
$content = (string) $response->getBody();
if ($returnType !== 'string') {
$content = json_decode($content);
}
}
return [
ObjectSerializer::deserialize($content, $returnType, []),
$response->getStatusCode(),
$response->getHeaders()
];
},
function ($exception) {
$response = $exception->getResponse();
$statusCode = $response->getStatusCode();
throw new ApiException(
sprintf(
'[%d] Error connecting to the API (%s)',
$statusCode,
$exception->getRequest()->getUri()
),
$statusCode,
$response->getHeaders(),
(string) $response->getBody()
);
}
);
}
/**
* Create request for operation 'testEchoBodyStringEnum'
*
* @param string|null $body String enum (optional)
* @param string $contentType The value for the Content-Type header. Check self::contentTypes['testEchoBodyStringEnum'] to see the possible values for this operation
*
* @throws InvalidArgumentException
* @return \GuzzleHttp\Psr7\Request
*/
public function testEchoBodyStringEnumRequest(
$body = null,
string $contentType = self::contentTypes['testEchoBodyStringEnum'][0]
): Request
{
$resourcePath = '/echo/body/string_enum';
$formParams = [];
$queryParams = [];
$headerParams = [];
$httpBody = '';
$multipart = false;
$headers = $this->headerSelector->selectHeaders(
['application/json', ],
$contentType,
$multipart
);
// for model (json/xml)
if (isset($body)) {
if (stripos($headers['Content-Type'], 'application/json') !== false) {
# if Content-Type contains "application/json", json_encode the body
$httpBody = \GuzzleHttp\Utils::jsonEncode(ObjectSerializer::sanitizeForSerialization($body));
} else {
$httpBody = $body;
}
} elseif (count($formParams) > 0) {
if ($multipart) {
$multipartContents = [];
foreach ($formParams as $formParamName => $formParamValue) {
$formParamValueItems = is_array($formParamValue) ? $formParamValue : [$formParamValue];
foreach ($formParamValueItems as $formParamValueItem) {
$multipartContents[] = [
'name' => $formParamName,
'contents' => $formParamValueItem
];
}
}
// for HTTP post (form)
$httpBody = new MultipartStream($multipartContents);
} elseif (stripos($headers['Content-Type'], 'application/json') !== false) {
# if Content-Type contains "application/json", json_encode the form parameters
$httpBody = \GuzzleHttp\Utils::jsonEncode($formParams);
} else {
// for HTTP post (form)
$httpBody = ObjectSerializer::buildQuery($formParams);
}
}
$defaultHeaders = [];
if ($this->config->getUserAgent()) {
$defaultHeaders['User-Agent'] = $this->config->getUserAgent();
}
$headers = array_merge(
$defaultHeaders,
$headerParams,
$headers
);
$operationHost = $this->config->getHost();
$query = ObjectSerializer::buildQuery($queryParams);
return new Request(
'POST',
$operationHost . $resourcePath . ($query ? "?{$query}" : ''),
$headers,
$httpBody
);
}
/**
* Operation testEchoBodyTagResponseString
*