Skip to content

Commit

Permalink
Add a config property to ignore unknown properties for all API calls.
Browse files Browse the repository at this point in the history
This is especially useful to make sure fields added to the XML API by
BGG do not crash the client.
  • Loading branch information
Bram-- committed Oct 10, 2024
1 parent bbd9117 commit bd2272d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/main/kotlin/org/audux/bgg/BggClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -954,14 +954,17 @@ object BggClient {
* @property retryMaxDelayMs see kdoc for formula
* @property retryRandomizationMs see kdoc for formula
* @property requestTimeoutMillis At which point requests time out/throw an time out Exception.
* @property failOnUnknownProperties whether BGGClient should fail when it encounters unknown
* properties or if it should parse what it can for the given api.
*/
data class BggClientConfiguration(
var maxConcurrentRequests: Int = 10,
var maxRetries: Int = 5,
var retryBase: Double = 2.0,
var retryMaxDelayMs: Long = 60_000,
var retryRandomizationMs: Long = 1_000,
var requestTimeoutMillis: Long = 15_000
var requestTimeoutMillis: Long = 15_000,
var failOnUnknownProperties: Boolean = true,
)

/** Thrown whenever any exception is thrown during a request to BGG. */
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/org/audux/bgg/InternalBggClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ internal class InternalBggClient {
XmlMapper.builder()
.apply {
configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true)
configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true)
configure(
DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,
BggClient.configuration.failOnUnknownProperties
)

addModule(JacksonXmlModule())
addModule(JavaTimeModule())
Expand Down
38 changes: 38 additions & 0 deletions src/test/kotlin/org/audux/bgg/BggClientTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import io.ktor.http.HttpStatusCode
import java.util.concurrent.CompletableFuture
import java.util.concurrent.CountDownLatch
import kotlinx.coroutines.runBlocking
import org.audux.bgg.request.things
import org.audux.bgg.response.Response
import org.audux.bgg.util.TestUtils
import org.audux.bgg.util.TestUtils.delayedResponse
import org.audux.bgg.util.TestUtils.setupMockEngine
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.params.ParameterizedTest
Expand Down Expand Up @@ -196,6 +199,41 @@ class BggClientTest {
}
}

@Nested
inner class UnknownProperty {
@Test
fun `fails parsing an unknown property`() = runBlocking {
BggClient.configure { failOnUnknownProperties = true }
BggClient.engine = { setupMockEngine("thing?id=with-unknown-property") }

val response = BggClient.things(ids = arrayOf(1)).call()

assertThat(response.data).isNull()
assertThat(response.error)
.isEqualTo(
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
"<items termsofuse=\"https://boardgamegeek.com/xmlapi/termsofuse\">\n" +
" <item><unkown>Exception</unkown></item>\n" +
"</items>"
)

BggClient.configure { failOnUnknownProperties = true }
}

@Test
fun `parses an item wih an unknown field`() = runBlocking {
BggClient.configure { failOnUnknownProperties = false }
BggClient.engine = { setupMockEngine("thing?id=with-unknown-property") }

val response = BggClient.things(ids = arrayOf(1)).call()

assertThat(response.data).isNotNull()
assertThat(response.error).isNull()

BggClient.configure { failOnUnknownProperties = true }
}
}

private fun testRetryConfiguration(config: BggClientConfiguration) =
config.apply {
retryBase = 1.0
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/xml/thing?id=with-unknown-property.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<items termsofuse="https://boardgamegeek.com/xmlapi/termsofuse">
<item><unkown>Exception</unkown></item>
</items>

0 comments on commit bd2272d

Please sign in to comment.