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

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

Open
DavidArchibald opened this issue Jun 27, 2024 · 1 comment
Open

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

DavidArchibald opened this issue Jun 27, 2024 · 1 comment

Comments

@DavidArchibald
Copy link

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
@AnatolyRugalev
Copy link
Contributor

I looked into this, and it seems that the yaml package that you are using is to blame:

gopkg.in/yaml%2ev3.(*encoder).marshal(0x14000120c08, {0x0, 0x0}, {0x1010ce540?, 0x140001138d0?, 0x98?})
	/Users/anatoliirugalev/go/pkg/mod/gopkg.in/[email protected]/encode.go:140 +0x704

Specifically, this code:

	case Marshaler:
		v, err := value.MarshalYAML()
		if err != nil {
			fail(err)
		}

It assumes that value will be a pointer, but it's not guaranteed. Here's the issue in yaml.v3: go-yaml/yaml#747

I don't think there's anything to be done in kin-openapi. I would suggest you to switch to a different library, as yaml.v3 seems abandoned. For example: https://pkg.go.dev/github.com/goccy/go-yaml

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

No branches or pull requests

2 participants