Skip to content

Add support for if/then/else conditional schemas#76

Merged
cubahno merged 1 commit into
mainfrom
ig/if-then-else
Apr 27, 2026
Merged

Add support for if/then/else conditional schemas#76
cubahno merged 1 commit into
mainfrom
ig/if-then-else

Conversation

@cubahno
Copy link
Copy Markdown
Collaborator

@cubahno cubahno commented Apr 27, 2026

Adds code generation support for JSON Schema if/then/else keywords, which are available in OpenAPI 3.1.

Approach

  • Single branch (only then or only else): flat-merged into the parent struct as optional fields
  • Both branches: generates a runtime.Conditional[T, E] union wrapper with _Then and _Else named variant types

Why Conditional instead of Either

Either uses generic .A/.B fields and .IsA()/.IsB() methods. For if/then/else this is confusing - which one is "then"? Conditional uses .Then/.Else fields and .IsThen()/.IsElse() methods, making the semantics immediately clear.

Why no If field/method on Conditional

Go reserves if as a keyword - you can't have a field or method named If. More importantly, the if schema is purely a validation predicate (e.g. "if kind equals typeA") and contributes no structural properties to code generation. It's not meaningful at the type level.

Limitations:

  • The if schema is ignored - it's a validation concern, not structural
  • Validation-only branches (only required, no new properties, ...) produce an empty wrapper type - the required constraint is conditional and can't be statically enforced in Go types.

Closes #74

@cubahno cubahno marked this pull request as ready for review April 27, 2026 16:12
@cubahno cubahno merged commit 3e26e49 into main Apr 27, 2026
3 checks passed
@cubahno cubahno deleted the ig/if-then-else branch April 27, 2026 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Add Support For if/then/else

1 participant