This repository was archived by the owner on Apr 14, 2026. It is now read-only.
Commit b5a25d0
feat: replace oneOf/anyOf with json.RawMessage union + typed accessors
Replace the try-all-variants pointer-per-field union pattern with the
original oapi-codegen's proven json.RawMessage approach. Union types now
have a private 'union json.RawMessage' field with typed As/From/Merge
accessor methods for each variant.
Key changes:
- Union structs use json.RawMessage instead of pointer-per-variant
- Generate As<Variant>(), From<Variant>(), Merge<Variant>() for each member
- Add discriminator support: Discriminator() extracts the value from
union data, ValueByDiscriminator() routes to the correct As method
- Discriminator mapping supports both explicit (spec mapping) and
implicit (inferred from $ref schema names)
- Partial mappings handled gracefully (warn, don't error)
- Add JSONMerge helper (template + runtime) for Merge methods
- Use 'any' not 'interface{}' for ValueByDiscriminator return type
Generated code pattern:
type Pet struct { union json.RawMessage }
func (t Pet) AsCat() (Cat, error)
func (t *Pet) FromCat(v Cat) error
func (t *Pet) MergeCat(v Cat) error
func (t Pet) Discriminator() (string, error) // when present
func (t Pet) ValueByDiscriminator() (any, error) // when mapped
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>1 parent d066c9e commit b5a25d0
18 files changed
Lines changed: 3172 additions & 2001 deletions
File tree
- experimental
- codegen/internal
- templates
- files/helpers
- test
- components
- composition/output
- default_values/output
- comprehensive/output
- name_conflict_resolution
- inline_identifiers/output
- output
- output_options/name_normalizer/output
- runtime/helpers
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
413 | 413 | | |
414 | 414 | | |
415 | 415 | | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
416 | 419 | | |
417 | 420 | | |
418 | 421 | | |
| |||
439 | 442 | | |
440 | 443 | | |
441 | 444 | | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
442 | 466 | | |
443 | 467 | | |
444 | 468 | | |
| |||
1012 | 1036 | | |
1013 | 1037 | | |
1014 | 1038 | | |
1015 | | - | |
1016 | | - | |
1017 | | - | |
1018 | | - | |
1019 | | - | |
| 1039 | + | |
1020 | 1040 | | |
1021 | | - | |
1022 | | - | |
1023 | | - | |
| 1041 | + | |
| 1042 | + | |
| 1043 | + | |
| 1044 | + | |
| 1045 | + | |
| 1046 | + | |
| 1047 | + | |
| 1048 | + | |
1024 | 1049 | | |
1025 | | - | |
| 1050 | + | |
| 1051 | + | |
| 1052 | + | |
| 1053 | + | |
| 1054 | + | |
| 1055 | + | |
| 1056 | + | |
| 1057 | + | |
| 1058 | + | |
| 1059 | + | |
| 1060 | + | |
| 1061 | + | |
1026 | 1062 | | |
1027 | 1063 | | |
1028 | 1064 | | |
| |||
1034 | 1070 | | |
1035 | 1071 | | |
1036 | 1072 | | |
1037 | | - | |
1038 | 1073 | | |
1039 | 1074 | | |
1040 | | - | |
1041 | | - | |
1042 | | - | |
1043 | | - | |
1044 | | - | |
1045 | | - | |
| 1075 | + | |
| 1076 | + | |
| 1077 | + | |
| 1078 | + | |
| 1079 | + | |
| 1080 | + | |
| 1081 | + | |
| 1082 | + | |
1046 | 1083 | | |
1047 | | - | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
| 1095 | + | |
1048 | 1096 | | |
1049 | 1097 | | |
1050 | 1098 | | |
| |||
1095 | 1143 | | |
1096 | 1144 | | |
1097 | 1145 | | |
| 1146 | + | |
| 1147 | + | |
| 1148 | + | |
| 1149 | + | |
| 1150 | + | |
| 1151 | + | |
| 1152 | + | |
| 1153 | + | |
1098 | 1154 | | |
1099 | 1155 | | |
1100 | | - | |
| 1156 | + | |
1101 | 1157 | | |
| 1158 | + | |
1102 | 1159 | | |
1103 | 1160 | | |
1104 | 1161 | | |
1105 | 1162 | | |
1106 | 1163 | | |
1107 | | - | |
| 1164 | + | |
1108 | 1165 | | |
| 1166 | + | |
1109 | 1167 | | |
1110 | 1168 | | |
1111 | 1169 | | |
| |||
1124 | 1182 | | |
1125 | 1183 | | |
1126 | 1184 | | |
1127 | | - | |
| 1185 | + | |
1128 | 1186 | | |
1129 | 1187 | | |
1130 | 1188 | | |
1131 | 1189 | | |
1132 | 1190 | | |
1133 | | - | |
1134 | | - | |
| 1191 | + | |
1135 | 1192 | | |
1136 | 1193 | | |
1137 | 1194 | | |
1138 | 1195 | | |
1139 | 1196 | | |
1140 | | - | |
1141 | | - | |
1142 | | - | |
1143 | | - | |
| 1197 | + | |
| 1198 | + | |
| 1199 | + | |
| 1200 | + | |
| 1201 | + | |
1144 | 1202 | | |
1145 | 1203 | | |
1146 | 1204 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
619 | 620 | | |
620 | 621 | | |
621 | 622 | | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
622 | 659 | | |
623 | 660 | | |
624 | 661 | | |
| |||
0 commit comments