You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs: add cross-language comparison for oneOf/anyOf/allOf handling
Added comprehensive comparison showing how different strongly-typed
languages handle these OpenAPI constructs:
- Java: Interface/inheritance approach, treats anyOf as oneOf
- TypeScript: Union/intersection types (most natural fit)
- Go: Explicit struct with pointers and custom unmarshaling
- C#: Abstract classes and polymorphic serialization
- Python: Union types with runtime validation
- Swift: Enums with associated values (perfect for oneOf)
Key findings:
- No language correctly implements anyOf semantics (one or more)
- Most treat anyOf identical to oneOf (exactly one)
- TypeScript's type system is best suited for these constructs
- Rust's proposed struct approach is reasonable given its constraints
This comparison validates that the anyOf problem is industry-wide,
not specific to Rust, and that our proposed solution is pragmatic.
1.**No language perfectly handles anyOf**: Most treat it identical to oneOf, missing the "one or more" semantics
740
+
2.**TypeScript has the best model**: Union and intersection types naturally express these concepts
741
+
3.**Swift enums are ideal for oneOf**: Associated values provide perfect type safety
742
+
4.**Go's approach is explicit**: No magic, clear what's happening, verbose but correct
743
+
5.**Dynamic languages rely on runtime validation**: Python/Ruby validate at runtime, not compile time
744
+
745
+
### Why Rust's Proposed Approach Makes Sense
746
+
747
+
Given Rust's type system constraints:
748
+
-**No union types**: Can't do TypeScript-style unions
749
+
-**No inheritance**: Can't do Java/C# style class hierarchies
750
+
-**Strong type safety**: Want compile-time guarantees
751
+
752
+
The proposed struct-with-optional-fields for anyOf:
753
+
-**Explicit about semantics**: Clear that multiple can be set
754
+
-**Type safe**: Compiler enforces field types
755
+
-**Serde compatible**: Works with existing serialization
756
+
-**Migration path**: Different from enum, so existing code breaks loudly (good!)
757
+
518
758
## Conclusion
519
759
520
-
The current Rust generator makes pragmatic choices favoring simplicity and Rust idioms over strict OpenAPI compliance. The proposed changes in PR #21915 move toward semantic correctness while maintaining reasonable ergonomics. Future work should focus on incremental improvements guided by user needs and Rust ecosystem evolution.
760
+
The current Rust generator makes pragmatic choices favoring simplicity and Rust idioms over strict OpenAPI compliance. The proposed changes in PR #21915 move toward semantic correctness while maintaining reasonable ergonomics.
761
+
762
+
Comparing with other languages shows that:
763
+
1. No language has solved anyOf perfectly
764
+
2. Rust's constraints (no unions, no inheritance) require creative solutions
765
+
3. The proposed struct approach for anyOf is reasonable given these constraints
766
+
4. Future work should focus on incremental improvements guided by user needs and Rust ecosystem evolution
0 commit comments