Skip to content

Commit bfce072

Browse files
fix: Accept any numeric tracking event value (#65)
`track` threw a `ClassCastException` for a tracking event value which was not a `Double`. - `TrackingEventDetails.getValue()` is an `Optional<Number>`, so `new MutableTrackingEventDetails(99)` (an `Integer`) reached the provider and failed the `(Double)` cast before any event was sent. - The value is now converted with `Number.doubleValue()`, which is what the LaunchDarkly `trackMetric` API takes. - Added a test tracking an integer value. <details> <summary>Implementation details</summary> The OpenFeature Java SDK does not constrain the numeric type an application passes, and `MutableTrackingEventDetails` stores the `Number` as given, so any of `Integer`, `Long`, `Float`, or `BigDecimal` reached the cast. Only `Double` worked. Testing: the change is covered by `ProviderTest.itCanTrackAnIntegerTrackingEventValue`. The test suite could not be run on this machine because Maven Central and the Gradle plugin portal answered dependency resolution with HTTP 429 from this network; CI runs the suite. </details> Link to Devin session: https://app.devin.ai/sessions/fe1eb757fe694ef79f3d09f6307d4b47 Open in Devin Desktop: https://app.devin.ai/desktop/session/fe1eb757fe694ef79f3d09f6307d4b47?variant=devin Requested by: @kinyoklion <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Overview** > Fixes **`track`** failing with a **`ClassCastException`** when **`TrackingEventDetails`** carries a metric that is not a **`Double`** (for example **`MutableTrackingEventDetails(99)`** with an **`Integer`**). > > The provider now reads **`Optional<Number>`** via **`Number.doubleValue()`** before calling LaunchDarkly **`trackMetric`**, matching the API’s double metric parameter. A unit test asserts integer metrics are forwarded as **`99.0`**. > > <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit d3177c0. Bugbot is set up for automated code reviews on this repo. Configure [here](https://www.cursor.com/dashboard/bugbot).</sup> <!-- /CURSOR_SUMMARY --> Signed-off-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 373eac5 commit bfce072

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/main/java/com/launchdarkly/openfeature/serverprovider/Provider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public void track(String eventName, EvaluationContext ctx, TrackingEventDetails
266266
if (details != null) {
267267
Double metricValue = null;
268268
if (details.getValue().isPresent()) {
269-
metricValue = (Double) details.getValue().get();
269+
metricValue = details.getValue().get().doubleValue();
270270
}
271271
// Convert the Structure portion of the TrackingEventDetails into a key value
272272
// map.

src/test/java/com/launchdarkly/openfeature/serverprovider/ProviderTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,25 @@ public void itCanTrackFullTrackingEventDetails() {
214214
valueConverter.toLdValue(new Value(trackingEventDetails)),
215215
99.77);
216216
}
217+
218+
@Test
219+
public void itCanTrackAnIntegerTrackingEventValue() {
220+
EvaluationContext evaluationContext = new ImmutableContext("user-key");
221+
EvaluationContextConverter evaluationContextConverter = new EvaluationContextConverter(null);
222+
ValueConverter valueConverter = new ValueConverter(null);
223+
224+
TrackingEventDetails trackingEventDetails = new MutableTrackingEventDetails(99).add("currency", "USD");
225+
226+
OpenFeatureAPI.getInstance().setProvider(ldProvider);
227+
228+
OpenFeatureAPI
229+
.getInstance()
230+
.getClient().track("metric-key", evaluationContext, trackingEventDetails);
231+
232+
verify(mockedLdClient).trackMetric(
233+
"metric-key",
234+
evaluationContextConverter.toLdContext(evaluationContext),
235+
valueConverter.toLdValue(new Value(trackingEventDetails)),
236+
99.0);
237+
}
217238
}

0 commit comments

Comments
 (0)