Skip to content

fix: Exclude transient properties when false #63

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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,9 +1,27 @@
package com.flagsmith.entities

import com.google.gson.*
import com.google.gson.annotations.SerializedName
import java.lang.reflect.Type

data class IdentityAndTraits(
@SerializedName(value = "identifier") val identifier: String,
@SerializedName(value = "traits") val traits: List<Trait>,
@SerializedName(value = "transient") val transient: Boolean? = null
)
)

class IdentityAndTraitsSerializer : JsonSerializer<IdentityAndTraits> {
override fun serialize(src: IdentityAndTraits, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
// Create a JsonObject with all fields except transient
val jsonObject = JsonObject()
jsonObject.addProperty("identifier", src.identifier)
jsonObject.add("traits", context.serialize(src.traits))

// Only add transient if it's true
if (src.transient == true) {
jsonObject.addProperty("transient", true)
}

return jsonObject
}
}
17 changes: 15 additions & 2 deletions FlagsmithClient/src/main/java/com/flagsmith/entities/Trait.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.flagsmith.entities


import com.google.gson.*
import com.google.gson.annotations.SerializedName

import java.lang.reflect.Type

data class Trait (
val identifier: String? = null,
Expand Down Expand Up @@ -46,6 +46,19 @@ data class Trait (
get() = traitValue as? Boolean
}

class TraitSerializer : JsonSerializer<Trait> {
override fun serialize(src: Trait, typeOfSrc: Type, context: JsonSerializationContext): JsonElement {
val jsonObject = JsonObject()
src.identifier?.let { jsonObject.addProperty("identifier", it) }
jsonObject.addProperty("trait_key", src.key)
jsonObject.addProperty("trait_value", src.traitValue.toString())
if (src.transient) {
jsonObject.addProperty("transient", true)
}
return jsonObject
}
}

data class TraitWithIdentity (
@SerializedName(value = "trait_key") val key: String,
@SerializedName(value = "trait_value") val traitValue: Any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import android.util.Log
import com.flagsmith.FlagsmithCacheConfig
import com.flagsmith.entities.Flag
import com.flagsmith.entities.IdentityAndTraits
import com.flagsmith.entities.IdentityAndTraitsSerializer
import com.flagsmith.entities.IdentityFlagsAndTraits
import com.flagsmith.entities.Trait
import com.flagsmith.entities.TraitSerializer
import okhttp3.Cache
import okhttp3.Interceptor
import okhttp3.OkHttpClient
Expand All @@ -15,6 +18,7 @@ import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Query
import com.google.gson.GsonBuilder

interface FlagsmithRetrofitService {

Expand Down Expand Up @@ -97,9 +101,14 @@ interface FlagsmithRetrofitService {
.cache(cache)
.build()

val gson = GsonBuilder()
.registerTypeAdapter(IdentityAndTraits::class.java, IdentityAndTraitsSerializer())
.registerTypeAdapter(Trait::class.java, TraitSerializer())
.create()

val retrofit = Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,9 @@ class FeatureFlagTests {
},
{
"trait_key": "persisted-trait",
"trait_value": "value",
"transient": false
"trait_value": "value"
}
],
"transient": false
]
}
""".trimIndent()
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.flagsmith.entities

import com.google.gson.GsonBuilder
import org.junit.Assert.assertEquals
import org.junit.Test

class SerializerTests {
private val gson = GsonBuilder()
.registerTypeAdapter(IdentityAndTraits::class.java, IdentityAndTraitsSerializer())
.registerTypeAdapter(Trait::class.java, TraitSerializer())
.create()

@Test
fun `IdentityAndTraitsSerializer omits transient field when false`() {
val identity = IdentityAndTraits(
identifier = "test-user",
traits = listOf(Trait("key", "value")),
transient = false
)

val json = gson.toJson(identity)
assertEquals(
"""{"identifier":"test-user","traits":[{"trait_key":"key","trait_value":"value"}]}""",
json
)
}

@Test
fun `IdentityAndTraitsSerializer includes transient field when true`() {
val identity = IdentityAndTraits(
identifier = "test-user",
traits = listOf(Trait("key", "value")),
transient = true
)

val json = gson.toJson(identity)
assertEquals(
"""{"identifier":"test-user","traits":[{"trait_key":"key","trait_value":"value"}],"transient":true}""",
json
)
}

@Test
fun `TraitSerializer omits transient field when false`() {
val trait = Trait("key", "value", false)
val json = gson.toJson(trait)
assertEquals(
"""{"trait_key":"key","trait_value":"value"}""",
json
)
}

@Test
fun `TraitSerializer includes transient field when true`() {
val trait = Trait("key", "value", true)
val json = gson.toJson(trait)
assertEquals(
"""{"trait_key":"key","trait_value":"value","transient":true}""",
json
)
}

@Test
fun `TraitSerializer handles optional identifier`() {
val traitWithId = Trait(
identifier = "test-id",
key = "key",
traitValue = "value",
transient = false
)
val traitWithoutId = Trait("key", "value", false)

assertEquals(
"""{"identifier":"test-id","trait_key":"key","trait_value":"value"}""",
gson.toJson(traitWithId)
)
assertEquals(
"""{"trait_key":"key","trait_value":"value"}""",
gson.toJson(traitWithoutId)
)
}
}
Loading