Skip to content

Commit cd7a61a

Browse files
committed
fix(flipt): set variant attachment as value for object evaluation
Signed-off-by: Mark Phelps <[email protected]>
1 parent 03dfc91 commit cd7a61a

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

providers/flipt/pom.xml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@
2020
<javadoc.failOnWarnings>false</javadoc.failOnWarnings>
2121
</properties>
2222

23+
<developers>
24+
<developer>
25+
<id>markphelps</id>
26+
<name>Mark Phelps</name>
27+
<organization>Flipt Software</organization>
28+
<url>https://flipt.io/</url>
29+
</developer>
30+
</developers>
31+
2332
<dependencies>
2433
<dependency>
2534
<groupId>io.flipt</groupId>
@@ -33,6 +42,12 @@
3342
<version>2.0.16</version>
3443
</dependency>
3544

45+
<dependency>
46+
<groupId>com.fasterxml.jackson.core</groupId>
47+
<artifactId>jackson-databind</artifactId>
48+
<version>2.17.2</version>
49+
</dependency>
50+
3651
<dependency>
3752
<groupId>com.github.tomakehurst</groupId>
3853
<artifactId>wiremock-jre8</artifactId>

providers/flipt/src/main/java/dev/openfeature/contrib/providers/flipt/FliptProvider.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class FliptProvider extends EventProvider {
4747
@Getter
4848
private ProviderState state = ProviderState.NOT_READY;
4949

50-
private AtomicBoolean isInitialized = new AtomicBoolean(false);
50+
private final AtomicBoolean isInitialized = new AtomicBoolean(false);
5151

5252
/**
5353
* Constructor.
@@ -205,17 +205,19 @@ public ProviderEvaluation<Value> getObjectEvaluation(String key, Value defaultVa
205205
.build();
206206
}
207207

208+
Value value = new Value(response.getVariantKey());
208209
ImmutableMetadata.ImmutableMetadataBuilder flagMetadataBuilder = ImmutableMetadata.builder();
209-
if (response.getVariantAttachment() != null) {
210+
if (response.getVariantAttachment() != null && !response.getVariantAttachment().isEmpty()) {
210211
flagMetadataBuilder.addString("variant-attachment", response.getVariantAttachment());
212+
value = new Value(response.getVariantAttachment());
211213
}
212214

213215
return ProviderEvaluation.<Value>builder()
214-
.value(new Value(response.getVariantKey()))
215-
.variant(response.getVariantKey())
216-
.reason(TARGETING_MATCH.name())
217-
.flagMetadata(flagMetadataBuilder.build())
218-
.build();
216+
.value(value)
217+
.variant(response.getVariantKey())
218+
.reason(TARGETING_MATCH.name())
219+
.flagMetadata(flagMetadataBuilder.build())
220+
.build();
219221
}
220222

221223
@Override

providers/flipt/src/test/java/dev/openfeature/contrib/providers/flipt/FliptProviderTest.java

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@
55
import com.github.tomakehurst.wiremock.client.WireMock;
66
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
77
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
8-
import dev.openfeature.sdk.Client;
9-
import dev.openfeature.sdk.FlagEvaluationDetails;
10-
import dev.openfeature.sdk.ImmutableContext;
11-
import dev.openfeature.sdk.ImmutableMetadata;
12-
import dev.openfeature.sdk.MutableContext;
13-
import dev.openfeature.sdk.OpenFeatureAPI;
14-
import dev.openfeature.sdk.ProviderEvaluation;
15-
import dev.openfeature.sdk.ProviderEventDetails;
16-
import dev.openfeature.sdk.ProviderState;
178
import dev.openfeature.sdk.exceptions.GeneralError;
189
import dev.openfeature.sdk.exceptions.ProviderNotReadyError;
1910
import lombok.SneakyThrows;
@@ -25,14 +16,15 @@
2516
import java.net.URL;
2617
import java.nio.file.Files;
2718
import java.nio.file.Paths;
19+
import java.util.HashMap;
20+
import java.util.Map;
2821

2922
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
3023
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
3124
import static com.github.tomakehurst.wiremock.client.WireMock.post;
3225
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
3326
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
34-
import static org.junit.jupiter.api.Assertions.assertEquals;
35-
import static org.junit.jupiter.api.Assertions.assertThrows;
27+
import static org.junit.jupiter.api.Assertions.*;
3628

3729
/**
3830
* FliptProvider test, based on APIs mocking.
@@ -50,6 +42,8 @@ class FliptProviderTest {
5042
public static final Double DOUBLE_FLAG_VALUE = 1.23;
5143
public static final String USERS_FLAG_NAME = "users-flag";
5244
public static final String TARGETING_KEY = "targeting_key";
45+
public static final String OBJECT_FLAG_NAME = "object-flag";
46+
5347
private static FliptProvider fliptProvider;
5448
private static Client client;
5549

@@ -101,7 +95,6 @@ void getBooleanEvaluation() {
10195
mockFliptAPI("/evaluate/v1/boolean", "boolean.json", FLAG_NAME);
10296
MutableContext evaluationContext = new MutableContext();
10397
evaluationContext.setTargetingKey(TARGETING_KEY);
104-
assertEquals(true, fliptProvider.getBooleanEvaluation(FLAG_NAME, false, evaluationContext).getValue());
10598
assertEquals(true, client.getBooleanValue(FLAG_NAME, false, evaluationContext));
10699
assertEquals(false, client.getBooleanValue("non-existing", false, evaluationContext));
107100
}
@@ -171,7 +164,7 @@ void getEvaluationMetadataTest() {
171164
assertEquals("attachment-1", flagMetadata.getString("variant-attachment"));
172165
FlagEvaluationDetails<String> nonExistingFlagEvaluation = client.getStringDetails("non-existing", "",
173166
evaluationContext);
174-
assertEquals(null, nonExistingFlagEvaluation.getFlagMetadata().getBoolean("variant-attachment"));
167+
assertNull(nonExistingFlagEvaluation.getFlagMetadata().getBoolean("variant-attachment"));
175168
}
176169

177170
@SneakyThrows
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"flagKey": "object-flag",
3+
"match": true,
4+
"variantKey": "object-variant",
5+
"variantAttachment": "{\"key1\":\"value1\",\"key2\":42,\"key3\":true}"
6+
}

0 commit comments

Comments
 (0)