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

Generated code for nested inline attributes doesn't compile #3438

Closed
markbrockhoff opened this issue Jan 4, 2024 · 2 comments · Fixed by #3442
Closed

Generated code for nested inline attributes doesn't compile #3438

markbrockhoff opened this issue Jan 4, 2024 · 2 comments · Fixed by #3442

Comments

@markbrockhoff
Copy link

Hi, I'm trying to build my first Goa Api and ran into an issue when defining a nested attribute inside a type.

The type I'm defining in my design looks like this:

var Project = Type("Project", func() {
	Attribute("id", String, func() {
		Example("123456789")
	})
	Attribute("title", String, func() {
		Example("Test Project")
	})
	Attribute("createdAt", String, func() {
		Format(FormatDateTime)
		Example("2024-01-02T13:59:43+01")
	})
	Attribute("projectDetails", func() {
		Attribute("status", String)
	})
	Required("id", "title", "createdAt")
})

After generating the code using goa gen I get the following function inside ./gen/http/projects/server/encode_decode.go:

// marshalProjectsProjectToProjectResponse builds a value of type
// *ProjectResponse from a value of type *projects.Project.
func marshalProjectsProjectToProjectResponse(v *projects.Project) *ProjectResponse {
	res := &ProjectResponse{
		ID:        v.ID,
		Title:     v.Title,
		CreatedAt: v.CreatedAt,
	}
	if v.ProjectDetails != nil {
		res.ProjectDetails = &struct {
			Status *string
		}{
			Status: v.ProjectDetails.Status,
		}
	}

	return res
}

Problem is that the assignment of the inline struct to res.ProjectDetails fails during compilation with the following error message:
cannot use &struct{Status *string}{…} (value of type *struct{Status *string}) as *struct{Status *string "form:\"status,omitempty\" json:\"status,omitempty\" xml:\"status,omitempty\""} value in assignment

I'm able to fix the issue by editing the function like this:

// marshalProjectsProjectToProjectResponse builds a value of type
// *ProjectResponse from a value of type *projects.Project.
func marshalProjectsProjectToProjectResponse(v *projects.Project) *ProjectResponse {
	res := &ProjectResponse{
		ID:        v.ID,
		Title:     v.Title,
		CreatedAt: v.CreatedAt,
	}
	if v.ProjectDetails != nil {
		res.ProjectDetails = &struct {
			Status *string "form:\"status,omitempty\" json:\"status,omitempty\" xml:\"status,omitempty\""
		}{
			Status: v.ProjectDetails.Status,
		}
	}

	return res
}

Is this a bug inside goa or do I need to do something differently inside my design? I'm pretty new to Goa so any help would be appreciated. :)

Btw.: I'm using Goa v3.14.1 and go v1.21.5

@raphael
Copy link
Member

raphael commented Jan 4, 2024

This looks like a bug, are you able to provide a minimal but complete Goa design that reproduces the issue?

@markbrockhoff
Copy link
Author

Thanks for the quick response.

Sure, here you go:

package design

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

var _ = API("goaBugRepro", func() {
	Title("Goa Bug Repro")
	Description("Minimal example to reproduce a bug found inside goa")
})

var _ = Service("bugService", func() {
	Method("createBug", func() {
		// Generation works if the result is just one Bug and not an array of Bugs.
		Result(ArrayOf(Bug))

		HTTP(func() {
			GET("/bug")
		})
	})
})

var Bug = Type("Bug", func() {
	Attribute("id", Int)
	Attribute("nested", func() {
		Attribute("nestedString", String)
		Attribute("nestedNumber", Int)
	})
})

While creating the repro I noticed that the bug only appears once I return an array of bugs as result of the method createBug. When just one Bug is returned, everything's fine.

raphael added a commit that referenced this issue Jan 8, 2024
Properly generate marshal tags when generating inline definitions of structs that are array elements.

Fix #3438
raphael added a commit that referenced this issue Jan 8, 2024
Properly generate marshal tags when generating inline definitions of structs that are array elements.

Fix #3438
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.

2 participants