Skip to content

Commit 2b7163a

Browse files
fix: issue on unknown field (#246)
Signed-off-by: Thomas Poignant <[email protected]> Co-authored-by: Michael Beemer <[email protected]>
1 parent cc5d7e5 commit 2b7163a

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

providers/go-feature-flag/src/main/java/dev/openfeature/contrib/providers/gofeatureflag/GoFeatureFlagProvider.java

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package dev.openfeature.contrib.providers.gofeatureflag;
22

33
import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;
4+
5+
import com.fasterxml.jackson.databind.DeserializationFeature;
46
import com.fasterxml.jackson.databind.ObjectMapper;
57
import com.fasterxml.jackson.databind.SerializationFeature;
68
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
@@ -44,7 +46,8 @@
4446
public class GoFeatureFlagProvider implements FeatureProvider {
4547
private static final String NAME = "GO Feature Flag Provider";
4648
private static final ObjectMapper requestMapper = new ObjectMapper();
47-
private static final ObjectMapper responseMapper = new ObjectMapper();
49+
private static final ObjectMapper responseMapper = new ObjectMapper()
50+
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
4851
private HttpUrl parsedEndpoint;
4952
// httpClient is the instance of the OkHttpClient used by the provider
5053
private OkHttpClient httpClient;
@@ -197,7 +200,10 @@ private <T> ProviderEvaluation<T> resolveEvaluationGoFeatureFlagProxy(
197200
if (Reason.DISABLED.name().equalsIgnoreCase(goffResp.getReason())) {
198201
// we don't set a variant since we are using the default value, and we are not able to know
199202
// which variant it is.
200-
return ProviderEvaluation.<T>builder().value(defaultValue).reason(Reason.DISABLED.name()).build();
203+
return ProviderEvaluation.<T>builder()
204+
.value(defaultValue)
205+
.variant(goffResp.getVariationType())
206+
.reason(Reason.DISABLED.name()).build();
201207
}
202208

203209
if (ErrorCode.FLAG_NOT_FOUND.name().equalsIgnoreCase(goffResp.getErrorCode())) {

providers/go-feature-flag/src/test/java/dev/openfeature/contrib/providers/gofeatureflag/GoFeatureFlagProviderTest.java

+10
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,16 @@ void should_throw_an_error_if_no_targeting_key() throws InvalidOptions {
284284
assertEquals("True", res.getVariant());
285285
}
286286

287+
@Test
288+
void should_not_fail_if_receive_an_unknown_field_in_response() throws InvalidOptions {
289+
GoFeatureFlagProvider g = new GoFeatureFlagProvider(GoFeatureFlagProviderOptions.builder().endpoint(this.baseUrl.toString()).timeout(1000).build());
290+
ProviderEvaluation<Boolean> res = g.getBooleanEvaluation("unknown_field", false, this.evaluationContext);
291+
assertEquals(true, res.getValue());
292+
assertNull(res.getErrorCode());
293+
assertEquals(Reason.TARGETING_MATCH.toString(), res.getReason());
294+
assertEquals("True", res.getVariant());
295+
}
296+
287297
private String readMockResponse(String filename) throws IOException {
288298
String file = getClass().getClassLoader().getResource("mock_responses/" + filename).getFile();
289299
byte[] bytes = Files.readAllBytes(Paths.get(file));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"trackEvents": true,
3+
"variationType": "True",
4+
"failed": false,
5+
"version": 0,
6+
"reason": "TARGETING_MATCH",
7+
"errorCode": "",
8+
"value": true,
9+
"unknown_field": true,
10+
"unknown_field2": "unknown_field2"
11+
}

0 commit comments

Comments
 (0)