OpenAPITools / OpenAPITools/openapi-generator

[BUG] openapi-generator-cli generates C# code that ignores incorrect server responses

Open
#10,903 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The deserialization logic which is output in clients throws an ApiException in case the deserialization fails:

try
{
    return JsonConvert.DeserializeObject(response.Content, type, _serializerSettings);
}
catch (Exception e)
{
    throw new ApiException(500, e.Message);
}

However RestSharp's ExecuteAsync (which is being used in ApiClient) catches it, and ApiClient continues with an IRestResponse whose Data field is null:

response = await client.ExecuteAsync<T>(req, cancellationToken).ConfigureAwait(false);

The response is then converted into an ApiResponse<T> where Data is null and ErrorText is set:

var result = ToApiResponse(response);
if (response.ErrorMessage != null)
{
    result.ErrorText = response.ErrorMessage;
}

Then, the generated Api class returns it (this.ExceptionFactory!=null is true, but this.ExceptionFactory("ListBatchComplianceJobs", localVarResponse); returns null):

var localVarResponse = await this.AsynchronousClient.GetAsync<MultiComplianceJobResponse>("/2/compliance/jobs", localVarRequestOptions, this.Configuration, cancellationToken).ConfigureAwait(false);

if (this.ExceptionFactory != null)
{
    Exception _exception = this.ExceptionFactory("ListBatchComplianceJobs", localVarResponse);
    if (_exception != null) throw _exception;
}

return localVarResponse;

Then the `null data field is returned:

Foo.Client.ApiResponse<MultiComplianceJobResponse> localVarResponse = await ListBatchComplianceJobsWithHttpInfoAsync(type, status, cancellationToken).ConfigureAwait(false);
return localVarResponse.Data;
openapi-generator version

5.3.1

OpenAPI declaration file content or url

https://api.twitter.com/2/openapi.json

Generation Details
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
    -i https://api.twitter.com/2/openapi.json \
    -g csharp-netcore \
    -o /local/out/C#
  • some syntax error fixes
Steps to reproduce
  • Generate source code
  • Fix syntax errors introduced by #10897
  • Execute a request that returns an invalid enum variant
  • Observe that null is returned, instead of a thrown ApiException
Suggest a fix

I don't know your code base well enough to suggest a solution. Is it intended that the ExceptionFactory returns null?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated C# ApiClient and API methods using the csharp-netcore generator command described in the issue. Reproduce with the Twitter OpenAPI document and a response containing an invalid enum variant, then trace how RestSharp, ToApiResponse, and ExceptionFactory handle the deserialization failure. Done means the invalid response produces the intended ApiException rather than returning null data.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.