JSON Unmarshall change in 0.17.71 #3696
-
Hello, there was a change in 0.17.71 (probably "json.Marshaler to Enum code gen by @sonatard in #3663") that introduce a bug in my app. Reproduction: GraphQL schema: enum MyEnum {
VAL0
VAL1
VAL2
}
type MyObj {
thing1: String!
thing2: MyEnum!
thing3: String!
}
input MyObjInput {
thing1: String
thing2: MyEnum
}
extend type Mutation{
myMutation(obj: MyObjInput!): MyObj!
} And the resolver: func (r *mutationResolver) MyMutation(ctx context.Context, obj MyObjInput) (*MyObj, error) {
var ret MyObj
bytes, err := json.Marshal(obj)
if err != nil {
return nil, err
}
err = json.Unmarshal(bytes, &ret)
if err != nil {
return nil, err
}
ret.Thing3 = "ok"
return &ret, nil
} The request: query: mutation MyMutation($obj: MyObjInput!) {
myMutation(obj: $obj) {
thing1
thing2
thing3
}
} args: {
"obj": {
"thing1": "test"
}
} In v0.17.70, the response: {
"data": {
"myMutation": {
"thing1": "test",
"thing2": "",
"thing3": "ok"
}
}
} In v0.17.73, the response (I have a custom error handler):
This is due to the Because I have So I wonder if:
Thank you |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Thank you for your report. I wrote minimal code to reproduce the issue. The issue can be resolved by making the return value's field Optional, as shown below. |
Beta Was this translation helpful? Give feedback.
-
Thank you, I will update my code. |
Beta Was this translation helpful? Give feedback.
Thank you for your report.
I wrote minimal code to reproduce the issue.
The input is Optional, but the return value is not. Therefore, it feels like the correct behavior for an error to occur when trying to unmarshal null into a non-Optional field.
https://go.dev/play/p/9WRLb5495cZ
The issue can be resolved by making the return value's field Optional, as shown below.
https://go.dev/play/p/Oc2r0DoiTYU