Skip to content

Commit

Permalink
change BidResponse.ext to be a JsonElement (#155)
Browse files Browse the repository at this point in the history
* change BidResponse.ext to a class, updates tests
  • Loading branch information
renam authored Aug 15, 2024
1 parent e087abb commit a56520b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class BidResponse(
@JvmField @SerialName("duration") public val duration: Int = 0,
@JvmField @SerialName("exp") public val exp: Int = -1,
@JvmField @SerialName("external_notifications") public val external_notifications: Map<String, String> = emptyMap(),
@JvmField @SerialName("ext") public val ext: Map<String, String> = emptyMap(),
@JvmField @SerialName("ext") public val ext: Extension = Extension(),
) {
/** Urls to fire a request to when an impression is registered */
public val impression_trackers: Array<String>? get() = trackers["impression_trackers"]
Expand All @@ -66,9 +66,6 @@ public class BidResponse(
/** Url to fire a request to when this bid loses an auction */
public val loss_response: String? get() = external_notifications["loss_response"]

public val useNewRenderer: Boolean
get() = ext["use_new_renderer"].toBoolean()

public companion object {
/** Decodes a BidResponse from a Json string using the built in serializer */
@JvmStatic @JvmOverloads
Expand All @@ -84,4 +81,14 @@ public class BidResponse(
jsonSerializer: Json = BidRequest.lenientSerializer,
): String = jsonSerializer.encodeToString(serializer(), response)
}

/**
* BidResponse Extension object from Nimbus
*
* @property use_new_renderer Set to false if Nimbus has determined the creative should be rendered using IMA SDK
*/
@Serializable
public class Extension(
@JvmField @SerialName("use_new_renderer") public val use_new_renderer: Boolean = false,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldStartWith

const val testJson = """
private fun testJson(ext: String = "") = """
{
"type": "native",
"auction_id": "d07668d6-35ba-4870-a3cd-02b18fec1a12",
Expand All @@ -33,12 +33,13 @@ const val testJson = """
"win_response": "https://test.adsbynimbus.com/win_response/",
"loss_response": "https://test.adsbynimbus.com/loss_response/auctionPrice=[AUCTION_PRICE]&auctionMinToWin=[AUCTION_MIN_TO_WIN]&winningSource=[WINNING_SOURCE]"
}
$ext
}
"""

class DeserializationTest : StringSpec({

val response = BidResponse.fromJson(testJson)
val response = BidResponse.fromJson(testJson())

"BidResponse fromJson deserializes the type field" {
response.type shouldBe "native"
Expand Down Expand Up @@ -104,6 +105,20 @@ class DeserializationTest : StringSpec({
response.win_response shouldBe "https://test.adsbynimbus.com/win_response/"
}

"BidResponse fromJson deserializes use_new_renderer" {
response.ext.use_new_renderer shouldBe false
BidResponse.fromJson(testJson("""
,"ext": {
"use_new_renderer": true
}
""".trimIndent())).ext.use_new_renderer shouldBe true
BidResponse.fromJson(testJson("""
,"ext": {
"use_new_renderer": false
}
""".trimIndent())).ext.use_new_renderer shouldBe false
}

"BidResponse fromJson deserializes loss urls" {
response.loss_response shouldStartWith "https://test.adsbynimbus.com/loss_response/"
response.loss_response shouldContain "auctionPrice=[AUCTION_PRICE]"
Expand Down

0 comments on commit a56520b

Please sign in to comment.