Skip to content

fix(schema): state .default() values in OpenAPI and Avro - #154

Merged
bbasinsk merged 1 commit into
mainfrom
benbasinski97/cook-1702-openapi-and-avro-output-never-state
Dec 5, 2026
Merged

bbasinsk merged 1 commit into
mainfrom
benbasinski97/cook-1702-openapi-and-avro-output-never-state

Conversation

@bbasinsk

@bbasinsk bbasinsk commented Dec 5, 2026 •

Copy link
Copy Markdown
Owner

Linear: COOK-1702

Status: reported head 59b23a5 · merge readiness ready · risk medium

PR report

bbasinsk/katalyst · PR #154 · benbasinski97/cook-1702-openapi-and-avro-output-never-state → main · range 9809207..59b23a5 (merge-base 9809207) · head 59b23a5. There are no local-only changes.

Merge readiness: ready · Risk: medium · head 59b23a5

  • The required check build passed at 59b23a5. claude-review is not required. It failed in 22 s, before it reviewed anything, as it did on fix(schema): honor wrapped optional fields in OpenAPI and Avro #142.
  • There are 0 review threads. The PR is mergeable.
  • Waiting on you:
    • Read SpecAdapter.kt and OpenAPI.kt (the generated OpenAPI spec).
    • Decide on B7 (see section 1). It is met for the OpenAPI 3.0 that the spec declares today. COOK-1703 has not chosen a version yet.
Factor Assessment Evidence
Impact medium Generated OpenAPI specs and Avro schemas change for .default() fields, and consumers regenerate clients and register schemas from them. Bytes on the wire and union branch order do not change.
Rollback two-way A revert restores the earlier output. Field types are unchanged, so bytes written under either version read the same way.
Error detection silent A wrong stated default shows up only in a generated client or as a wrong filled-in value in a consumer. Tests pin the cases that are likely to go wrong (union branch, NaN, $ref).

Quality profile

Dimension Assessment Evidence or remaining concern
Behavior and scope No material concern identified B1–B6 pass (table in section 5). B7 is met for OpenAPI 3.0. JSON Schema is unchanged.
Maintainability No material concern identified (improved relative to base) Both generators read absence() for each field, which removes the two TODO(COOK-1702) markers. Avro keeps its rendered-type rule only for the null default (AvroSchema.kt toAvroField).
Correctness risk No material concern identified Avro states a default only when every union in the value is on its first branch and every float is finite (avroDatum). toAvroSchema() types are unchanged. Avro's validating parser re-reads every stated default in the tests.
Concept coherence No material concern identified (improved relative to base) Absence from #153 now decides required, default, and the Avro default. No generator matches Schema.Default to decide them.
Verification No material concern identified ./gradlew jvmTest test passed on all modules locally. The new tests that cover stated defaults fail on the base. CI build passed at the head.

Blockers: none identified. Main residual risk: clients generated from the spec now treat .default() fields as optional. Main verification gap: no generated client or OpenAPI validator ran against the new output.

1. Delivered behavior

Usage OpenAPI before → after Avro before → after
int().default(42) in required, no default → not required, default: 42 no default → "default": 42. A reader fills in 42 for data written without the field.
enumeration<Role>().default(MANAGER) required → not required, default: "MANAGER" no default → "default": "MANAGER"
list(person()).default(listOf(Customer(7, null))) (first union case) required → not required, default stated no default → [{"id":7,"email":null}], read back as Customer
list(person()).default(listOf(Employee(…))) (not the first case), or a non-null value in a nested .optional() required → not required, default stated no default → still no default. Avro would read it as Customer.
double().default(NaN) required → not required, no default no default → still no default. Nothing throws.
a record field .default(r) rendered as $ref required → not required, bare $ref depends on the union rule above
.optional() fields unchanged unchanged
  • B7: a $ref property states no default, because OpenAPI 3.0 ignores keywords beside $ref. An inlined record (inlineRefs = true) states it. If COOK-1703 moves to 3.1, references can carry default, and this rule should be revisited there.
  • Compatibility: a schema registry with BACKWARD compatibility now accepts adding a .default() field, which it rejected before.

2. Change accounting

Subsystem Category Files Added Removed Net
http-openapi source 2 32 6 +26
schema-avro source 1 55 4 +51
http-openapi tests 1 73 0 +73
schema-avro tests 1 144 0 +144
Total 5 304 10 +294

