Repository navigation
FFL-3565: Accept every numeric type in Int and Double reads - #4099
gh-worker-dd-mergequeue-cf854d[bot] merged 10 commits into
Conversation
🎉 All green!🧪 All tests passed 🎯 Code Coverage (details) 🔗 Commit SHA: a35f42a | Docs | View more details | Give us feedback! |
Read any numeric flag as Int when it is a whole number within Int range. Return TYPE_MISMATCH for fractional or out-of-range values, keep PARSE_ERROR for values that can't be parsed, and keep Double reads. Ref: FFL-3565
3a64335 to
6c1181d
Compare
Cap decimal parsing at 64 characters, add non-numeric, boundary and single-fit tests, and correct the README and KDoc. Unparseable values of every numeric type, including "float", return PARSE_ERROR. Ref: FFL-3565
Add 64- and 65-character boundary rows for the Int parse length limit, describe the limit precisely in the README and resolveIntValue KDoc, and reword the zero-scale comment to match observed Android behavior. Ref: FFL-3565
There was a problem hiding this comment.
🟡 Changes recommended
The boundary tests lack a regression case for long zero-padded integers, leaving the documented length-check exemption unprotected.
1 open issue
What changed in this PR
This PR makes numeric flag reads as Int lossless across "integer", "number" and "float" values, while leaving Double reads unchanged.
Changes:
- Accept whole numbers within
Intrange; distinguish type mismatches from parsing errors. - Bound decimal parsing to 64 characters, with an exception for plain integers within range.
- Document the behavior and extend conversion, logging and telemetry tests.
| File | Description |
|---|---|
reliability/single-fit/flags/src/test/kotlin/com/datadog/android/flags/integration/FlagsEvaluationTest.kt |
Adds cross-type numeric resolution checks. |
reliability/single-fit/flags/src/test/kotlin/com/datadog/android/flags/integration/EvaluationTelemetryTest.kt |
Checks numeric type-mismatch telemetry. |
features/dd-sdk-android-flags/src/test/kotlin/com/datadog/android/flags/internal/DatadogFlagsClientTest.kt |
Covers conversion boundaries, errors and warnings. |
features/dd-sdk-android-flags/src/main/kotlin/com/datadog/android/flags/internal/FlagValueConverter.kt |
Implements bounded, lossless numeric-to-Int conversion. |
features/dd-sdk-android-flags/src/main/kotlin/com/datadog/android/flags/FlagsClient.kt |
Documents Int resolution and length limits. |
features/dd-sdk-android-flags/README.md |
Explains numeric reads and error classifications. |
detekt_custom_unsafe_calls.yml |
Registers the potentially throwing trailing-zero operation. |
detekt_custom_safe_calls_third_party.yml |
Registers safe numeric parsing and conversion calls. |
Review effort: Balanced
Add a 65-character zero-padded integer row so moving the length check before toIntOrNull() fails a test. Ref: FFL-3565
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
More details
No actionable conversion defect was reported, including in the intentional length-limit exemption for zero-padded plain integers. Gradle suites and Android runtime checks were not rerun.
🤖 Bits Code Review · Commit c90d1c3
| Double::class -> | ||
| variationType == VariationType.NUMBER.value || variationType == VariationType.FLOAT.value || | ||
| variationType == VariationType.INTEGER.value | ||
| Int::class, Double::class -> isNumeric(variationType) |
There was a problem hiding this comment.
🤖 Context from Claude: Int reads now accept all three numeric types. A "number" value can hold a whole or a fractional number. OpenFeature defines one Number type. So the served type no longer decides an Int read. The parsed value does: a whole value in Int range succeeds. Double reads already accepted all three types.
| Boolean::class -> variationValue.lowercase(Locale.US).toBooleanStrictOrNull() as? T | ||
| String::class -> variationValue as T | ||
| Int::class -> variationValue.toIntOrNull() as? T | ||
| Int::class -> parseLosslessInt(variationValue, variationType) as? T |
There was a problem hiding this comment.
🤖 Context from Claude: The new Int and Double parsers use the existing runCatching block. If decimal parsing throws an unexpected exception, the client returns the default value with PARSE_ERROR. The exception does not reach the host app.
| * [MAX_DECIMAL_LENGTH] characters. | ||
| */ | ||
| private fun parseLosslessInt(variationValue: String, variationType: String): Int? = variationValue.toIntOrNull() | ||
| ?: variationValue.takeIf { it.length <= MAX_DECIMAL_LENGTH } |
There was a problem hiding this comment.
🤖 Context from Claude: Plain integers take the fast toIntOrNull() path. Other values longer than 64 characters skip the BigDecimal parse and report PARSE_ERROR. Without this limit, one long number string can take seconds on each read. On the JVM, a value of 1 followed by 200,000 zeros and .0 took about 10 s in stripTrailingZeros().
| } | ||
| if (problem != null) { | ||
| @Suppress("ThrowingInternalException") // Caught by runCatching in convert() | ||
| throw TypeMismatchException("Flag has type '$variationType' with $problem but Int was requested") |
There was a problem hiding this comment.
🤖 Context from Claude: Parsing succeeded before this branch. A fractional value, or a whole value outside Int range, cannot be an Int without loss. So it is a type mismatch, not a parse error. runCatching in convert() catches the exception. The client then reports TYPE_MISMATCH.
| * [BigDecimal.stripTrailingZeros]. | ||
| */ | ||
| private fun isFractional(decimal: BigDecimal): Boolean { | ||
| if (decimal.scale() <= 0 || decimal.signum() == 0) return false |
There was a problem hiding this comment.
🤖 Context from Claude: BigDecimal tells whole values from fractional ones exactly, with no rounding. The scale check comes first: stripTrailingZeros() throws ArithmeticException on scale overflow for input such as 100e2147483647. The zero check is for older Android libcore, which returns a zero such as 0.0 unchanged from stripTrailingZeros(). The detekt config now lists stripTrailingZeros() as a throwing call.
| "number, 5.00000000000000000000000000000000000000000000000000000000000000, 5", | ||
| "number, 00000000000000000000000000000000000000000000000000000000000000005, 5" | ||
| ) | ||
| fun `M return value W resolve() { numeric flag, whole value, Int default }`( |
There was a problem hiding this comment.
🤖 Context from Claude: These tests protect lossless Int conversion for all numeric flag types. After parsing succeeds, a whole value in Int range succeeds. A parsed fractional or out-of-range value returns TYPE_MISMATCH. A value that cannot be parsed returns PARSE_ERROR. If direct Int parsing fails, an input over 64 characters also returns PARSE_ERROR. The -2147483649 row catches a removed lower bound. The -2147483648.0 row catches <= instead of <. The 100e2147483647 row catches a removed scale check. The 67-character row catches a removed length limit.
Resolve the conflict in DatadogFlagsClientTest. Move the numeric tests to getEvaluationSnapshot(), which replaces getPrecomputedFlagWithContext() on develop. Ref: FFL-3565
There was a problem hiding this comment.
🔵 Needs a closer look
Changes to numeric results, error hooks, and exposure behavior require maintainer confirmation of release policy and coordination with stacked #4101’s stricter whole-Double and nonzero-underflow rules.
No open issues 🎉
Review effort: Balanced
|
🤖 Comment from Codex working with @typotter: [P2] Could you refresh #4101, rerun the shared fixtures at the merged |
Match the shared Double contract. A nonzero value that rounds to zero, such as 1e-400, and a whole value that the Double does not hold exactly, such as 1e300, now return TYPE_MISMATCH. Bound generated values so the unit test exercises a successful read and the single-fit test cannot pass by returning its default. Ref: FFL-3565
a35f42a
|
🤖 Response from AI: Re the fixture receipt: you were right: the earlier run used #65 at
|
| variationType: String | ||
| ): Double { | ||
| val problem = when { | ||
| double == 0.0 && decimal.signum() != 0 -> "nonzero value '$variationValue' that Double rounds to zero" |
There was a problem hiding this comment.
🤖 Context from Claude: This branch rejects underflow. The shared contract says a nonzero value that rounds to zero, such as 1e-400, returns the default value with TYPE_MISMATCH. Before, the read returned 0.0. double == 0.0 is also true for -0.0, so -1e-400 gives the same error. A true zero, such as 0e-400 or -0.0, has signum() == 0, so it still reads as zero.
| "float, 2e-324", | ||
| "integer, 1e-400" | ||
| ) | ||
| fun `M return default with TYPE_MISMATCH W resolve() { nonzero value that rounds to zero, Double default }`( |
There was a problem hiding this comment.
🤖 Context from Claude: These rows detect a lost underflow check. All 4 rows fail when the check is removed. They also fail when zero takes the fast path again, because the fast path returns 0.0 with no check. 2e-324 rounds to zero, but the smallest nonzero Double, 4.9e-324, does not. That value and the true zeros 0e-400 and -0.0 are success rows.
| "number, 10000000000000000000000000000000000000000000000000000000000000000.0", | ||
| "number, 9007199254740992.0000000000000000000000000000000000000000000000001", | ||
| "number, 1e2147483649", | ||
| "number, 0.000000000000000000000000000000000000000000000000000000000000000" |
There was a problem hiding this comment.
🤖 Context from Claude: A zero Double now goes through BigDecimal, so the 64-character limit applies to it. This 65-character zero gives PARSE_ERROR. Before, it read as 0.0. The row fails when zero takes the fast path again.
| val fakeFlagKey2 = forge.anAlphabeticalString() | ||
| val fakeFlagKey3 = forge.anAlphabeticalString() | ||
| val fakeFlagValue = forge.aDouble() | ||
| val fakeFlagValue = forge.aDouble(min = -1_000_000.0, max = 1_000_000.0) |
There was a problem hiding this comment.
🤖 Context from Claude: This bound keeps the test on the success path. A whole value must now equal the Double exactly. An unbounded random Double, such as 4.128878252279344E307, fails that check, because its printed form is not its exact binary value. The test failed with that value before the bound. #4101 uses the same bound.
| @StringForgery fakeString: String, | ||
| @IntForgery fakeInt: Int, | ||
| @DoubleForgery fakeDouble: Double, | ||
| @DoubleForgery(min = -1_000_000.0, max = 1_000_000.0) fakeDouble: Double, |
There was a problem hiding this comment.
🤖 Context from Claude: This bound keeps the test meaningful. By default, @DoubleForgery covers the full Double range. At those magnitudes, fakeDouble + 1 == fakeDouble, so a read that returns the default value still passes. With exact-only whole values, nearly every unbounded value takes that path.
| val discountPercentage = client.resolveDoubleValue("discount-rate", 0.0) | ||
| ``` | ||
|
|
||
| Any numeric flag can be read with either method. `resolveIntValue` returns the value only when it is a whole number within `Int` range, such as `5` or `5.0`. Otherwise, it returns the default value: `resolve()` reports `TYPE_MISMATCH` when the value is fractional, such as `1.2`, or outside `Int` range, such as `2147483648`, and `PARSE_ERROR` when the value can't be parsed. To bound parsing work, a value longer than 64 characters is not parsed unless it is a plain integer within `Int` range, so it reports `PARSE_ERROR`. `resolveDoubleValue` returns a fractional value as the nearest `Double`, such as `1.2`. It returns a whole value only when the `Double` has exactly that value. For other whole values, such as `9007199254740993` or `1e300`, `resolve()` reports `TYPE_MISMATCH`. It also reports `TYPE_MISMATCH` for a nonzero value that rounds to zero, such as `1e-400`. `NaN` and `Infinity` report `PARSE_ERROR`. The same applies to a value longer than 64 characters whose `Double` is zero or 2^53 or more in magnitude. |
There was a problem hiding this comment.
🤖 Context from Claude: This paragraph documents the Int-read and Double-read rules for app developers. The README definition of TYPE_MISMATCH, and the resolveIntValue and resolveDoubleValue KDoc, match it. The Double rules follow the shared fixture contract: a whole value must be exact, and a nonzero value that rounds to zero is TYPE_MISMATCH.

What does this PR do?
This PR makes Int reads of numeric flags lossless, and checks whole values in Double reads (FFL-3565). The served numeric type (
"integer","number"or"float") does not change the result:TYPE_MISMATCHwhen the parsed value is fractional or out of Int range.PARSE_ERRORwhen the SDK cannot parse the value. The SDK does not parse a value longer than 64 characters unless it is a plain integer in Int range.Double, such as1.2.Doublehas exactly that value. Otherwise it returnsTYPE_MISMATCH, for example for9007199254740993and1e300.TYPE_MISMATCHfor a nonzero value that rounds to zero, such as1e-400.PARSE_ERRORforNaNandInfinity, and for a value longer than 64 characters whoseDoubleis zero or 2^53 or more in magnitude.Int reads, compared with
develop:develop:"integer","number"develop:"float"5TYPE_MISMATCH5.0,1e3PARSE_ERRORTYPE_MISMATCH1.2PARSE_ERRORTYPE_MISMATCHTYPE_MISMATCH2147483648PARSE_ERRORTYPE_MISMATCHTYPE_MISMATCHnot-a-numberPARSE_ERRORTYPE_MISMATCHPARSE_ERRORDouble reads, compared with
develop(all numeric types):develop1.2,5,90071992547409929007199254740993,9223372036854775807,1e3009.007199254740992E15TYPE_MISMATCH1e400InfinityTYPE_MISMATCH1e-4000.0TYPE_MISMATCHNaN,Infinity,1e2147483649NaNorInfinityPARSE_ERRORDoubleis zero or 2^53 or more in magnitudeDoublePARSE_ERROREach error returns the default value.
Motivation
The OpenFeature spec defines a Number as "a numeric value of unspecified type or size". This PR uses lossless Int conversion and checks whole values in Double reads, for all three numeric types. Requirement 1.3.4 recommends the default value when a typed read gets a value of an unexpected type. The flagd numeric coercion ADR describes lossless numeric coercion, with a separate limit of 2^53−1. This PR does not add that limit. The Double rules match the shared fixture contract in DataDog/ffe-system-test-data#65.
Additional Notes
Release notes
resolve()and the OpenFeature provider do not logTYPE_MISMATCH. For parsed fractional or out-of-range"integer"or"number"values, these paths no longer log aPARSE_ERRORwarning.resolveIntValuelogs aTYPE_MISMATCHwarning."float"values that the SDK cannot parse now log aPARSE_ERRORwarning on all paths."integer"or"number"values, OpenFeature error hooks now getTYPE_MISMATCHinstead ofPARSE_ERROR."float"value in Int range now returns the value. SoresolveIntValueno longer logs aTYPE_MISMATCHwarning for it.TYPE_MISMATCHmessages include the value. Example:Flag has type 'number' with fractional value '1.2' but Int was requested.Doubledoes not hold exactly, such as9007199254740993or1e300, now returns the default value withTYPE_MISMATCHinstead of a rounded value.1e-400, now returns the default value withTYPE_MISMATCHinstead of0.0.NaNorInfinitynow returnPARSE_ERROR.1e400now returnsTYPE_MISMATCHinstead ofInfinity.resolveDoubleValuelogs aTYPE_MISMATCHwarning, and every read path logs aPARSE_ERRORwarning. Failed Double reads send no exposure or RUM evaluation events.Doubleis zero or 2^53 or more in magnitude, a Double read of a spelling thatBigDecimalrejects, such as one with surrounding spaces, now returnsPARSE_ERROR. This comes from reading the source; no test checks it.Implementation
parseLosslessInt()in therunCatchingblock ofconvert().BigDecimalfinds fractional values and checks the Int range. It does not round.isFractional()checks the scale before it callsstripTrailingZeros(). This prevents a scale overflow for values such as100e2147483647.toIntOrNull(). If that fails, a value longer than 64 characters returnsPARSE_ERROR. This limit bounds theBigDecimalwork.parseLosslessDouble(). A nonzeroDoublebelow 2^53 in magnitude returns with no other check, because every whole value there is exact. A zeroDouble, or one of 2^53 or more, goes throughBigDecimalwith the 64-character limit. There, a nonzero value that rounds to zero isTYPE_MISMATCH, and a whole value must equal theDoubleexactly.Documentation
FlagsClient.resolveIntValueandFlagsClient.resolveDoubleValueKDoc and the Flags README describe the Int-read and Double-read rules and the 64-character limit.TYPE_MISMATCHas a flag value type that does not match the expected type. Its example is a fractional value read withresolveIntValue.Validation
ktlintFormat,detektandlintReleasepass.testDebugUnitTest: 1107 tests, 0 failures.testDebugUnitTest: 84 tests, 0 failures.:reliability:single-fit:flags:testReleaseUnitTest: 212 tests, 0 failures. The new single-fit checks fail with thedevelopconverter (3 failures).checkGeneratedFilespasses."string"as numeric, remove the length limit, the lower Int bound or the scale check, or move the length limit by one or two characters.1e300.a35f42a), 100 pass, 3 are skipped and 4 fail.9afc3f7, 63 fixtures. The runner and snapshot are from FFL-3568: Share precomputed fixtures and lossless numeric policy #4101 at878dd3f. The snapshot'scases/is identical to9afc3f7. The fixtures omitextraLogging, which the Android model requires, so the test copy adds an empty object to each assignment. The local runner also continues past failed evaluation checks.variationType: this PR returnsTYPE_MISMATCH, and the contract expectsFLAG_NOT_FOUND. The runs coverdefaults-and-emission-gates, plain and obfuscated, from the network and from the restored cache. FFL-3568: Share precomputed fixtures and lossless numeric policy #4101 handles this case: on FFL-3568: Share precomputed fixtures and lossless numeric policy #4101 with07e625dmerged in locally, 104 pass, 3 are skipped and 0 fail.07e625d, 16 runs failed: the same 4, and 12 that readunderflow-double(1e-400) as0.0.faa89ed. That revision did not have the 6numeric-decimalfixtures.1e300. Unit tests cover it.BigDecimal("0.0").stripTrailingZeros()returns scale 0 on API 28 (buildPSR1.210301.009.B1) and API 36 emulators. The AOSP libcore source for Android 6 to 9 returns a zero unchanged, with its scale, so the API 28 emulator differs from that source. API 23 to 27 were not checked at runtime, so the zero guard inisFractional()stays."number". Later changes do not affect these values.20011.2PARSE_ERRORTYPE_MISMATCH1.2Review checklist (to be filled by reviewers)