Skip to content

Zenith: fix Go struct field json serialization #5860

Closed

Description

@sipsma

#5757 (comment)

Right now if you have something like:

type Foo struct {
  Bar `json:"blahblah"`
}

The graphql schema for Foo will be populated with a field called Bar, but when we do json serialization we use blahblah, which means that if you try to query for the value of the Bar field, our gql server hits the trivial resolver codepath and looks for bar in the json object and doesn't find it (since it's been serialized to blahblah).

My first thought was that if a json struct tag is present, we should use that as the graphql field name rather than the go name. That would fix this problem I think, but it's more debatable whether it's surprising behavior (or honestly if anyone will really care about this).


Possibly related issue from #5757 (comment)

package main

import (
	"context"
)

type Foo struct{}

type Test struct {
	X string
	Y int
}

func (t *Test) Void() {}

func (m *Foo) MyFunction(ctx context.Context) Test {
	return Test{X: "hello", Y: 1337}
}
$ echo '{foo{myFunction{x}}}' | dagger query -m ""
...
⧗ 1.17s ✔ 22 ∅ 20 ✘ 2
Error: make request: input:1: foo.myFunction.x Cannot return null for non-nullable field Test.x.

If I modify the struct with JSON field names, the above works:

type Test struct {
	X string `json:"x"`
	Y int    `json:"y"`
}

cc @vito @jedevc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions