danielgtaylor / restish

Restish is a CLI for interacting with REST-ish HTTP APIs with some nice features built-in
https://rest.sh/
MIT License
923 stars 74 forks source link

Loading openapi schema with `default: null` generates `invalid memory address or nil pointer dereference` #254

Open filipebreda opened 6 months ago

filipebreda commented 6 months ago
restish --version
dev-2024-04-21-17:50

When hash := items.GoLow().Hash() is called, libopenapi considers !s.Default.IsEmpty() to be true, I assume bcause it is defined in the openapi file, proceeding to pass a nil value low.GenerateHashString(s.Default.Value).

--- FAIL: TestLoader (0.01s)
    --- FAIL: TestLoader/petstore (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=0x1 addr=0x98 pc=0x6f4623]

goroutine 80 [running]:
testing.tRunner.func1.2({0xc324a0, 0x13915b0})
    /usr/local/go/src/testing/testing.go:1545 +0x238
testing.tRunner.func1()
    /usr/local/go/src/testing/testing.go:1548 +0x397
panic({0xc324a0?, 0x13915b0?})
    /usr/local/go/src/runtime/panic.go:914 +0x21f
github.com/pb33f/libopenapi/datamodel/low.GenerateHashString({0x0, 0x0})
    /home/filipe/go/pkg/mod/github.com/pb33f/libopenapi@v0.9.7/datamodel/low/extraction_functions.go:707 +0x83
github.com/pb33f/libopenapi/datamodel/low/base.(*Schema).Hash(0xc0001b0000)
    /home/filipe/go/pkg/mod/github.com/pb33f/libopenapi@v0.9.7/datamodel/low/base/schema.go:249 +0x18fb
github.com/pb33f/libopenapi/datamodel/low/base.(*SchemaProxy).Hash(0xc000168b40)
    /home/filipe/go/pkg/mod/github.com/pb33f/libopenapi@v0.9.7/datamodel/low/base/schema_proxy.go:146 +0x105
github.com/pb33f/libopenapi/datamodel/low.GenerateHashString({0xc8bc80, 0xc000168b40})
    /home/filipe/go/pkg/mod/github.com/pb33f/libopenapi@v0.9.7/datamodel/low/extraction_functions.go:703 +0x1e2
github.com/pb33f/libopenapi/datamodel/low/base.(*Schema).Hash(0xc000436800)
    /home/filipe/go/pkg/mod/github.com/pb33f/libopenapi@v0.9.7/datamodel/low/base/schema.go:312 +0x2cdc
github.com/danielgtaylor/restish/openapi.renderSchemaInternal(0xc0003d2a00, {0x0, 0x0}, 0x0, 0xc069e0?)
    /home/filipe/Projects/restish/openapi/schema.go:170 +0x138d
github.com/danielgtaylor/restish/openapi.renderSchema(...)
    /home/filipe/Projects/restish/openapi/schema.go:56
github.com/danielgtaylor/restish/openapi.openapiOperation(_, {_, _}, _, _, _)
    /home/filipe/Projects/restish/openapi/openapi.go:516 +0x2af3
github.com/danielgtaylor/restish/openapi.loadOpenAPI3({0xee1080, 0xc0002309d0}, 0x58d629?, 0xc000618510, 0xd354ea?)
    /home/filipe/Projects/restish/openapi/openapi.go:618 +0x16a5
github.com/danielgtaylor/restish/openapi.(*loader).Load(_, {{0xd2089b, 0x4}, {0x0, 0x0}, 0x0, {0xd208a2, 0xf}, {0x0, 0x0}, ...}, ...)
    /home/filipe/Projects/restish/openapi/openapi.go:803 +0x19c
github.com/danielgtaylor/restish/openapi.TestLoader.func1(0xc0005429c0?)
    /home/filipe/Projects/restish/openapi/openapi_test.go:233 +0x466
testing.tRunner(0xc0005429c0, 0xc000230490)
    /usr/local/go/src/testing/testing.go:1595 +0xff
created by testing.(*T).Run in goroutine 613
    /usr/local/go/src/testing/testing.go:1648 +0x3ad
exit status 2
FAIL    github.com/danielgtaylor/restish/openapi    0.026s

It's reproducible by adjusting openapi/testdata and running go test.

diff --git a/openapi/testdata/petstore/openapi.yaml b/openapi/testdata/petstore/openapi.yaml
index e0f4553..763a238 100644
--- a/openapi/testdata/petstore/openapi.yaml
+++ b/openapi/testdata/petstore/openapi.yaml
@@ -86,6 +86,8 @@ components:
           type: integer
           format: int64
         name:
+          nullable: true
+          default: null
           type: string
         tag:
           type: string

I checked the libopenapi repository and the code which is causing this issue has been changed, meaning a dependency upgrade might be in order.

Cheers.

LegendWojciechPolowniak commented 2 months ago

Yes, this dependency should be upgraded. I've reported that very issue to libopenapi maintainer back in the day. Restish is using libopenapi v0.9.7 while latest is v0.17 already, was surprised to see that issue again in restish

@danielgtaylor can we bump this please, currently restish is not usable for my spec (and I would love to explore the possibilities it provides!)

thanks in advance 🙏