apollographql / apollo-kotlin-adapters

Datetime, BigDecimal and other adapters for Apollo Kotlin
MIT License
5 stars 2 forks source link

Character array is missing "e" notation exponential mark #27

Open jvanderwee opened 1 month ago

jvanderwee commented 1 month ago

When updating to 0.0.4 we get this exception thrown in tests using data builders for queries with BigDecimal fields:

Character array is missing "e" notation exponential mark.
java.lang.NumberFormatException: Character array is missing "e" notation exponential mark.
    at java.base/java.math.BigDecimal.<init>(Unknown Source)
    at java.base/java.math.BigDecimal.<init>(Unknown Source)
    at java.base/java.math.BigDecimal.<init>(Unknown Source)
    at com.apollographql.adapter.core.BigDecimalAdapter.fromJson(BigDecimal.kt:14)
martinbonnin commented 1 month ago

Thanks for reporting this. I believe https://github.com/apollographql/apollo-kotlin-adapters/commit/d2ccdecd37bd2b25dfa74f1c472278b6757f6e7e might be the issue although it mostly changes writing BigDecimal, not reading them.

Can you share your data builder code and fake resolver?

jvanderwee commented 4 weeks ago

Data builder:

UserReferralQuery.Data(resolver = FakeApolloResolver()) {
    userReferralTotal = buildReferralTotalType {
        value = BigDecimal.valueOf(20)
        currency = "GBP"
    }
},

Fake resolver:

class FakeApolloResolver : FakeResolver {
    private val delegate = DefaultFakeResolver(__Schema.all)

    override fun resolveLeaf(context: FakeResolverContext): Any {
        val fieldName = context.mergedField.type.rawType().name
        return when (fieldName) {
            "Decimal" -> BigDecimal.TEN
            else -> delegate.resolveLeaf(context)
        }
    }

    override fun resolveListSize(context: FakeResolverContext): Int {
        return delegate.resolveListSize(context)
    }

    override fun resolveMaybeNull(context: FakeResolverContext): Boolean {
        return delegate.resolveMaybeNull(context)
    }

    override fun resolveTypename(context: FakeResolverContext): String {
        return delegate.resolveTypename(context)
    }

    override fun stableIdForObject(
        obj: Map<String, Any?>,
        mergedField: CompiledField,
    ): String? {
        return delegate.stableIdForObject(obj, mergedField)
    }
}
martinbonnin commented 4 weeks ago

Thanks for this, I can reproduce. Will look into this

martinbonnin commented 4 weeks ago

Fix is here, it'll require a new version of Apollo Kotlin. In the meantime, feel free to stay on Apollo Adapters 0.0.3. Apologies for the inconvenience and thanks for catching this 🙏