diff --git a/src/main/java/com/launchdarkly/client/LDUser.java b/src/main/java/com/launchdarkly/client/LDUser.java index 6a68b64a4..0e14205c7 100644 --- a/src/main/java/com/launchdarkly/client/LDUser.java +++ b/src/main/java/com/launchdarkly/client/LDUser.java @@ -71,11 +71,13 @@ public LDUser(String key) { } protected JsonElement getValueForEvaluation(String attribute) { - try { - return UserAttribute.valueOf(attribute).get(this); - } catch (IllegalArgumentException expected) { - return getCustom(attribute); + // Don't use Enum.valueOf because we don't want to trigger unnecessary exceptions + for (UserAttribute builtIn: UserAttribute.values()) { + if (builtIn.name().equals(attribute)) { + return builtIn.get(this); + } } + return getCustom(attribute); } JsonPrimitive getKey() { diff --git a/src/test/java/com/launchdarkly/client/LDUserTest.java b/src/test/java/com/launchdarkly/client/LDUserTest.java index 085476034..f80184dce 100644 --- a/src/test/java/com/launchdarkly/client/LDUserTest.java +++ b/src/test/java/com/launchdarkly/client/LDUserTest.java @@ -4,6 +4,8 @@ import com.google.gson.JsonElement; import com.google.gson.JsonPrimitive; import com.google.gson.reflect.TypeToken; + +import org.junit.Assert; import org.junit.Test; import java.lang.reflect.Type; @@ -198,6 +200,38 @@ public void testLDUserCustomMarshalWithBuiltInAttributesRedactsCorrectAttrs() { Type type = new TypeToken>(){}.getType(); Map privateJson = config.gson.fromJson(config.gson.toJson(user), type); assertNull(privateJson.get("email")); - + } + + @Test + public void getValueGetsBuiltInAttribute() { + LDUser user = new LDUser.Builder("key") + .name("Jane") + .build(); + assertEquals(new JsonPrimitive("Jane"), user.getValueForEvaluation("name")); + } + + @Test + public void getValueGetsCustomAttribute() { + LDUser user = new LDUser.Builder("key") + .custom("height", 5) + .build(); + assertEquals(new JsonPrimitive(5), user.getValueForEvaluation("height")); + } + + @Test + public void getValueGetsBuiltInAttributeEvenIfCustomAttrHasSameName() { + LDUser user = new LDUser.Builder("key") + .name("Jane") + .custom("name", "Joan") + .build(); + assertEquals(new JsonPrimitive("Jane"), user.getValueForEvaluation("name")); + } + + @Test + public void getValueReturnsNullIfNotFound() { + LDUser user = new LDUser.Builder("key") + .name("Jane") + .build(); + assertNull(user.getValueForEvaluation("height")); } }