Skip to content

fix: conversion with a parameter schema ref#419

Closed
vbehar wants to merge 2 commits into
getkin:masterfrom
vbehar:fix-nil-pointer
Closed

fix: conversion with a parameter schema ref#419
vbehar wants to merge 2 commits into
getkin:masterfrom
vbehar:fix-nil-pointer

Conversation

@vbehar

@vbehar vbehar commented Sep 16, 2021

Copy link
Copy Markdown
Contributor

converting from v3 to v2 with an endpoint using a parameter with a schema ref used to fail with the following error:

--- FAIL: TestConvOpenAPIV3ToV2 (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x58 pc=0x100f71688]

goroutine 4 [running]:
testing.tRunner.func1.2({0x101035da0, 0x1012b6160})
	/opt/homebrew/Cellar/go/1.17/libexec/src/testing/testing.go:1209 +0x258
testing.tRunner.func1(0x14000117380)
	/opt/homebrew/Cellar/go/1.17/libexec/src/testing/testing.go:1212 +0x284
panic({0x101035da0, 0x1012b6160})
	/opt/homebrew/Cellar/go/1.17/libexec/src/runtime/panic.go:1038 +0x21c
github.com/getkin/kin-openapi/openapi2conv.FromV3Parameter(0x140001ce7c8, 0x1400007da18)
	/Users/vbehar/projects/oss/kin-openapi/openapi2conv/openapi2_conv.go:979 +0x138
github.com/getkin/kin-openapi/openapi2conv.FromV3Operation(0x1400007da00, 0x14000129c20)
	/Users/vbehar/projects/oss/kin-openapi/openapi2conv/openapi2_conv.go:895 +0x250
github.com/getkin/kin-openapi/openapi2conv.FromV3(0x1400007da00)
	/Users/vbehar/projects/oss/kin-openapi/openapi2conv/openapi2_conv.go:586 +0xddc
github.com/getkin/kin-openapi/openapi2conv.TestConvOpenAPIV3ToV2(0x14000117380)
	/Users/vbehar/projects/oss/kin-openapi/openapi2conv/openapi2_conv_test.go:26 +0x19c
testing.tRunner(0x14000117380, 0x1010870e8)
	/opt/homebrew/Cellar/go/1.17/libexec/src/testing/testing.go:1259 +0x104
created by testing.(*T).Run
	/opt/homebrew/Cellar/go/1.17/libexec/src/testing/testing.go:1306 +0x328
FAIL	github.com/getkin/kin-openapi/openapi2conv	0.840s
FAIL

This change fixes the issue by ensuring that we correctly handle a schema ref.

converting from v3 to v2 with an endpoint using a parameter with a schema ref used to fail with the following error:

```
--- FAIL: TestConvOpenAPIV3ToV2 (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x58 pc=0x100f71688]

goroutine 4 [running]:
testing.tRunner.func1.2({0x101035da0, 0x1012b6160})
	/opt/homebrew/Cellar/go/1.17/libexec/src/testing/testing.go:1209 +0x258
testing.tRunner.func1(0x14000117380)
	/opt/homebrew/Cellar/go/1.17/libexec/src/testing/testing.go:1212 +0x284
panic({0x101035da0, 0x1012b6160})
	/opt/homebrew/Cellar/go/1.17/libexec/src/runtime/panic.go:1038 +0x21c
github.com/getkin/kin-openapi/openapi2conv.FromV3Parameter(0x140001ce7c8, 0x1400007da18)
	/Users/vbehar/projects/oss/kin-openapi/openapi2conv/openapi2_conv.go:979 +0x138
github.com/getkin/kin-openapi/openapi2conv.FromV3Operation(0x1400007da00, 0x14000129c20)
	/Users/vbehar/projects/oss/kin-openapi/openapi2conv/openapi2_conv.go:895 +0x250
github.com/getkin/kin-openapi/openapi2conv.FromV3(0x1400007da00)
	/Users/vbehar/projects/oss/kin-openapi/openapi2conv/openapi2_conv.go:586 +0xddc
github.com/getkin/kin-openapi/openapi2conv.TestConvOpenAPIV3ToV2(0x14000117380)
	/Users/vbehar/projects/oss/kin-openapi/openapi2conv/openapi2_conv_test.go:26 +0x19c
testing.tRunner(0x14000117380, 0x1010870e8)
	/opt/homebrew/Cellar/go/1.17/libexec/src/testing/testing.go:1259 +0x104
created by testing.(*T).Run
	/opt/homebrew/Cellar/go/1.17/libexec/src/testing/testing.go:1306 +0x328
FAIL	github.com/getkin/kin-openapi/openapi2conv	0.840s
FAIL
```

This change fixes the issue by ensuring that we correctly handle a schema ref.
@vbehar

vbehar commented Sep 16, 2021

Copy link
Copy Markdown
Contributor Author

hum in fact I was too focused on the v3 to v2 conversion and missed that the v2 to v3 is also missing a fix here

@vbehar vbehar marked this pull request as draft September 16, 2021 14:31
@fenollp

fenollp commented Sep 17, 2021

Copy link
Copy Markdown
Collaborator

LGTM
You can also do the inverse conversion as another PR, if you want.

@fenollp

fenollp commented Dec 2, 2021

Copy link
Copy Markdown
Collaborator

(Disregard that previous comment).

Your test is not valid OpenAPIv2 per https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md#fixed-fields-7 or https://swagger.io/docs/specification/2-0/describing-parameters/#faq in that the schema field can only be used with parameters with in: body.

But the issue that the parameter schema $ref be used when defined is valid. I'm looking into it.

@fenollp fenollp marked this pull request as ready for review December 2, 2021 16:21
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

Successfully merging this pull request may close these issues.

2 participants