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 in OnAddOperation #371

Closed
steeling opened this issue Apr 11, 2024 · 2 comments · Fixed by #375
Closed

Panic in OnAddOperation #371

steeling opened this issue Apr 11, 2024 · 2 comments · Fixed by #375
Labels
bug Something isn't working

Comments

@steeling
Copy link

OnAddOperation can panic here if one of the fields of a return type is an embedded struct (there might be a requirement that the embedded struct also has some methods on it)

Panics with

panic: reflect: embedded type with methods not implemented if type is not first field
@danielgtaylor danielgtaylor added the bug Something isn't working label Apr 11, 2024
@danielgtaylor
Copy link
Owner

@steeling here's a basic repro. This panics:

type Foo struct {
	Field string `json:"field"`
}

func (f Foo) Method() string {
	return "bar"
}

// GreetingOutput represents the greeting operation response.
type GreetingOutput struct {
	Body struct {
		Foo
		Message string `json:"message" example:"Hello, world!" doc:"Greeting message"`
	}
}

This works (note the pointer method receiver):

type Foo struct {
	Field string `json:"field"`
}

func (f *Foo) Method() string {
	return "bar"
}

// GreetingOutput represents the greeting operation response.
type GreetingOutput struct {
	Body struct {
		Foo
		Message string `json:"message" example:"Hello, world!" doc:"Greeting message"`
	}
}

Apparently this is a long-standing known issue in Go: golang/go#15924. It's unclear right now if it can be worked around since it's a problem with the underlying language support for reflection.

In the meantime, you can use pointer method receivers or disable the transformer:

config := huma.DefaultConfig("My API", "1.0.0")
config.CreateHooks = []func(Config) Config{}

// api := ... use config ...

@steeling
Copy link
Author

I wonder if it's possible to catch the issue, and not render the schema link for specific objects that suffer from this issue? Or is there an alterantive mechanism to construct the schema links?

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