goadesign / goa

🌟 Goa: Elevate Go API development! 🚀 Streamlined design, automatic code generation, and seamless HTTP/gRPC support. ✨
https://goa.design
MIT License
5.69k stars 560 forks source link

Latest Goa fails to codegen nested ResultType when Extend() is used #3598

Closed smessier closed 5 days ago

smessier commented 1 month ago

See file attachment for API that demonstrates the issue. foo.zip

We have had to change from using Type to ResultType for media with multiple views since Type no longer allows views after goa v3.17 Changing to ResultType exposes a weird issue having to do with nesting and extending ResultType. when top-level media includes a field that has an extended ResultType then the deepest ResultType fails to codegen properly in "foo/gen/http/bar/client/encode_decode.go" and "foo/gen/http/bar/server/encode_decode.go". the bug is the same in both cases:

// marshalBarviewsImmediateChildExtenderViewToImmediateChildExtenderResponseBody
// builds a value of type *ImmediateChildExtenderResponseBody from a value of
// type *barviews.ImmediateChildExtenderView.
func marshalBarviewsImmediateChildExtenderViewToImmediateChildExtenderResponseBody(v *barviews.ImmediateChildExtenderView) *ImmediateChildExtenderResponseBody {
    if v == nil {
        return nil
    }
    res := &ImmediateChildExtenderResponseBody{
        ID:   *v.ID,
        Name: *v.Name,
        More: v.More,
    }
    if v.DeepChild != nil {
        res.DeepChild = marshalBarviewsDeepChildToDeepChildResponseBody(v.DeepChild)  <--- wrong function name, missing "View"
    }

    return res
}

// marshalBarviewsDeepChildViewToDeepChildResponseBody builds a value of type
// *DeepChildResponseBody from a value of type *barviews.DeepChildView.
func marshalBarviewsDeepChildViewToDeepChildResponseBody(v *barviews.DeepChildView) *DeepChildResponseBody {
    if v == nil {
        return nil
    }
    res := &DeepChildResponseBody{
        ID:   *v.ID,
        Name: *v.Name,
    }

    return res
}

Note that this bug appears to have been present for many previous versions but is only seen when forced to change from using Type to ResultType. Not sure if having multiple views are required to reproduce but our use case has them.

Example of codegen:

~/gopath/src/foo$ goa version
Goa version v3.19.1
~/gopath/src/foo$ goa gen foo/design
gen/bar/client.go
...
raphael commented 1 week ago

Hello Scott!! Sorry it took so long to get to this. I have a potential fix on this branch: fix/deep_user_type_extend_views (https://github.com/goadesign/goa/pull/3608). Are you able to test the fix?

raphael commented 5 days ago

Merged the PR, issue should be fixed.