Skip to content

Commit 79ca933

Browse files
fix(gofeatureflag): Fix NPE when error code is not set in the API response (#855)
Signed-off-by: Thomas Poignant <[email protected]>
1 parent f10d872 commit 79ca933

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public <T> EvaluationResponse<T> evaluateFlag(
142142
requestMapper.writeValueAsBytes(goffRequest),
143143
MediaType.get("application/json; charset=utf-8")));
144144

145-
if (this.apiKey != null && !"".equals(this.apiKey)) {
145+
if (this.apiKey != null && !this.apiKey.isEmpty()) {
146146
reqBuilder.addHeader(HttpHeaders.AUTHORIZATION, BEARER_TOKEN + this.apiKey);
147147
}
148148

@@ -299,6 +299,10 @@ public ConfigurationChange configurationHasChanged() throws GoFeatureFlagExcepti
299299
* @return an item from the enum
300300
*/
301301
private ErrorCode mapErrorCode(String errorCode) {
302+
if (errorCode == null || errorCode.isEmpty()) {
303+
return null;
304+
}
305+
302306
try {
303307
return ErrorCode.valueOf(errorCode);
304308
} catch (IllegalArgumentException e) {

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

+18
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,24 @@ void should_resolve_a_valid_boolean_flag_with_TARGETING_MATCH_reason() {
316316
assertEquals(want, got);
317317
}
318318

319+
@SneakyThrows
320+
@Test
321+
void should_resolve_a_valid_boolean_flag_with_TARGETING_MATCH_reason_without_error_code_in_payload() {
322+
GoFeatureFlagProvider g = new GoFeatureFlagProvider(GoFeatureFlagProviderOptions.builder().endpoint(this.baseUrl.toString()).timeout(1000).build());
323+
String providerName = this.testName;
324+
OpenFeatureAPI.getInstance().setProviderAndWait(providerName, g);
325+
Client client = OpenFeatureAPI.getInstance().getClient(providerName);
326+
FlagEvaluationDetails<Boolean> got = client.getBooleanDetails("bool_targeting_match_no_error_code", false, this.evaluationContext);
327+
FlagEvaluationDetails<Boolean> want = FlagEvaluationDetails.<Boolean>builder()
328+
.value(true)
329+
.variant("True")
330+
.flagKey("bool_targeting_match_no_error_code")
331+
.reason(Reason.TARGETING_MATCH.name())
332+
.flagMetadata(defaultMetadata)
333+
.build();
334+
assertEquals(want, got);
335+
}
336+
319337
@SneakyThrows
320338
@Test
321339
void should_resolve_a_valid_boolean_flag_with_TARGETING_MATCH_reason_cache_disabled() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"trackEvents": true,
3+
"variationType": "True",
4+
"failed": false,
5+
"version": 0,
6+
"reason": "TARGETING_MATCH",
7+
"value": true,
8+
"cacheable": true,
9+
"metadata": {
10+
"pr_link": "https://github.com/thomaspoignant/go-feature-flag/pull/916",
11+
"version": 1
12+
}
13+
}

0 commit comments

Comments
 (0)