Skip to content

Nil Info in openapi.T causes panic when marshalled #972

@DavidArchibald

Description

@DavidArchibald

I discovered a panic through a test where the Info was missing. I understand it's a required field but I would have expected an error from the loader, not a panic when marshalling.

This is roughly how I discovered it:

loader := &openapi3.Loader{}
doc, err := loader.LoadFromData([]byte("{}"))
require.NoError(t, err) // Doesn't error

// This panics on v0.125.0 but not v0.124.0
yaml.Marshal(doc)

This is of course simplified. In my actual code I have some transformations going on between that and it loads from a file. If all you want to do is reproduce the panic you can do yaml.Marshal(&openapi3.Loader{}). Interestingly yaml.Marshal(openapi3.Loader{}) still doesn't panic.

Here's the relevant stacktrace, trimmed down a little:

panic({0x16ba440?, 0xc00005a3d0?})
	/usr/local/go/src/runtime/panic.go:770 +0x132
gopkg.in/yaml%2ev3.handleErr(0xc000149c50)
	gopkg.in/yaml.v3@v3.0.1/yaml.go:294 +0x6d
panic({0x16ba440?, 0xc00005a3d0?})
	/usr/local/go/src/runtime/panic.go:770 +0x132
github.com/getkin/kin-openapi/openapi3.(*Info).MarshalYAML(0x1688540?)
	<autogenerated>:1 +0x8c
gopkg.in/yaml%2ev3.(*encoder).marshal(0xc0004d4c08, {0x0, 0x0}, {0x1688540?, 0xc00005a3c0?, 0x98?})
	gopkg.in/yaml.v3@v3.0.1/encode.go:140 +0x897
gopkg.in/yaml%2ev3.(*encoder).marshal.(*encoder).mapv.func1()
	gopkg.in/yaml.v3@v3.0.1/encode.go:192 +0xe5
gopkg.in/yaml%2ev3.(*encoder).mappingv(0xc0004d4c08, {0x0?, 0x0}, 0xc0001499b0)
	gopkg.in/yaml.v3@v3.0.1/encode.go:265 +0x14a
gopkg.in/yaml%2ev3.(*encoder).mapv(...)
	gopkg.in/yaml.v3@v3.0.1/encode.go:187
gopkg.in/yaml%2ev3.(*encoder).marshal(0xc0004d4c08, {0x0, 0x0}, {0x16a60c0?, 0xc000382ab0?, 0x1?})
	gopkg.in/yaml.v3@v3.0.1/encode.go:164 +0x7d4
gopkg.in/yaml%2ev3.(*encoder).marshal(0xc0004d4c08, {0x0, 0x0}, {0x1872d00?, 0xc000404240?, 0xc000149bd8?})
	gopkg.in/yaml.v3@v3.0.1/encode.go:148 +0x926
gopkg.in/yaml%2ev3.(*encoder).marshalDoc(0xc0004d4c08, {0x0, 0x0}, {0x1872d00?, 0xc000404240?, 0x0?})
	gopkg.in/yaml.v3@v3.0.1/encode.go:105 +0x12e
gopkg.in/yaml%2ev3.Marshal({0x1872d00, 0xc000404240})
	gopkg.in/yaml.v3@v3.0.1/yaml.go:222 +0x326
[my test]
	main_test.go:85 +0x7f

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions