Skip to content

NullReferenceException in ApiClient.CallApi when network connection fails #148

@mojtabaashena

Description

@mojtabaashena

When the ApiClient encounters a network-level failure (e.g., inability to connect to the host, DNS failure, or timeout), the actualRestClient.Execute(request) returns a response where the Headers property is null.

Currently, CallApi attempts to call response.Headers.ToList() immediately. Because Headers is null, this throws a System.ArgumentNullException: Value cannot be null. (Parameter 'source') instead of allowing the user to handle the actual network error or status code.

Steps to Reproduce

  • Initialize the ApiClient with an invalid base path or disconnect from the network.
  • Call any API method (e.g., PaymentsApi.CreatePayment).
  • The SDK throws System.ArgumentNullException during the logging/processing phase of the response.

Stack Trace
System.ArgumentNullException: Value cannot be null. (Parameter 'source').
ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
Enumerable.ToList[TSource](IEnumerable1 source) ApiClient.CallApi(String path, Method method, Dictionary2 queryParams, Object postBody, Dictionary2 headerParams, Dictionary2 formParams, Dictionary2 fileParams, Dictionary2 pathParams, String contentType, Boolean isResponseMLEForApi)
PaymentsApi.CreatePaymentWithHttpInfo(CreatePaymentRequest createPaymentRequest)
PaymentsApi.CreatePayment(CreatePaymentRequest createPaymentRequest)

Proposed Fix
Add a null check (null-coalescing operator) when assigning httpResponseHeaders to ensure the collection is at least an empty list if the connection failed.

httpResponseHeaders = response.Headers?.ToList<Parameter>() ?? new List<Parameter>();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions