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

fix: trigger result json unmarshal #2883

Merged
merged 2 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion server/test/test_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,9 @@ func (output *RunOutput) UnmarshalJSON(data []byte) error {
output.Value = obj.Value
output.SpanID = obj.SpanID
output.Resolved = obj.Resolved
output.Error = fmt.Errorf(obj.Error)
if err = stringToErr(obj.Error); err != nil {
output.Error = err
}
return nil
}

Expand Down
44 changes: 44 additions & 0 deletions server/test/trigger/trigger_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,47 @@ func (t *Trigger) UnmarshalJSON(data []byte) error {

return nil
}

type triggerResultV1 struct {
Type TriggerType `json:"type"`
HTTP *HTTPResponse `json:"http,omitempty"`
GRPC *GRPCResponse `json:"grpc,omitempty"`
TraceID *TraceIDResponse `json:"traceid,omitempty"`
}

type triggerResultV2 struct {
Type TriggerType `json:"type"`
HTTP *HTTPResponse `json:"httpRequest,omitempty"`
GRPC *GRPCResponse `json:"grpc,omitempty"`
TraceID *TraceIDResponse `json:"traceid,omitempty"`
}

func (tr *triggerResultV2) valid() bool {
return tr.HTTP != nil || tr.GRPC != nil || tr.TraceID != nil
}

func (t *TriggerResult) UnmarshalJSON(data []byte) error {
v2 := triggerResultV2{}
json.Unmarshal(data, &v2)

if v2.valid() {
t.Type = v2.Type
t.HTTP = v2.HTTP
t.GRPC = v2.GRPC
t.TraceID = v2.TraceID

return nil
}

// Fallback to v1 in last case
// TriggerResult might be empty at the time of the Unmarshal, so it's ok to not validate it
v1 := triggerResultV1{}
json.Unmarshal(data, &v1)

t.Type = v1.Type
t.HTTP = v1.HTTP
t.GRPC = v1.GRPC
t.TraceID = v1.TraceID

return nil
}
80 changes: 80 additions & 0 deletions server/test/trigger/trigger_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,83 @@ func TestTriggerFormatV3(t *testing.T) {

assert.Equal(t, expected, current)
}

func TestTriggerResultV1(t *testing.T) {
v1 := struct {
Type trigger.TriggerType
HTTP *trigger.HTTPResponse
GRPC *trigger.GRPCResponse
TraceID *trigger.TraceIDResponse
}{
Type: trigger.TriggerTypeHTTP,
HTTP: &trigger.HTTPResponse{
Status: "200",
StatusCode: 200,
Headers: []trigger.HTTPHeader{
{Key: "Content-Type", Value: "application/json"},
},
Body: "this is a body",
},
}

expected := trigger.TriggerResult{
Type: trigger.TriggerTypeHTTP,
HTTP: &trigger.HTTPResponse{
Status: "200",
StatusCode: 200,
Headers: []trigger.HTTPHeader{
{Key: "Content-Type", Value: "application/json"},
},
Body: "this is a body",
},
}

v1Json, err := json.Marshal(v1)
require.NoError(t, err)

current := trigger.TriggerResult{}
err = json.Unmarshal(v1Json, &current)
require.NoError(t, err)

assert.Equal(t, expected, current)
}

func TestTriggerResultV2(t *testing.T) {
v2 := struct {
Type trigger.TriggerType `json:"type,omitEmpty"`
HTTP *trigger.HTTPResponse `json:"httpRequest,omitEmpty"`
GRPC *trigger.GRPCResponse `json:"grpc,omitEmpty"`
TraceID *trigger.TraceIDResponse `json:"traceid,omitEmpty"`
}{
Type: trigger.TriggerTypeHTTP,
HTTP: &trigger.HTTPResponse{
Status: "200",
StatusCode: 200,
Headers: []trigger.HTTPHeader{
{Key: "Content-Type", Value: "application/json"},
},
Body: "this is a body",
},
}

expected := trigger.TriggerResult{
Type: trigger.TriggerTypeHTTP,
HTTP: &trigger.HTTPResponse{
Status: "200",
StatusCode: 200,
Headers: []trigger.HTTPHeader{
{Key: "Content-Type", Value: "application/json"},
},
Body: "this is a body",
},
}

v1Json, err := json.Marshal(v2)
require.NoError(t, err)

current := trigger.TriggerResult{}
err = json.Unmarshal(v1Json, &current)
require.NoError(t, err)

assert.Equal(t, expected, current)
}