Skip to content

Commit deb218f

Browse files
committed
Addess review comments (#1557)
1 parent 06046bd commit deb218f

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,8 @@ private <T> T convertValue(Object value, Class<?> expectedType) {
190190
|| expectedType == Double.class;
191191

192192
Object flagValue;
193+
String message = "Flag value had an unexpected type ("
194+
+ (value != null ? value.getClass() : "null") + "), expected (" + expectedType + ").";
193195
if (isPrimitive) {
194196
if (expectedType == Double.class) {
195197
if (value instanceof Double) {
@@ -201,8 +203,19 @@ private <T> T convertValue(Object value, Class<?> expectedType) {
201203
throw new TypeMismatchError("Flag value string could not be parsed as Double: " + value);
202204
}
203205
} else {
204-
throw new TypeMismatchError("Flag value had an unexpected type "
205-
+ (value != null ? value.getClass() : "null") + ", expected " + expectedType + ".");
206+
throw new TypeMismatchError(message);
207+
}
208+
} else if (expectedType == Integer.class) {
209+
if (value instanceof Integer) {
210+
flagValue = value;
211+
} else if (value instanceof String) {
212+
try {
213+
flagValue = Integer.parseInt((String) value);
214+
} catch (NumberFormatException e) {
215+
throw new TypeMismatchError("Flag value string could not be parsed as Integer: " + value);
216+
}
217+
} else {
218+
throw new TypeMismatchError(message);
206219
}
207220
} else {
208221
flagValue = value;
@@ -212,8 +225,7 @@ private <T> T convertValue(Object value, Class<?> expectedType) {
212225
}
213226

214227
if (!expectedType.isInstance(flagValue)) {
215-
throw new TypeMismatchError("Flag value had an unexpected type "
216-
+ (flagValue != null ? flagValue.getClass() : "null" + ", expected " + expectedType + "."));
228+
throw new TypeMismatchError(message);
217229
}
218230
return (T) flagValue;
219231
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,10 @@ private static Stream<Arguments> convertValueArguments() {
173173
Arguments.of(true, Boolean.class, true),
174174
Arguments.of("test", String.class, "test"),
175175
Arguments.of(123, Integer.class, 123),
176+
Arguments.of("123", Integer.class, 123),
176177
Arguments.of(3.14, Double.class, 3.14),
177-
Arguments.of("3.14", Double.class, 3.14));
178+
Arguments.of("3.14", Double.class, 3.14)
179+
);
178180
}
179181

180182
@Test

0 commit comments

Comments
 (0)