Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
avinashbhashyam-rs opened this issue Mar 8, 2024 · 0 comments · Fixed by #3491
Closed

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

avinashbhashyam-rs opened this issue Mar 8, 2024 · 0 comments · Fixed by #3491

Comments

@avinashbhashyam-rs
Copy link
Contributor

avinashbhashyam-rs commented Mar 8, 2024

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant