Skip to content

fix: issue on unknown field #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.openfeature.contrib.providers.gofeatureflag;

import static java.net.HttpURLConnection.HTTP_BAD_REQUEST;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
Expand Down Expand Up @@ -44,7 +46,8 @@
public class GoFeatureFlagProvider implements FeatureProvider {
private static final String NAME = "GO Feature Flag Provider";
private static final ObjectMapper requestMapper = new ObjectMapper();
private static final ObjectMapper responseMapper = new ObjectMapper();
private static final ObjectMapper responseMapper = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
private HttpUrl parsedEndpoint;
// httpClient is the instance of the OkHttpClient used by the provider
private OkHttpClient httpClient;
Expand Down Expand Up @@ -197,7 +200,10 @@ private <T> ProviderEvaluation<T> resolveEvaluationGoFeatureFlagProxy(
if (Reason.DISABLED.name().equalsIgnoreCase(goffResp.getReason())) {
// we don't set a variant since we are using the default value, and we are not able to know
// which variant it is.
return ProviderEvaluation.<T>builder().value(defaultValue).reason(Reason.DISABLED.name()).build();
return ProviderEvaluation.<T>builder()
.value(defaultValue)
.variant(goffResp.getVariationType())
.reason(Reason.DISABLED.name()).build();
}

if (ErrorCode.FLAG_NOT_FOUND.name().equalsIgnoreCase(goffResp.getErrorCode())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ void should_throw_an_error_if_no_targeting_key() throws InvalidOptions {
assertEquals("True", res.getVariant());
}

@Test
void should_not_fail_if_receive_an_unknown_field_in_response() throws InvalidOptions {
GoFeatureFlagProvider g = new GoFeatureFlagProvider(GoFeatureFlagProviderOptions.builder().endpoint(this.baseUrl.toString()).timeout(1000).build());
ProviderEvaluation<Boolean> res = g.getBooleanEvaluation("unknown_field", false, this.evaluationContext);
assertEquals(true, res.getValue());
assertNull(res.getErrorCode());
assertEquals(Reason.TARGETING_MATCH.toString(), res.getReason());
assertEquals("True", res.getVariant());
}

private String readMockResponse(String filename) throws IOException {
String file = getClass().getClassLoader().getResource("mock_responses/" + filename).getFile();
byte[] bytes = Files.readAllBytes(Paths.get(file));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"trackEvents": true,
"variationType": "True",
"failed": false,
"version": 0,
"reason": "TARGETING_MATCH",
"errorCode": "",
"value": true,
"unknown_field": true,
"unknown_field2": "unknown_field2"
}