3. Maintainability and bug hotspots

  • Avro reads a default with the first branch of every union (AvroSchema.kt avroDatum). Classification: accepted tradeoff (the ticket's B4 allows "or the field has no Avro default"). Origin: introduced. A default that uses a later case gets no Avro default, so readers of old data still fail for that field, as before. Reordering branches would misread bytes already written, and the ticket forbids it.
  • The null default stays tied to the rendered type (toAvroField). Classification: accepted tradeoff, from the feat(schema)!: allow one .optional() or .default() per schema #153 review. Origin: pre-existing. This keeps the null default for unions led by an empty() case, which absence() reads as Required.
  • Pre-existing: a record whose fields all have .optional() or .default() emits "required": [], which is invalid in OpenAPI 3.0. COOK-1703 tracks this.

4. Concept changes and retirement

  • Changed: SchemaObject gains default: JsonElement?.
  • Removed: the two TODO(COOK-1702) markers. OpenAPI required now excludes Absence.Default as well as Absence.Null.
  • Introduced (private): avroDatum, which converts a value to Avro's default form or rejects it. jsonDefault and isFinite in SpecAdapter.kt.

5. Review and verification evidence

Evidence Revision Result Scope and limits
./gradlew jvmTest test 59b23a5 tree (before a refactor that only inlined a helper), local pass all modules, JVM only
./gradlew :schema-avro:jvmTest :http-openapi:jvmTest 59b23a5, local pass both changed modules
New Avro tests with AvroSchema.kt from the base local 3 fail (stated value, missing field filled in, first-case union) The guard tests pass on the base as expected.
CI build (required) 59b23a5 pass run
Review 59b23a5 author self-review only No fresh-context review ran. claude-review failed before it reviewed anything.
Example Check Surface Revision Result
B1 DefaultSchemaObjectTest "default field is not required and states its default" http-openapi 59b23a5 pass
B2 AvroDefaultTest "reader with a default fills in data written before the field existed" schema-avro 59b23a5 pass
B3 AvroDefaultTest "values written by the previous schema read back unchanged" plus "stating a default changes no field type" schema-avro 59b23a5 pass. The bytes are written with the new schema. The types are proven equal to the previous types.
B4 AvroDefaultTest "default Avro would read as another union branch is not stated" schema-avro 59b23a5 pass (no default)
B5 "non-finite default is not stated", in both modules both 59b23a5 pass
B6 the existing full-schema assertions in AvroSchemaTest and the field-type test schema-avro 59b23a5 pass
B7 "referenced default field is a bare reference" http-openapi 59b23a5 pass for OpenAPI 3.0. Not run through an OpenAPI validator.

6. Human attention

Human-review list (generated OpenAPI spec):

  • SpecAdapter.kt: required and default come from absence() for each record field.
  • OpenAPI.kt: SchemaObject.default.

Outstanding:

  1. B7: accept the 3.0 reading, or wait for COOK-1703 to choose a version.

What

A .default() field now leaves OpenAPI required and states its default. In Avro it gets a field default whenever Avro reads that default back unchanged.

Why

int().default(42) decodes a missing value as 42 in every decoder. The generated output did not say so:

  • Clients generated from the OpenAPI spec made the field mandatory.
  • Avro readers failed with "missing required field" on data written before the field existed.

How to verify

Run ./gradlew :schema-avro:jvmTest :http-openapi:jvmTest. The tests are in AvroDefaultTest and DefaultSchemaObjectTest.

Out of scope

  • JSON Schema defaults.
  • OpenAPI 3.0 $ref siblings and the empty required array (COOK-1703).

Risk + rollback

To roll back, revert the PR. No field type or union branch order changes.

🤖 Generated with Claude Code

A field declared `int().default(42)` decodes a missing value as 42, but
OpenAPI listed it in `required` with no `default`, and its Avro field had
no default, so a reader could not read data written before the field
existed.

Both generators now read the field's `absence()`:

- OpenAPI: a `.default()` field leaves `required` and states its value
  as JSON. A `$ref` property states none, because OpenAPI 3.0 ignores
  keywords beside `$ref`. A NaN or infinite value is left out.
- Avro: a `.default()` field states its value when Avro reads it back
  unchanged. Avro fills in every union in a default with its first
  branch, so a value on another branch, or a non-null value inside a
  nested `.optional()`, leaves the field without a default. Field types
  and union branch order do not change, so bytes already written read
  the same way.

Refs COOK-1702

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bbasinsk
bbasinsk marked this pull request as ready for review December 5, 2026 00:58
Copilot AI balanced review requested due to automatic review settings December 5, 2026 00:58
@bbasinsk
bbasinsk merged commit 8161006 into main Dec 5, 2026
1 of 2 checks passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants