kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

Nil `Info` in `openapi.T` causes panic when marshalled

Open DavidArchibald opened this issue 7 months ago • 1 comments

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/[email protected]/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/[email protected]/encode.go:140 +0x897
gopkg.in/yaml%2ev3.(*encoder).marshal.(*encoder).mapv.func1()
	gopkg.in/[email protected]/encode.go:192 +0xe5
gopkg.in/yaml%2ev3.(*encoder).mappingv(0xc0004d4c08, {0x0?, 0x0}, 0xc0001499b0)
	gopkg.in/[email protected]/encode.go:265 +0x14a
gopkg.in/yaml%2ev3.(*encoder).mapv(...)
	gopkg.in/[email protected]/encode.go:187
gopkg.in/yaml%2ev3.(*encoder).marshal(0xc0004d4c08, {0x0, 0x0}, {0x16a60c0?, 0xc000382ab0?, 0x1?})
	gopkg.in/[email protected]/encode.go:164 +0x7d4
gopkg.in/yaml%2ev3.(*encoder).marshal(0xc0004d4c08, {0x0, 0x0}, {0x1872d00?, 0xc000404240?, 0xc000149bd8?})
	gopkg.in/[email protected]/encode.go:148 +0x926
gopkg.in/yaml%2ev3.(*encoder).marshalDoc(0xc0004d4c08, {0x0, 0x0}, {0x1872d00?, 0xc000404240?, 0x0?})
	gopkg.in/[email protected]/encode.go:105 +0x12e
gopkg.in/yaml%2ev3.Marshal({0x1872d00, 0xc000404240})
	gopkg.in/[email protected]/yaml.go:222 +0x326
[my test]
	main_test.go:85 +0x7f

DavidArchibald avatar Jun 27 '24 18:06 DavidArchibald