goadesign / goa

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

Goa generating invalid code when OneOf types is a user defined type #3489

Closed avinashbhashyam-rs closed 6 months ago

avinashbhashyam-rs commented 6 months ago

When a ResultType had a field that is union type and one of the fields of the union type is a user type the goa is generating code that doesn't compile.

Design

package design

import (
    . "goa.design/goa/v3/dsl"
)

var _ = Service("Cars Service", func() {
    Description(`Test Cars Service`)

    Method("get_car", func() {
        Description("Fetches list of cars.")
        Payload(func() {
            Field(1, "ID", Int64, "ID of the car")
            Required("ID")
        })
        Result(Car)

        Error("internal")
        GRPC(func() {
            Response(CodeOK)
            Response("internal", CodeInternal)
        })
    })
})

var Car = ResultType("Car", "Car", func() {
    Description("Test Car")

    Field(1, "ID", Int64, "ID of the car")
    Field(2, "Name", String, "Name of the car")
    Field(3, "Type", CarType, "Model of the car")
    Field(4, "Year", Int, "Year of the car")

    Required("ID", "Name")
})

var CarType = Type("CarType", func() {
    Description("Car Type")

    OneOf("Type", func() {
        Field(1, "Sedan", Sedan, "Type of the car")
        Field(2, "SUV", SUV, "Type of the car")
    })

    Required("Type")
})

var Sedan = Type("Sedan", func() {
    Description("Sedan Type")

    Field(1, "DriveType", String, "Model of the car")
    Field(2, "Seats", Count, "Number of seats")

    Required("DriveType")
})

var SUV = Type("SUV", func() {
    Description("SUV Type")

    Field(1, "DriveType", String, "Model of the car")
    Field(2, "Seats", Count, "Number of seats")
    Field(3, "TowingCapacity", TowingCapacity, "Towing capacity of the car")

    Required("DriveType")
})

var Count = Type("Count", Int, func() {
    Description("Seat Count")
    Maximum(7)
    Minimum(2)
})

var TowingCapacity = Type("TowingCapacity", Int, func() {
    Description("Towing Capacity")
    Maximum(10000)
    Minimum(1000)
})

The generated code to transform the view to actual type and actual type to view is invalid

// transformCarsserviceviewsCarTypeViewToCarType builds a value of type
// *CarType from a value of type *carsserviceviews.CarTypeView.
func transformCarsserviceviewsCarTypeViewToCarType(v *carsserviceviews.CarTypeView) *CarType {
    if v == nil {
        return nil
    }
    res := &CarType{}
    if v.Type != nil {
        switch actual := v.Type.(type) {
        case *carsserviceviews.SedanView:
            res.Type = &Sedan{
                DriveType: *actual.DriveType,
            }
            if actual.Seats != nil {
                seats := Count(*actual.Seats)
                res.Type.Seats = &seats
            }

        case *carsserviceviews.SUVView:
            res.Type = &SUV{
                DriveType: *actual.DriveType,
            }
            if actual.Seats != nil {
                seats := Count(*actual.Seats)
                res.Type.Seats = &seats
            }
            if actual.TowingCapacity != nil {
                towingCapacity := TowingCapacity(*actual.TowingCapacity)
                res.Type.TowingCapacity = &towingCapacity
            }

        }
    }

    return res
}