From 628ce6eb12c765113f7e4da7e25d92a12c436ae0 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 28 Nov 2026 09:20:02 -0400 Subject: [PATCH] fix(event): omit non-finite event tags from conversion events Event tags holding NaN or Infinity were copied into the conversion event as is. Gson and org.json refuse to serialize those values, so the exception surfaced on the dispatcher thread and the whole batch was dropped. Leave such tags out when the event is built, as is already done for the reserved value tag and for attributes. --- .../ab/event/internal/EventFactory.java | 36 +++++++++++++++- .../ab/event/internal/EventFactoryTest.java | 41 +++++++++++++++++++ 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/core-api/src/main/java/com/optimizely/ab/event/internal/EventFactory.java b/core-api/src/main/java/com/optimizely/ab/event/internal/EventFactory.java index c46703b28..65a1775d9 100644 --- a/core-api/src/main/java/com/optimizely/ab/event/internal/EventFactory.java +++ b/core-api/src/main/java/com/optimizely/ab/event/internal/EventFactory.java @@ -29,6 +29,7 @@ import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; @@ -130,6 +131,39 @@ private static Visitor createVisitor(ImpressionEvent impressionEvent) { .build(); } + /** + * Event tags holding non-finite numbers (NaN, Infinity) can not be represented in JSON and make + * strict serializers throw for the whole batch, so they are left out of the event. + * + * @param tags The event tags + * @return The event tags without non-finite numeric values + */ + private static Map removeNonFiniteTags(Map tags) { + if (tags == null) { + return null; + } + + Map filteredTags = null; + for (Map.Entry entry : tags.entrySet()) { + Object value = entry.getValue(); + if (!(value instanceof Double || value instanceof Float)) { + continue; + } + + double doubleValue = ((Number) value).doubleValue(); + if (Double.isNaN(doubleValue) || Double.isInfinite(doubleValue)) { + logger.warn("Event tag \"{}\" has a non-finite value \"{}\" and will be omitted from the event.", + entry.getKey(), value); + if (filteredTags == null) { + filteredTags = new HashMap(tags); + } + filteredTags.remove(entry.getKey()); + } + } + + return filteredTags == null ? tags : filteredTags; + } + private static Visitor createVisitor(ConversionEvent conversionEvent) { if (conversionEvent == null) { return null; @@ -143,7 +177,7 @@ private static Visitor createVisitor(ConversionEvent conversionEvent) { .setEntityId(conversionEvent.getEventId()) .setKey(conversionEvent.getEventKey()) .setRevenue(conversionEvent.getRevenue()) - .setTags(conversionEvent.getTags()) + .setTags(removeNonFiniteTags(conversionEvent.getTags())) .setType(conversionEvent.getEventKey()) .setValue(conversionEvent.getValue()) .build(); diff --git a/core-api/src/test/java/com/optimizely/ab/event/internal/EventFactoryTest.java b/core-api/src/test/java/com/optimizely/ab/event/internal/EventFactoryTest.java index ed9d32979..bc8a6aa01 100644 --- a/core-api/src/test/java/com/optimizely/ab/event/internal/EventFactoryTest.java +++ b/core-api/src/test/java/com/optimizely/ab/event/internal/EventFactoryTest.java @@ -1071,6 +1071,47 @@ public static LogEvent createImpressionEvent(ProjectConfig projectConfig, } + /** + * Verify that event tags holding non-finite numbers (NaN, Infinity) are omitted from the + * conversion event, since they have no JSON representation and make strict serializers + * (Gson, org.json) throw for the whole batch. + */ + @Test + public void createConversionEventOmitsNonFiniteEventTags() throws Exception { + EventType eventType = validProjectConfig.getEventTypes().get(0); + + Map eventTagMap = new HashMap(); + eventTagMap.put("string_param", "123"); + eventTagMap.put("double_param", 1.5); + eventTagMap.put("nan_param", Double.NaN); + eventTagMap.put("infinite_param", Double.POSITIVE_INFINITY); + eventTagMap.put("negative_infinite_param", Float.NEGATIVE_INFINITY); + + LogEvent conversionEvent = createConversionEvent( + validProjectConfig, + "userId", + eventType.getId(), + eventType.getKey(), + Collections.emptyMap(), + eventTagMap); + + Map expectedTags = new HashMap(); + expectedTags.put("string_param", "123"); + expectedTags.put("double_param", 1.5); + + Map tags = conversionEvent.getEventBatch() + .getVisitors().get(0).getSnapshots().get(0).getEvents().get(0).getTags(); + assertEquals(expectedTags, tags); + + // the caller's map must not be modified + assertEquals(5, eventTagMap.size()); + + // Gson rejects NaN and Infinity by default, so this used to throw IllegalArgumentException + String body = new Gson().toJson(conversionEvent.getEventBatch()); + assertTrue(body.contains("double_param")); + assertFalse(body.contains("nan_param")); + } + /** * Helper method for generating a conversion based LogEvent. */