OpenAPITools / OpenAPITools/openapi-generator

[BUG] Generated SDK for C# does not handle TCP failure

Open
#15,471 2 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

It is possible to send a request to an URL that is properly resolved with DNS, but that is not accessible (TCP failure). In this case, the SDK generated with openapi-generator for C# simply returns null, without an exception.

openapi-generator version

I am using openapi-generator-cli-6.5.0.jar downloaded from:
https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/6.5.0/

OpenAPI declaration file content or url

https://gist.github.com/MatrixRonny/76a87acaf059685db1fa58d7b5c0855d/

Generation Details
openapi-generator-cli generate -i .\api-docs.json -g csharp-netcore -o "DeploymentServiceSdk" --skip-validate-spec
Steps to reproduce
  1. Generate API for specified JSON definition
  2. Create the following DNS entry on local machine:
    0.0.0.0 something.does-not-exist
  3. Run the following C# code
string noIpUrl = "http://something.does-not-exist"
var apiHandler = new ObfuscatedNameApi(noIpUrl);
var result = apiHandler.ObfuscatedPathPost(new ObfuscatedRequest());
//Actual behavior: result is null and no exception is thrown
//Expected behavior: throw exception
Related issues/PRs

There are issues for specific HTTP status codes. In this case, the internal ISynchronousClient returns a response with HTTP Status 0.

Suggest a fix

In the following generated code:

public static readonly ExceptionFactory DefaultExceptionFactory = (methodName, response) =>
{
    var status = (int)response.StatusCode;
    if (status >= 400)
    {
        return new ApiException(status,
            string.Format("Error calling {0}: {1}", methodName, response.RawContent),
            response.RawContent, response.Headers);
    }
    return null;
};

additional checks need to be made:

if (status == 0)
{
  return new ApiException(0, "Could not connect to API endpoint.");
}
else if (status >= 400)
{
  ...
}
Additional Information

The real problem was encountered within Kubernetees, due to services trying to access other services with external hostname. It was difficult to pinpoint what the actual problem was. The way the generated SDK currently works suggests that the API returns nothing. This is misleading.

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# ExceptionFactory and the internal ISynchronousClient described in the issue, then reproduce the behavior using the linked OpenAPI declaration and the openapi-generator-cli 6.5.0 command. Trace the HTTP status 0 path and add coverage for an unreachable endpoint; done means the generated SDK raises an exception instead of returning null.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, java, openapi
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.