Skip to content

Commit 2a290bb

Browse files
committed
fix: flagsmith string to double conversion issue (#1557)
1 parent 60544cf commit 2a290bb

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

providers/flagsmith/src/main/java/dev.openfeature.contrib.providers.flagsmith/FlagsmithProvider.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,19 +180,42 @@ private <T> ProviderEvaluation<T> buildEvaluation(
180180
* @param expectedType the type we expect for this value
181181
* @param <T> the type we want to convert to
182182
* @return A converted object
183+
* @throws TypeMismatchError if the value cannot be converted to the expected type
183184
*/
185+
@SuppressWarnings("unchecked")
184186
private <T> T convertValue(Object value, Class<?> expectedType) {
185187
boolean isPrimitive = expectedType == Boolean.class
186188
|| expectedType == String.class
187189
|| expectedType == Integer.class
188190
|| expectedType == Double.class;
189-
T flagValue = isPrimitive ? (T) value : (T) objectToValue(value);
190191

191-
if (flagValue.getClass() != expectedType) {
192-
throw new TypeMismatchError(
193-
"Flag value had an unexpected type " + flagValue.getClass() + ", expected " + expectedType + ".");
192+
Object flagValue;
193+
if (isPrimitive) {
194+
if (expectedType == Double.class) {
195+
if (value instanceof Double) {
196+
flagValue = value;
197+
} else if (value instanceof String) {
198+
try {
199+
flagValue = Double.parseDouble((String) value);
200+
} catch (NumberFormatException e) {
201+
throw new TypeMismatchError("Flag value string could not be parsed as Double: " + value);
202+
}
203+
} else {
204+
throw new TypeMismatchError("Flag value had an unexpected type "
205+
+ (value != null ? value.getClass() : "null") + ", expected " + expectedType + ".");
206+
}
207+
} else {
208+
flagValue = value;
209+
}
210+
} else {
211+
flagValue = objectToValue(value);
212+
}
213+
214+
if (!expectedType.isInstance(flagValue)) {
215+
throw new TypeMismatchError("Flag value had an unexpected type "
216+
+ (flagValue != null ? flagValue.getClass() : "null" + ", expected " + expectedType + "."));
194217
}
195-
return flagValue;
218+
return (T) flagValue;
196219
}
197220

198221
/**

providers/flagsmith/src/test/java/dev.openfeature.contrib.providers.flagsmith/FlagsmithProviderTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,24 @@ void tearDown() throws IOException {
159159
mockFlagsmithErrorServer.shutdown();
160160
}
161161

162+
@ParameterizedTest
163+
@MethodSource("convertValueArguments")
164+
void testConvertValue(Object input, Class<?> expectedType, Object expected) throws Exception {
165+
var method = flagsmithProvider.getClass().getDeclaredMethod("convertValue", Object.class, Class.class);
166+
method.setAccessible(true);
167+
Object result = method.invoke(flagsmithProvider, input, expectedType);
168+
assertEquals(expected, result);
169+
}
170+
171+
private static Stream<Arguments> convertValueArguments() {
172+
return Stream.of(
173+
Arguments.of(true, Boolean.class, true),
174+
Arguments.of("test", String.class, "test"),
175+
Arguments.of(123, Integer.class, 123),
176+
Arguments.of(3.14, Double.class, 3.14),
177+
Arguments.of("3.14", Double.class, 3.14));
178+
}
179+
162180
@Test
163181
void shouldInitializeProviderWhenAllOptionsSet() {
164182
HashMap<String, String> headers = new HashMap<String, String>() {

0 commit comments

Comments
 (0)