Skip to content
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
Expand Up @@ -51,7 +51,7 @@ class TraitSerializer : JsonSerializer<Trait> {
val jsonObject = JsonObject()
src.identifier?.let { jsonObject.addProperty("identifier", it) }
jsonObject.addProperty("trait_key", src.key)
jsonObject.addProperty("trait_value", src.traitValue.toString())
jsonObject.add("trait_value", context.serialize(src.traitValue))
if (src.transient) {
jsonObject.addProperty("transient", true)
}
Expand Down
43 changes: 43 additions & 0 deletions FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,49 @@ class FeatureFlagTests {
}
}

@Test
fun testGetFeatureFlagsWithTypedTraits() {
mockServer.`when`(
request()
.withPath("/identities/")
.withMethod("POST")
.withBody(
json(
"""
{
"identifier": "identity",
"traits": [
{ "trait_key": "string-trait", "trait_value": "value" },
{ "trait_key": "int-trait", "trait_value": 5 },
{ "trait_key": "double-trait", "trait_value": 0.5 },
{ "trait_key": "boolean-trait", "trait_value": true }
]
}
""".trimIndent()
)
)
)
.respond(
response()
.withStatusCode(200)
.withBody(MockResponses.getTransientIdentities)
)

runBlocking {
val result = flagsmith.getFeatureFlagsSync(
"identity",
listOf(
Trait("string-trait", "value"),
Trait("int-trait", 5),
Trait("double-trait", 0.5),
Trait("boolean-trait", true),
),
)

assertTrue(result.isSuccess)
}
}

@Test
fun testGetFeatureFlagsWithTransientIdentity() {
mockServer.`when`(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ class SerializerTests {
)
}

@Test
fun `TraitSerializer keeps the JSON type of trait values`() {
assertEquals("""{"trait_key":"key","trait_value":"value"}""", gson.toJson(Trait("key", "value")))
assertEquals("""{"trait_key":"key","trait_value":5}""", gson.toJson(Trait("key", 5)))
assertEquals("""{"trait_key":"key","trait_value":0.5}""", gson.toJson(Trait("key", 0.5)))
assertEquals("""{"trait_key":"key","trait_value":true}""", gson.toJson(Trait("key", true)))
}

@Test
fun `IdentityAndTraitsSerializer keeps the JSON type of trait values`() {
val identity = IdentityAndTraits(
identifier = "test-user",
traits = listOf(Trait("plan", "premium"), Trait("age", 30), Trait("verified", true))
)

assertEquals(
"""{"identifier":"test-user","traits":[{"trait_key":"plan","trait_value":"premium"},""" +
"""{"trait_key":"age","trait_value":30},{"trait_key":"verified","trait_value":true}]}""",
gson.toJson(identity)
)
}

@Test
fun `TraitSerializer handles optional identifier`() {
val traitWithId = Trait(
Expand Down
Loading