alexjomin / openapi-parser

Simple and still naive openapi documentation generator from comments of your Go code.
18 stars 8 forks source link

Handle anonymous structs #37

Closed Sadzeih closed 3 years ago

Sadzeih commented 3 years ago

Problem

With a type which contains an array of anonymous struct:

type uploadBatchBody struct {
    Data []struct {
        CreativeID string `json:"creative_id"`
    } `json:"data"`
}

We get a segfault:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x58 pc=0x1185d82]
goroutine 1 [running]:
github.com/alexjomin/openapi-parser/docparser.parseNamedType(0xc00021dc00, 0x12c2800, 0xc000253a10, 0x0, 0x0, 0x0, 0x0)
    /Users/user/go/pkg/mod/github.com/alexjomin/openapi-parser@v1.3.0/docparser/parser.go:106 +0x7e2
github.com/alexjomin/openapi-parser/docparser.(*openAPI).parseStructs(0xc00009f2b0, 0xc00021dc00, 0xc00026a3a0, 0x1b, 0xc00045d820)
    /Users/user/go/pkg/mod/github.com/alexjomin/openapi-parser@v1.3.0/docparser/model.go:403 +0x5b0
github.com/alexjomin/openapi-parser/docparser.(*openAPI).parseSchemas(0xc00009f2b0, 0xc00021dc00)
    /Users/user/go/pkg/mod/github.com/alexjomin/openapi-parser@v1.3.0/docparser/model.go:486 +0x648
github.com/alexjomin/openapi-parser/docparser.(*openAPI).Parse.func1(0xc0002af140, 0x33, 0x12c45c0, 0xc000268340, 0x0, 0x0, 0x18, 0xc000347600)
    /Users/user/go/pkg/mod/github.com/alexjomin/openapi-parser@v1.3.0/docparser/model.go:254 +0xc6
path/filepath.walk(0xc0002af140, 0x33, 0x12c45c0, 0xc000268340, 0xc000347b10, 0x0, 0x0)
    /usr/local/Cellar/go/1.14/libexec/src/path/filepath/path.go:360 +0x425
path/filepath.walk(0xc0003f8ba0, 0x1a, 0x12c45c0, 0xc0002fcea0, 0xc000347b10, 0x0, 0x0)
    /usr/local/Cellar/go/1.14/libexec/src/path/filepath/path.go:384 +0x2ff
path/filepath.walk(0xc0003f8b40, 0x11, 0x12c45c0, 0xc0002fcdd0, 0xc000347b10, 0x0, 0x0)
    /usr/local/Cellar/go/1.14/libexec/src/path/filepath/path.go:384 +0x2ff
path/filepath.walk(0xc00023be80, 0xc, 0x12c45c0, 0xc0002f1e10, 0xc000347b10, 0x0, 0x0)
    /usr/local/Cellar/go/1.14/libexec/src/path/filepath/path.go:384 +0x2ff
path/filepath.walk(0xc00023be1a, 0x3, 0x12c45c0, 0xc0002f1ba0, 0xc000347b10, 0x0, 0x0)
    /usr/local/Cellar/go/1.14/libexec/src/path/filepath/path.go:384 +0x2ff
path/filepath.walk(0x126b364, 0x1, 0x12c45c0, 0xc00009f380, 0xc000347b10, 0x0, 0x16007d0)
    /usr/local/Cellar/go/1.14/libexec/src/path/filepath/path.go:384 +0x2ff
path/filepath.Walk(0x126b364, 0x1, 0xc0000c5b10, 0x1258a20, 0xc0000c5b01)
    /usr/local/Cellar/go/1.14/libexec/src/path/filepath/path.go:406 +0xff
github.com/alexjomin/openapi-parser/docparser.(*openAPI).Parse(0xc00009f2b0, 0x126b364, 0x1, 0x1472ad8, 0x0, 0x0, 0x126bcfc, 0x6)
    /Users/user/go/pkg/mod/github.com/alexjomin/openapi-parser@v1.3.0/docparser/model.go:260 +0x93
github.com/alexjomin/openapi-parser/cmd.glob..func2(0x1445c00, 0x1472ad8, 0x0, 0x0)
    /Users/user/go/pkg/mod/github.com/alexjomin/openapi-parser@v1.3.0/cmd/root.go:28 +0x19d
github.com/spf13/cobra.(*Command).execute(0x1445c00, 0xc00009a1c0, 0x0, 0x0, 0x1445c00, 0xc00009a1c0)
    /Users/user/go/pkg/mod/github.com/spf13/cobra@v0.0.5/command.go:830 +0x29d
github.com/spf13/cobra.(*Command).ExecuteC(0x1445c00, 0x1040f4a, 0x142fd40, 0xc000000300)
    /Users/user/go/pkg/mod/github.com/spf13/cobra@v0.0.5/command.go:914 +0x2fb
github.com/spf13/cobra.(*Command).Execute(...)
    /Users/user/go/pkg/mod/github.com/spf13/cobra@v0.0.5/command.go:864
github.com/alexjomin/openapi-parser/cmd.Execute()
    /Users/user/go/pkg/mod/github.com/alexjomin/openapi-parser@v1.3.0/cmd/root.go:40 +0x31
main.main()
    /Users/user/go/pkg/mod/github.com/alexjomin/openapi-parser@v1.3.0/main.go:6 +0x20

And if we just have the anonymous struct:

type uploadBatchBody struct {
    Data struct {
        CreativeID string `json:"creative_id"`
    } `json:"data"`
}

Get get a parsing error, but no crash:

ERRO[0000] Can't parse the type of field in struct       error="expr (&{%!s(token.Pos=369) %!s(*ast.FieldList=&{376 [0xc00016adc0] 436}) %!s(bool=false)}) not yet unsupported" field=Data

Potential solution

An anonymous struct could be handled like so:

// @openapi:schema
type SomeStruct struct {
    Data struct {
        ID string `json:"id"`
    } `json:"data"`
}

Outputs:

SomeStruct:
    type: object
    properties:
        data:
            type: object
            properties:
                id:
                    type: string
Sadzeih commented 3 years ago

Since my team needs this feature, I'll be working on it.

Do you have any better ideas than my potential solution @denouche