goas icon indicating copy to clipboard operation
goas copied to clipboard

Type Alias + Type Embedding causes panic:

Open markus-wa opened this issue 3 years ago • 0 comments

Hi @mikunalpha - thanks for this project

I'm hitting an issue when combining type aliasing with type embedding (both of these on their own are fine)

my minimal reproduction main.go

package main

import "fmt"

// @Version 1.0.0
// @Title Backend API

func main() {
	fmt.Println("hi")
}

type C struct {
	ID int
}

type B C

type A struct {
	B
}

// @Title Get comment list of a match.
// @Success  200  array  A  "A JSON"
// @Route /match/get_comments [post]
func something() {
}

with this spec, calling goas fails with the below error

$ goas
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x666451]

goroutine 1 [running]:
github.com/iancoleman/orderedmap.(*OrderedMap).Keys(...)
        /home/mark/go/pkg/mod/github.com/iancoleman/[email protected]/orderedmap.go:80
main.(*parser).parseSchemaPropertiesFromStructFields(0xc00014c6e0, 0xc0000c2620, 0x8, 0xc0000c2828, 0x3, 0xc00017e000, 0xc0000c6120, 0x1, 0x1)
        /home/mark/go/pkg/mod/github.com/mikunalpha/[email protected]/parser.go:1215 +0x1d71
main.(*parser).parseSchemaObject(0xc00014c6e0, 0xc0000c2620, 0x8, 0xc0000c2828, 0x3, 0xc0000be199, 0x1, 0x548e00, 0xc0000cd360, 0x6e1c1a)
        /home/mark/go/pkg/mod/github.com/mikunalpha/[email protected]/parser.go:947 +0x1010
main.(*parser).registerType(0xc00014c6e0, 0xc0000c2620, 0x8, 0xc0000c2828, 0x3, 0xc0000be199, 0x1, 0x68ce60, 0x7fcd431ee501, 0xc0000c29b8, ...)
        /home/mark/go/pkg/mod/github.com/mikunalpha/[email protected]/parser.go:829 +0x16c
main.(*parser).parseResponseComment(0xc00014c6e0, 0xc0000c2620, 0x8, 0xc0000c2828, 0x3, 0xc0000c8a80, 0xc0000be18d, 0x17, 0x8, 0x3)
        /home/mark/go/pkg/mod/github.com/mikunalpha/[email protected]/parser.go:761 +0x4f5
main.(*parser).parseOperation(0xc00014c6e0, 0xc0000c2620, 0x8, 0xc0000c2828, 0x3, 0xc0000da980, 0x3, 0x4, 0xc0000daab0, 0xc0000c2620)
        /home/mark/go/pkg/mod/github.com/mikunalpha/[email protected]/parser.go:587 +0x6ab
main.(*parser).parsePaths(0xc00014c6e0, 0x0, 0x0)
        /home/mark/go/pkg/mod/github.com/mikunalpha/[email protected]/parser.go:547 +0x129
main.(*parser).parseAPIs(0xc00014c6e0, 0x0, 0x0)
        /home/mark/go/pkg/mod/github.com/mikunalpha/[email protected]/parser.go:422 +0x85
main.(*parser).CreateOASFile(0xc00014c6e0, 0x6e7e45, 0x8, 0x0, 0x0)
        /home/mark/go/pkg/mod/github.com/mikunalpha/[email protected]/parser.go:205 +0xd7
main.action(0xc00014c580, 0xc00014c580, 0xc0000cd090)
        /home/mark/go/pkg/mod/github.com/mikunalpha/[email protected]/main.go:45 +0x1d6
github.com/urfave/cli.HandleAction(0x690860, 0x701410, 0xc00014c580, 0xc00014c580, 0x0)
        /home/mark/go/pkg/mod/github.com/urfave/[email protected]/app.go:524 +0x105
github.com/urfave/cli.(*App).Run(0xc00015c000, 0xc0000a01f0, 0x1, 0x1, 0x0, 0x0)
        /home/mark/go/pkg/mod/github.com/urfave/[email protected]/app.go:286 +0x675
main.main()
        /home/mark/go/pkg/mod/github.com/mikunalpha/[email protected]/main.go:63 +0x165

if I change the definition of type A to

type A struct {
	C
}

it works like a charm

but unfortunately I'm working on a big existing project and changing all these cases is probably not feasible.

I'd be happy to make a PR myself with a fix if you prefer, but may need some direction on how to go about that :slightly_smiling_face:

markus-wa avatar May 19 '21 13:05 markus-wa