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

Unnecessary resolver is created when json tag has omitempty and value different than property name #2465

Closed
mstephano opened this issue Dec 11, 2022 · 2 comments · Fixed by #2468

Comments

@mstephano
Copy link
Contributor

mstephano commented Dec 11, 2022

What happened?

A resolver is created when the field of my model has a json tag with omitempty, and the value is different than the property name.

What did you expect?

No resolver should be created, just use my model as is.

Minimal graphql.schema and models to reproduce

My schema:

type MyModel {
    newDesc: String
}

My model:

  1. Needs to be in another package than /graph/model
  2. The package must be under autobind in gqlgen.yml file, also need the struct_tag: json
  3. Create this file in its own folder to test (do not mix with other packages in same folder). ex: <gqlgen_root_folder>/testomitempty/testmodel.go

Here, my json tag has omitempty, my property name is Description, but the value in the json is newDesc

package testomitempty

type MyModel struct {
	Description string `json:"newDesc,omitempty"`
}

My gqlgen.yml file

...
struct_tag: json
...
autobind:
  - "github.com/99designs/gqlgen/graph/model"
  - "github.com/99designs/gqlgen/testomitempty"
...

It is now generating this resolver (that I really don't need):

// NewDesc is the resolver for the newDesc field.
func (r *myModelResolver) NewDesc(ctx context.Context, obj *testomitempty.MyModel) (*string, error) {
	panic(fmt.Errorf("not implemented: NewDesc - newDesc"))
}

// MyModel returns MyModelResolver implementation.
func (r *Resolver) MyModel() MyModelResolver { return &myModelResolver{r} }

type myModelResolver struct{ *Resolver }

UPDATE:
For those having the same issue, here is how I fixed it locally at line:

func equalFieldName(source, target string) bool {

func equalFieldName(source, target string) bool {
	source = strings.ReplaceAll(source, "_", "")
	source = strings.ReplaceAll(source, ",omitempty", "")  // Add this line
	target = strings.ReplaceAll(target, "_", "")
	return strings.EqualFold(source, target)
}

versions

  • go run github.com/99designs/gqlgen version? v0.17.22-dev
  • go version? go1.19.3 darwin/arm64
@StevenACoffman
Copy link
Collaborator

A PR would be welcome for a fix to this issue.

@mstephano
Copy link
Contributor Author

I will create a PR soon, just want to make sure I can TDD this by creating a failed test first. I want to make sure this line change will not affect anything else.

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 a pull request may close this issue.

2 participants