Skip to content

Fix handling multiple request form - #2048

Open
poojah-adyen wants to merge 1 commit into
mainfrom
fix-handling-multipart-request
Open

Fix handling multiple request form#2048
poojah-adyen wants to merge 1 commit into
mainfrom
fix-handling-multipart-request

Conversation

@poojah-adyen

Copy link
Copy Markdown
Contributor

Description

Tested scenarios

Fixed issue:

@poojah-adyen
poojah-adyen requested a review from a team as a code owner August 24, 2026 14:13

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for binary and multipart/form-data request bodies, adding new classes like BinaryRequestBody, HttpFile, and RequestForm, and updating AdyenHttpClient and API templates to handle file uploads and form parameters. The review feedback highlights several opportunities to improve robustness by adding defensive null checks in constructors and methods across the new classes to prevent potential NullPointerException issues.

Comment on lines +36 to +39
public BinaryRequestBody(byte[] data, String contentType) {
this.data = Arrays.copyOf(data, data.length);
this.contentType = contentType;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent a potential NullPointerException when data is null, we should add a defensive null check in the constructor. If data is null, we can default to an empty byte array.

Suggested change
public BinaryRequestBody(byte[] data, String contentType) {
this.data = Arrays.copyOf(data, data.length);
this.contentType = contentType;
}
public BinaryRequestBody(byte[] data, String contentType) {
this.data = data != null ? Arrays.copyOf(data, data.length) : new byte[0];
this.contentType = contentType;
}

Comment on lines +36 to +39
public HttpFile(byte[] data, String name) {
this.data = Arrays.copyOf(data, data.length);
this.name = name;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent a potential NullPointerException when data is null, we should add a defensive null check in the constructor. If data is null, we can default to an empty byte array.

Suggested change
public HttpFile(byte[] data, String name) {
this.data = Arrays.copyOf(data, data.length);
this.name = name;
}
public HttpFile(byte[] data, String name) {
this.data = data != null ? Arrays.copyOf(data, data.length) : new byte[0];
this.name = name;
}

Comment on lines +85 to +91
public void add(String name, HttpFile file) {
if (!multipart) {
throw new IllegalStateException("Files require a multipart/form-data request");
}
multipartBuilder.addBinaryBody(
name, file.getData(), ContentType.DEFAULT_BINARY, file.getName());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent a potential NullPointerException when file is null, we should add a defensive null check and throw a clear IllegalArgumentException.

  public void add(String name, HttpFile file) {
    if (!multipart) {
      throw new IllegalStateException("Files require a multipart/form-data request");
    }
    if (file == null) {
      throw new IllegalArgumentException("File cannot be null");
    }
    multipartBuilder.addBinaryBody(
        name, file.getData(), ContentType.DEFAULT_BINARY, file.getName());
  }

Comment on lines +124 to +130
private static String normalizeMediaType(String contentType) {
int parameterIndex = contentType.indexOf(';');
return contentType
.substring(0, parameterIndex < 0 ? contentType.length() : parameterIndex)
.trim()
.toLowerCase();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent a potential NullPointerException when contentType is null, we should add a defensive null check at the beginning of normalizeMediaType.

  private static String normalizeMediaType(String contentType) {
    if (contentType == null) {
      return "";
    }
    int parameterIndex = contentType.indexOf(';');
    return contentType
        .substring(0, parameterIndex < 0 ? contentType.length() : parameterIndex)
        .trim()
        .toLowerCase();
  }

Comment on lines +289 to +299
HttpUriRequestBase createRequest(
String endpoint,
BinaryRequestBody requestBody,
Config config,
boolean isApiKeyRequired,
RequestOptions requestOptions,
ApiConstants.HttpMethod httpMethod,
Map<String, String> params)
throws HTTPClientException {
HttpUriRequestBase httpRequest =
createHttpRequestBase(createUri(endpoint, params), requestBody.getData(), httpMethod);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent a potential NullPointerException when requestBody is null, we should add a defensive null check at the beginning of createRequest.

Suggested change
HttpUriRequestBase createRequest(
String endpoint,
BinaryRequestBody requestBody,
Config config,
boolean isApiKeyRequired,
RequestOptions requestOptions,
ApiConstants.HttpMethod httpMethod,
Map<String, String> params)
throws HTTPClientException {
HttpUriRequestBase httpRequest =
createHttpRequestBase(createUri(endpoint, params), requestBody.getData(), httpMethod);
HttpUriRequestBase createRequest(
String endpoint,
BinaryRequestBody requestBody,
Config config,
boolean isApiKeyRequired,
RequestOptions requestOptions,
ApiConstants.HttpMethod httpMethod,
Map<String, String> params)
throws HTTPClientException {
if (requestBody == null) {
throw new IllegalArgumentException("requestBody cannot be null");
}
HttpUriRequestBase httpRequest =
createHttpRequestBase(createUri(endpoint, params), requestBody.getData(), httpMethod);

@sonarqubecloud

Copy link
Copy Markdown

@gcatanese gcatanese added the Fix Indicates a bug fix label Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Fix Indicates a bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants