You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
The generated code calls RestSharp using generic methods that attempt to deserialize the response into a specified model. However, the model passed to the method is always the model defined in the 200 response of the OAS specification. Therefore, if another response is returned, even if it is expected and defined in the OAS file, the code throws an unhandled exception.
The expected result is an ApiException containing the data returned in the response from the Web API.
openapi-generator version
Versions 5.1.1, 5.2.0, 5.2.1, 5.2.2 and 6.0.0, including latest snapshots of each have been tried.
OpenAPI declaration file content or url
Consider the following OAS snippet, noting that two responses are expected and defined:
/file/search/{SearchRoot}:
get:
operationId: SearchFileSystemdescription: Search files and directories. Will search in all subdirectories.parameters:
- name: SearchRootin: pathrequired: trueschema:
type: string
- name: qin: queryrequired: truedescription: Search queryschema:
type: stringresponses:
200:
description: The search completed.content:
application/json:
schema:
type: arrayitems:
$ref: '#/components/schemas/FileSystemItemInfo'403:
$ref: '#/components/responses/Unauthorized'security:
- bearerAuth: []
Generation Details
Using the given command to generate client code (): java -jar openapi-generator-cli-6.0.0-20210712.095051-16.jar generate -i oas.yaml -o .\generated -g csharp-netcore --additional-properties=netCoreProjectFile=true,targetFramework=net5.0 --global-property modelTests=false --global-property apiTests=false
Steps to reproduce
The client is invoked with:
try{DefaultApim_sgcSSClient=new DefaultApi();List<FileSystemItemInfo>lstResults=await m_sgcSSClient.SearchFileSystemAsync("","cubes");}catch(ApiExceptionex){// handle the exception}
Note that the method called requires a bearer token, and that none was set. This is to force a 403 response from the Web API.
The expected result is an ApiException containing the data returned in the 403 response from the Web API.
The actual result is an unhandled exception thrown in the custom deserializer of the generated client. This happens because the WebAPI is called in the generated code using the following RestSharp call:
where T is FileSystemItemInfo. Thus, RestSharp attempts to deserialize the response to an object of type T regardless of the response code of the call, even though type T is only valid if the response code is 200.
Related issues/PRs
It seems that the same issue was found with the HttpClient library, and was fixed: #9389
Suggest a fix
The generated client should not attempt deserialization to the model type until the response code is verified, and the correct model is determined. One possible solution is to call RestSharp using the non-generic call:
Bug Report Checklist
Description
The generated code calls
RestSharp
using generic methods that attempt to deserialize the response into a specified model. However, the model passed to the method is always the model defined in the 200 response of the OAS specification. Therefore, if another response is returned, even if it is expected and defined in the OAS file, the code throws an unhandled exception.The expected result is an ApiException containing the data returned in the response from the Web API.
openapi-generator version
Versions 5.1.1, 5.2.0, 5.2.1, 5.2.2 and 6.0.0, including latest snapshots of each have been tried.
OpenAPI declaration file content or url
Consider the following OAS snippet, noting that two responses are expected and defined:
Generation Details
Using the given command to generate client code ():
java -jar openapi-generator-cli-6.0.0-20210712.095051-16.jar generate -i oas.yaml -o .\generated -g csharp-netcore --additional-properties=netCoreProjectFile=true,targetFramework=net5.0 --global-property modelTests=false --global-property apiTests=false
Steps to reproduce
The client is invoked with:
Note that the method called requires a bearer token, and that none was set. This is to force a 403 response from the Web API.
The expected result is an ApiException containing the data returned in the 403 response from the Web API.
The actual result is an unhandled exception thrown in the custom deserializer of the generated client. This happens because the WebAPI is called in the generated code using the following
RestSharp
call:where T is
FileSystemItemInfo
. Thus,RestSharp
attempts to deserialize the response to an object of type T regardless of the response code of the call, even though type T is only valid if the response code is 200.Related issues/PRs
It seems that the same issue was found with the
HttpClient
library, and was fixed:#9389
Suggest a fix
The generated client should not attempt deserialization to the model type until the response code is verified, and the correct model is determined. One possible solution is to call
RestSharp
using the non-generic call:and then call the custom deserialzor after checking
response.StatusCode
.The relevant lines are in the `ApiCLient.mustache' file, on lines 492 and 607.
The text was updated successfully, but these errors were encountered: