Write open enums as anyOf so third-party generators accept openapi.json

Merged
JE
1d ago
+2092-199

Stacked on #1140; retarget to main once it merges.

Why

openapi.json should work with mainstream third-party generators, not only with ours. Measured on #1140's head, two things in the document broke them:

  • contentEncoding: "byte". It is not an RFC 2045 value, and swift-openapi-generator's parser refuses the whole document: Cannot initialize ContentEncoding from invalid String value byte.
  • Open enums written as a bare enum list. JSON Schema reads a bare enum as closed. swift-openapi-generator's client threw on a status the spec does not list (Cannot initialize FilingStatus from invalid String value rebalancing). openapi-generator's Go output did not compile at all, because FilingStatus and ProjectConstructionStatus constants collided (FAILED redeclared).

What changes on the wire

  • WireFormatSchemaMapper writes every enum as anyOf [{type: string, enum: [known]}, {type: string}] plus x-basecamp-open-enum: true, with no type or enum beside them. There are 10 such schemas.
  • smithy-build.json sets defaultBlobFormat: binary, so 6 payloads move from contentEncoding: byte to binary.
  • SPEC §10 "Open Enums" now describes this wire form and what third-party tools make of it. I made that change here rather than in #1140.

Other shapes I measured: a $ref'd Known enum inside the anyOf, the two branches swapped, and x-extensible-enum. All were worse. The $ref form still collides in openapi-generator Go. The other two compile but carry less: either the known values are lost to Swift, or the enum is still closed.

Third-party generators

openapi-consumers/run (or make openapi-consumers) generates code from openapi.json with each tool and compiles it. Each tool runs at a pinned version; the jars are checked against openapi-consumers/checksums.sha256. CI runs this as two new jobs, openapi-consumers-jvm (ubuntu) and openapi-consumers-swift (macos-15). Both jobs feed the Conformance Tests fan-in, so a failure gates. The Swift job selects Xcode 26, since swift-openapi-generator 1.14 needs Swift 6.2. Results, run locally:

ToolVersionGeneratesChecked by#1140 headThis PR
swift-openapi-generator1.14.4 (runtime 1.13.4)types; types + clientswift build, swift testrefuses the document (byte)compiles; 3/3 decode tests pass
fabrikt29.3.0HTTP models, Jackson and kotlinx.serializationcompileKotlincompilescompiles
openapi-generator7.28.0typescript-fetchtsc with strictcompilescompiles
openapi-generator7.28.0gogo build, go vetfails: redeclared constantscompiles

Unknown values decode. openapi-consumers/swift/Tests/TypesTests uses three fixtures:

  • a Filing with "status": "rebalancing", which decodes, keeps the string, and re-encodes it unchanged;
  • a known status, which decodes as its case;
  • a Project with an unknown status and purpose.

Swift is the strictest decoder of the four, and the tests assert both its known case (value1) and its raw-string fallback (value2). fabrikt types an open enum as String. openapi-generator wraps the raw string (Go struct { String *string }, TypeScript an empty interface), which compiles and passes values through but gives callers no typed known cases. The "Cannot initialize FilingStatus" failure above comes from a scratch run against the closed form, measured before this change. The committed tests are written against the anyOf shape, so against a closed document they fail to compile rather than to decode.

What consumers need: fabrikt's kotlinx models need kotlinx-datetime, jakarta.validation-api and the kotlin.time.ExperimentalTime opt-in. The harness sets all three, as a consumer would.

Our seven generators: output unchanged

Every generator here reads the document back through one collapse step. The step turns anyOf [{type, enum}, {type}] back into {type, enum, x-basecamp-open-enum}, keeping key order:

SDKCollapse step
TypeScripttypescript/scripts/open-enum-collapse.ts
Rubywire_formats.rb
Pythongen_common.py
KotlinOpenEnumCollapse.kt
SwiftOpenEnumCollapse.swift
Rustrust/generator/src/open_enum.rs
Go (for oapi-codegen)scripts/collapse-open-enums (jq, via scripts/go-oapi-codegen)
  • One contract for all seven. Each implementation is tested against the shared contract in openapi-consumers/open-enum-collapse.json. Besides the collapse itself, every implementation refuses the same 8 malformed wrappers, including a marker of 1 or "true" instead of the boolean true.
  • No generated file changed. After make generate (and make kt-generate-services again after rebasing onto #1140's current head), the generated SDK sources are byte-identical. The one intended difference is typescript/src/generated/openapi-stripped.json, whose contentEncoding moves to binary (it mirrors the document). Generated timestamps were reverted.
  • The tool catalog is unchanged too. go/pkg/basecamp/catalog/catalog.json (basecamp-cli's MCP schemas) reads through the same jq collapse in make catalog / catalog-check, so it is byte-identical to #1140's head. gen-catalog now refuses the raw anyOf form rather than silently publishing it.
  • Only the collapse reads enum. make check-open-enum-readers is a new spec-gates step. It finds every file that reads an enum key: 26 today, each classified in openapi-consumers/open-enum-readers.json as collapse, downstream of a collapse, wire-form test, collapsed-view test, or unrelated. It fails on an unlisted reader, a stale entry, or an entry point (16) that no longer names its collapse. What it can't see: an enum read spelled some way its pattern doesn't match. The drift checks, which regenerate from the anyOf form, cover the behavior instead.
  • Fixture validation still holds. check-fixture-coverage still rejects a wrong-typed open-enum value. Its validator treats anyOf as at-least-one, so an integer status matches no branch.

Known warnings, not changed here

swift-openapi-generator warns about two things. Both are pre-existing, and decoding is correct either way.

  • nullable: true on 18 members. scripts/enhance-openapi-go-types.sh writes these; it is an OpenAPI 3.0 keyword, which the generator translates.
  • {"type": "null"} inside an anyOf, on Draft.parent and TemplateLibraryCardTables.kanban_boardset. The generator skips it but still decodes null.

Moving those to 3.1 form changes what oapi-codegen generates for Go, so it is a separate change.

Label

enhancement: the SDKs' APIs and generated code are unchanged. Readers of openapi.json outside this repo that read a top-level enum off these 10 schemas will see a different shape. If you count that as breaking, relabel; before 1.0 both labels mean a minor release.

Checks run locally

make check came back green except for rs-lint (cargo fmt), which I fixed. After the rebase and the catalog change I ran these again, and they pass:

  • rs-lint
  • kt-check-drift
  • kt-check-optional-arrays-and-scalars
  • check-open-enum-readers
  • go-check-drift
  • catalog-check
  • make lint-actions (actionlint, zizmor)
  • openapi-consumers/run all

Not from this change: Bundler Audit, npm Audit and Trivy (Ruby SDK) are red on dependency advisories, and GitHub Actions audit is red on a shellcheck SC2337 warning in existing steps (test.yml lines that predate this PR, release-kotlin.yml, release-typescript.yml).

Outside review

I ran the dual-model review script on 32aaf98b and fixed what it found in 0d2316d4: the Swift job's toolchain, the fan-in, the harness's --spec handling, verifying a jar before caching it, and fresh inside $(...); Swift's collapse taking a JSON 1 as true; Rust's contract test ignoring key order; the Swift project test not checking the enum branches; the SPEC text on openapi-generator's types; and two enum read spellings plus Swift's Generated/ in the reader check. One I declined: overlapping run invocations in one checkout share output paths. The script now says to run one at a time; CI runs each job in its own checkout.