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

Panic when attempting to refer to an external schema #703

Closed
harryjph opened this issue Jan 9, 2025 · 0 comments · Fixed by #729
Closed

Panic when attempting to refer to an external schema #703

harryjph opened this issue Jan 9, 2025 · 0 comments · Fixed by #729
Labels
bug Something isn't working

Comments

@harryjph
Copy link

harryjph commented Jan 9, 2025

My API is returning a JSON schema, and I'm trying to tell my OpenAPI clients that this is the case by referring to the well-known schema URL https://json-schema.org/draft/2020-12/schema.

I just want my openapi.json to contain something like:

"responses": {
  "200": {
     "content": {
       "application/json": {
         "schema": {
           "$ref": "https://json-schema.org/draft/2020-12/schema"
         }
       }
     },
   },

I've attempted to do that like this (full example here):

Responses: map[string]*huma.Response{
    "200": {
        Content: map[string]*huma.MediaType{
            "application/json": {
                Schema: &huma.Schema{
                    Ref: "https://json-schema.org/draft/2020-12/schema",
                },
            },
        },
    },
},

But this produces a panic:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x8 pc=0x5c353c]

goroutine 1 [running]:
github.com/danielgtaylor/huma/v2.(*SchemaLinkTransformer).addSchemaField(0xc00011ae70, 0xc00012e210, 0xc00011aff0)
	/tmp/gopath2575097088/pkg/mod/github.com/danielgtaylor/huma/[email protected]/transforms.go:57 +0x7c
github.com/danielgtaylor/huma/v2.(*SchemaLinkTransformer).OnAddOperation(0xc00011ae70, 0xc00012e210, 0xc00015f2c0)
	/tmp/gopath2575097088/pkg/mod/github.com/danielgtaylor/huma/[email protected]/transforms.go:103 +0x23d
github.com/danielgtaylor/huma/v2.(*OpenAPI).AddOperation(0xc00012e210, 0xc00015f2c0)
	/tmp/gopath2575097088/pkg/mod/github.com/danielgtaylor/huma/[email protected]/openapi.go:1533 +0x306
github.com/danielgtaylor/huma/v2.Register[...]({_, _}, {{0x64ae2c, 0x3}, {0x64ad38, 0x1}, 0x0, 0x0, 0x0, {0x0, ...}, ...}, ...)
	/tmp/gopath2575097088/pkg/mod/github.com/danielgtaylor/huma/[email protected]/huma.go:893 +0x2405
main.main()
	/tmp/sandbox4189181463/prog.go:14 +0x2f6

This occurs because the schema variable returned here isn't nil-checked before it is accessed on the following line.

Is this the correct way to accomplish this and this is a bug? Or am I doing something wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants