codegen: fix OneOf alias handling with wrappers #3787
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Fix generation for OneOf unions involving alias user types, especially when struct:pkg:path places the member in a different package.
Rationale
In Go, an interface that has an unexported method can only be implemented by types in the same package. Previously the union marker method could end up associated with a member type that lives in a different package than the union interface, which makes it impossible for that member to satisfy the interface. In addition, when multiple union branches share the same underlying type, generated type switches can produce duplicate cases.
This change generates a small, per-branch wrapper type in the package where the union interface lives (or a common shared package for shared user-type unions) and attaches the marker method to that wrapper. The wrapper is a distinct type defined locally, so it can satisfy the interface without cross-package unexported methods, and it gives each branch a unique concrete type for type switches. When a union member already lives in the same package and is unique, the marker is attached directly to the member to preserve existing pointer vs value receiver semantics.
Changes
Compatibility
No DSL changes. Only generated code changes; behavior remains the same for unions that did not require wrappers.
Testing
All existing Goa tests pass locally.