Kotlin: stop coercing a wrong-typed scalar into a String (#598)

Merged
JE
4mo ago
+421-46

Closes #598.

Kotlin's client-wide Json carried isLenient = true. That relaxes RFC-4627 far enough to read a JSON number or boolean into a declared String, so "description": 42 decoded to "42" — and the merge-safe composites then PUT that fabricated value back to a full-replace endpoint on a call that never mentioned the field. The coercion happens inside the decoder, so the per-composite guard that fixed Python, Ruby and TypeScript in #597 is structurally incapable of catching it: by the time the composite runs, the value is an ordinary String with no way to know whether the wire carried 42 or "42".

The fix is one line: drop isLenient, keep coerceInputValues.

The flag attribution — isLenient, not coerceInputValues

The issue text and the release plan disagreed about the cause. They are not interchangeable, and removing the wrong one ships a no-op as a fix. The issue was right.

kotlin/sdk/src/commonTest/kotlin/com/basecamp/sdk/DecoderStrictnessTest.kt is new and pins the semantics of each flag in isolation, so the answer lives in the tree rather than in anyone's recollection:

  • lenientCoercesNumberIntoString / lenientCoercesBooleanIntoString — isLenient alone produces "42" and "false" from 42 and false. This is the defect.
  • coerceInputValuesDoesNotCoerceAScalarType — coerceInputValues alone throws on {"description": 42}. It is not implicated, and dropping it would have been a no-op fix.
  • coerceInputValuesRewritesNullToTheDeclaredDefault / withoutCoerceInputValuesAnExplicitNullIsRejected — what coerceInputValues is actually for: an explicit null into a non-nullable property with a default. That is why it stays.
  • nullableFieldsAcceptExplicitNullEverywhere — the shape the SDK's own generated models use, so keeping or dropping coerceInputValues is invisible to them.
  • structuralMismatchIsRejectedEvenWhenLenient — arrays and objects were always refused. That is the half #576 correctly called safe, and it stays that way.

The tripwire flipped

TodolistsServiceTest carried a test that asserted the defect on purpose, and said so in its own doc comment:

This is a CROSS-SERVICE gap (#576) — the Documents composite has it too — not something a todolist-specific guard should paper over [...] This test records the exact behavior so closing #576 flips it visibly.

It asserted assertEquals(listOf("GET", "PUT"), capture.methods) and assertEquals("42", capture.putBody!!["description"]) — the coerced value landing on the wire.

It is now updateRefusesABareScalarDescriptionBeforeWriting and asserts the refusal: no PUT is issued, and the caller gets a statusless, non-retryable BasecampException.Api carrying a hint — for both a number (42) and a boolean (false), because isLenient accepted both and each produced a differently-shaped fabrication. That flip is the evidence the fix landed, on exactly the terms the original author asked for.

Todolists is a third affected service, beyond the Todos and Cards that #576 and #598 named. Documents and Schedules are the same composite shape and their comments are corrected here too.

Conformance: a bare-scalar kill case, and it needs no per-language skip

#598 anticipated a Kotlin-only fixture, since "no single value discriminates for both Kotlin (which needs a structural shape) and TypeScript Cards (which needs a falsey scalar)." That was true of the code as it stood. The decoder fix removes the asymmetry, so the fixture is better than anticipated: with isLenient gone, all six SDKs refuse a non-string scalar — Go, Kotlin and Swift in their model decoders (json.Unmarshal, kotlinx.serialization, Codable), and TypeScript, Python and Ruby in the hand-written writableString/write_string guards, whose non-string branch always covered 42. So conformance/tests/todos_write.json gains

update-kill: a bare-scalar description is refused before the full-replace PUT

as an ordinary six-runner case with zero entries added to any runner's skip map. Confirmed executing, not silently skipped, in all six: Go/Kotlin/Ruby/Python/Swift each print a PASS line, and TypeScript (which is non-verbose in the suite) was checked directly with npx vitest run -t 'bare-scalar' --reporter=verbose → ✓ conformance/todos_write.json > update-kill: a bare-scalar description is refused before the full-replace PUT.

conformance/check_kill_case_controls.py passes: the new body is the existing update-merge control's body with exactly one field perturbed, same as its array and object siblings.

It is a kill case, not decoration

Restoring isLenient = true locally and re-running make conformance-kotlin:

  FAIL: update-kill: a bare-scalar description is refused before the full-replace PUT
        Expected the call to fail, but it succeeded
Passed: 175, Failed: 1, Skipped: 1, Total: 177
MARKER_KT598_RED_EXIT=2

Reverted immediately; the committed tree has no isLenient assignment (git grep isLenient under kotlin/sdk/src/commonMain returns comment lines only).

Prose the fix falsified

Corrected only where the claim is now false — comments describing the scalar hole as an open cross-service gap, several of which also named the wrong flag pair:

  • TodolistsServiceTest.updateRefusesAMalformedDescriptionBeforeWriting — said arrays/objects were used because bare scalars get coerced; now cross-references the sibling.
  • DocumentsServiceTest, SchedulesServiceTest — same "An ARRAY, not a scalar" rationale.
  • DocumentsService.kt (×2), SchedulesService.kt, TodolistsService.kt — "(The client-wide coerceInputValues/isLenient scalar hole means a bare JSON scalar is coerced rather than rejected [...] not something this composite can close.)"
  • conformance/tests/todos_write.json — the two existing kill-case descriptions asserted a scalar "would NOT discriminate in Kotlin".

SPEC.md:1091 mentions coerceInputValues = true in the FlexibleIntSerializer discussion and is still accurate — that flag is unchanged — so it is left alone. Same for ReplayRunner.kt, whose stricter replay Json is unaffected.

Verification

Full suite run twice on the final tree, exit code captured via a marker written into the log and grepped back rather than trusted from a mid-run "BUILD SUCCESSFUL":

make kt-check conformance
MARKER_KT598_REAL_EXIT=0    (run 1)
MARKER_KT598_REAL_EXIT=0    (run 2)

Kotlin SDK checks passed and every runner was green. An earlier revision of this description claimed Passed: 176, Failed: 0, Skipped: 1, Total: 177 "in each runner" — that is Kotlin's and Swift's tally, not a shared one. The runners do not report a common shape, and their skip counts differ. The real per-runner numbers, from a fresh make conformance on the final tree:

RunnerPassedFailedSkippedTotal
Go17502177
Kotlin17601177
TypeScript21902221
Ruby166011177
Python17700177
Swift17601177
MARKER_CONFORMANCE_EXIT=0

TypeScript counts differently — it runs two suites, so 221 is not comparable to the others' 177. No test other than the deliberate tripwire needed flipping.


Summary by cubic

Stops Kotlin from coercing wrong-typed scalars into Strings by removing isLenient from the client-wide Json. Numbers/booleans are now rejected before any write, aligning Kotlin with the other SDKs and closing #598.

  • Bug Fixes
    • Removed isLenient from the client-wide Json; kept coerceInputValues for explicit nulls.
    • Strengthened tests: DecoderStrictnessTest pins each flag and now decodes through BasecampClient.json to guard the actual config.
    • Todolists: flipped the scalar test to assert refusal before PUT; updated Documents and Schedules tests and comments.
    • Cards: added a test to refuse a bare-scalar due_on from the server response.
    • Conformance: added a bare-scalar kill case to conformance/tests/todos_write.json; passes in all six SDKs with no skips.