Skip to content

Emit oneOf for rfl::TaggedUnion in JSON schema, add VariantsAsOneOf - #14

Open
clockblocker wants to merge 2 commits into
mainfrom
json-schema-variant-one-of
Open

clockblocker wants to merge 2 commits into
mainfrom
json-schema-variant-one-of

Conversation

@clockblocker

@clockblocker clockblocker commented Nov 13, 2026 •

Copy link
Copy Markdown
Owner

AI-authored

Issue: munich-data-engineering#375

JSON schema now emits oneOf for rfl::TaggedUnion. Every alternative carries a different tag, so a value matches exactly one of them.

The internal parsing::schema::Type::AnyOf gets a mutually_exclusive_ flag. Only the JSON schema writer reads it, so avro and capnproto output stays the same.

By default rfl::Variant and std::variant stay anyOf, because oneOf would reject data that reflect-cpp reads fine:

Type Valid value Matches
rfl::Variant<int, double> 1 integer and number
rfl::Variant<Square, Rectangle> {"width":1,"height":2} both (no additionalProperties: false)
rfl::Variant<rfl::Field<"a",A>, rfl::Field<"b",B>> + DefaultIfMissing {"a":{}} both (required is empty)

std::optional and rfl::Generic stay anyOf in all cases: std::optional<rfl::Generic> would match null twice, and rfl::Generic has integer and number.

New processor: rfl::json::to_schema<T, rfl::VariantsAsOneOf>() emits oneOf for rfl::Variant and std::variant too, for users who know their alternatives don't overlap.
Behaviour change: rfl::TaggedUnion schemas say oneOf where they used to say anyOf.

Tests: test_json_schema{,2,3,4} updated, new test_json_schema_one_of (with and without the processor). JSON tests pass except test_default_val_throwing_initializer_is_an_error_when_reading, which fails the same way on main after #779.

🤖 Generated with Claude Code

@clockblocker clockblocker changed the title Emit oneOf for rfl::TaggedUnion in JSON schema Emit oneOf for rfl::TaggedUnion in JSON schema, add VariantsAsOneOf Nov 13, 2026
@clockblocker
clockblocker force-pushed the json-schema-variant-one-of branch from be31c3b to 2509cc2 Compare November 16, 2026 05:18
@clockblocker
clockblocker force-pushed the json-schema-variant-one-of branch from 2509cc2 to d145caf Compare November 25, 2026 12:08
clockblocker and others added 2 commits November 27, 2026 21:12
Alternatives of a tagged union differ in their tag, so a value can match
at most one of them. Mark the internal AnyOf as mutually exclusive and
emit it as oneOf in JSON schema. Untagged variants and std::optional stay
anyOf: their alternatives can overlap (integer is a subset of number,
structs allow additional properties), and oneOf would reject valid data.

Refs munich-data-engineering#375

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Passing rfl::VariantsAsOneOf to rfl::json::to_schema makes rfl::Variant
and std::variant emit oneOf instead of anyOf. The default stays anyOf,
since alternatives can overlap and oneOf rejects such values.
std::optional and rfl::Generic stay anyOf either way: optional is not a
variant, and the alternatives of rfl::Generic overlap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@clockblocker
clockblocker force-pushed the json-schema-variant-one-of branch from d145caf to c372938 Compare November 27, 2026 20:23
clockblocker added a commit that referenced this pull request Nov 28, 2026
- P0-1: Eliminate flattened_field_offsets<T>() and unconstructed dummy[sizeof(T)]
  entirely; extract live-object field pointers directly in write(),
  read_with_default(), and to_schema() (skipping extraction when _defaults ==
  nullptr), add std::launder on destroy_field_erased, and reject aggregates
  with reference members in both C++20 (NonRefAggProbe) and C++26
  (std::meta::is_reference).
- P0-2: Replace all unevaluated-context generic lambdas in TableStructParser
  and DirectFieldNames with named visitor structs, and replace all 5 consteval
  make_table lambdas with direct static consteval pack-expansion helpers.
- P1-1: Add use_table_struct_parser_ = true to boost_serialization/Parser.hpp
  (10/10 built-in NamedTuple specializations opted in).
- P1-2: Gate Parser_named_tuple.hpp's use_table_struct_parser_ on
  R::UnsizedObjectReader so external ViewType-inspecting custom readers stay
  on NamedTupleParser without breaking; also catch exceptions inside
  NamedTupleParser::read_object_or_array_with_default's noexcept boundary.
- P1-3: Replace global #ifndef RFL_TABLE_PARSER_MIN_FIELDS macro definition
  with internal constexpr kTableStructParserMinFields (with temporary TODO(#7)
  sweep hook).
- P1-4 & Rule #14: Add direct <concepts>, <new>, and fail_fast_v.hpp includes
  in TableStructParser.hpp, remove unused is_field.hpp in DirectFieldNames.hpp,
  and align parsing helpers to namespace rfl::parsing (zero rfl::internal::parsing
  or detail namespaces, avoiding shadowing rfl::internal across include/rfl/parsing/).
- P1-5: Launder &t on return from Parser_default::read_struct_with_default
  after const-subobject replacement.
- P1-6: Expand test_descriptor_table_stress.cpp with N == 64 bitmask boundary
  (Wide64Fields), Flatten<EmptyStruct>, const Flatten<InnerPlain>, "___" /
  "a__b" case transforms, AllowRawPtrs wrapper pointers, reference-member
  rejection, ExternalViewTypeReader, rfl::env, and NamedTuple/Field fallbacks.
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.

1 participant