Skip to content

Conversation

@tylerjroach
Copy link
Contributor

@tylerjroach tylerjroach commented Dec 5, 2025

Add configurable strict mode for import messages

This PR adds the ability to disable strict validation when importing historical events via the /import endpoint.

Changes

  • New DeliveryOptions class with builder pattern for configuring delivery behavior

    • importStrictMode (default: true) - Controls validation on /import endpoint
    • useIpAddress (default: false) - Controls IP-based geolocation for events/people/groups
  • New deliver(ClientDelivery, DeliveryOptions) method for advanced delivery configuration

  • Updated /import endpoint handling to support both strict modes:

    • strict=1 (default): Validates events, returns JSON response with validation errors
    • strict=0: Bypasses validation, returns plain text "0" or "1"
  • Backward compatible - Existing deliver() methods unchanged, default to strict=1

Usage

// Disable strict validation for bulk imports
DeliveryOptions options = new DeliveryOptions.Builder()
    .importStrictMode(false)
    .build();

mixpanelApi.deliver(delivery, options);

Configurable Import Max Message Count Summary

Add importMaxMessageCount builder option to allow custom configuration of the import batch size for the /import endpoint.

Changes

New Builder Method

  • Added importMaxMessageCount(int) to MixpanelAPI.Builder
  • Allows configuring batch size for import messages (default: 2000, range: 1-2000)
  • Validates input at builder level and constructor level for defense in depth

Implementation Details

  • Added mImportMaxMessageCount field to MixpanelAPI class
  • Updated sendImportMessages() to use configured batch size instead of static Config.IMPORT_MAX_MESSAGE_SIZE
  • Added Math.min() safety check in constructor to enforce maximum of 2000 (Mixpanel API limit)
  • Updated all constructor signatures to support the new parameter

Testing

  • Added testCustomImportMaxMessageCount() test case
  • Verifies that 250 messages are correctly split into 3 batches (100 + 100 + 50) when batch size is set to 100
  • Confirms builder correctly sets the field value

Usage Example

MixpanelAPI api = new MixpanelAPI.Builder()
    .importMaxMessageCount(500)  // Custom batch size instead of default 2000
    .build();

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds configurable strict mode for the /import endpoint, allowing developers to bypass validation when importing historical events. The implementation uses a new DeliveryOptions class with a builder pattern to configure delivery behavior, maintaining backward compatibility while adding flexibility.

  • Introduces DeliveryOptions class with builder pattern for configuring importStrictMode and useIpAddress
  • Updates /import endpoint handling to support both strict=1 (validation enabled) and strict=0 (validation bypassed) modes
  • Maintains backward compatibility by defaulting to strict=1 when using existing deliver() methods

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
src/main/java/com/mixpanel/mixpanelapi/DeliveryOptions.java New class providing builder pattern for delivery configuration with importStrictMode and useIpAddress options
src/main/java/com/mixpanel/mixpanelapi/MixpanelAPI.java Refactored deliver() methods to use DeliveryOptions, added strict mode parameter to import URL, and updated response parsing to handle both strict=1 (JSON) and strict=0 (plain text) responses
src/test/java/com/mixpanel/mixpanelapi/MixpanelAPITest.java Added comprehensive tests for DeliveryOptions builder, strict mode URL parameter generation, and useIpAddress option

/**
* Sets whether to use strict mode for import messages.
*
* will validate the supplied events and return a 400 status code if any of the events fail validation with details of the error
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

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

This line appears to be a leftover fragment from editing. It's not formatted as a proper JavaDoc comment (missing <p> tag or proper punctuation) and duplicates the content of the following line. Remove this line as the complete description starts on line 76.

Suggested change
* will validate the supplied events and return a 400 status code if any of the events fail validation with details of the error

Copilot uses AI. Check for mistakes.
Comment on lines +1506 to +1544
public void testImportWithStrictModeDisabled() {
// Test that strict=0 is in the URL when strictMode is false
final Map<String, String> capturedUrls = new HashMap<String, String>();

MixpanelAPI api = new MixpanelAPI("events url", "people url", "groups url", "import url") {
@Override
public boolean sendImportData(String dataString, String endpointUrl, String token) {
capturedUrls.put("endpoint", endpointUrl);
return true;
}
};

ClientDelivery c = new ClientDelivery();
long historicalTime = System.currentTimeMillis() - (90L * 24L * 60L * 60L * 1000L);

try {
JSONObject props = new JSONObject();
props.put("time", historicalTime);
props.put("$insert_id", "insert-id-1");
JSONObject importEvent = mBuilder.importEvent("user-1", "test event", props);
c.addMessage(importEvent);

// Disable strict mode
DeliveryOptions options = new DeliveryOptions.Builder()
.importStrictMode(false)
.build();
api.deliver(c, options);

String url = capturedUrls.get("endpoint");
assertTrue("With importStrictMode=false: strict=0 in URL", url.contains("strict=0"));

} catch (IOException e) {
fail("IOException: " + e.toString());
} catch (JSONException e) {
fail("JSON error: " + e.toString());
}

api.close();
}
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

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

The tests verify that strict=0 is added to the URL, but they don't test the actual response parsing logic for strict=0 mode. The sendImportData method in lines 596-604 of MixpanelAPI.java handles plain text "0" and "1" responses for strict=0, but this logic isn't covered by tests.

Consider adding tests that verify:

  1. A response of "1" is treated as success in strict=0 mode
  2. A response of "0" is handled appropriately in strict=0 mode
  3. The fallback to JSON parsing still works when needed

This can be tested by mocking the HTTP response instead of overriding sendImportData().

Copilot uses AI. Check for mistakes.
@jaredmixpanel jaredmixpanel self-requested a review December 8, 2025 18:26
@tylerjroach tylerjroach changed the title feat: Configurable import strict mode feat: Configurable import strict mode and max import message count Dec 8, 2025
Copy link
Contributor

@jaredmixpanel jaredmixpanel left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants