Files
tasks/common/codec/jsonpb_test.go
Collin Van DyckandGitHub 43604173f9 fix: Ensure JSONPBEncoder terminates empty slice with ]. (#8664)
## What changed?

This fixes an issue in the `JSONPBEncoder` which does not close the
opening `[` with a `]` when encoding an empty slice. The fix was to move
the line which emitted `]` outside of the for loop.

## Why?

While working on another project which uses the `JSONPBEncoder` I saw
that an empty slice was getting encoded as `[`.

## How did you test it?
- [x] built
- [ ] run locally and tested manually
- [ ] covered by existing tests
- [x] added new unit test(s)
- [ ] added new functional test(s)

## Potential risks

I'm not too familiar with the OSS temporal code, but I would not imagine
there would be anything that would be dependent on invalid json being
emitted.
2025-11-20 15:12:27 -05:00

115 lines
4.1 KiB
Go

package codec
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/suite"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
historypb "go.temporal.io/api/history/v1"
"go.temporal.io/server/common/testing/protoassert"
"google.golang.org/protobuf/types/known/timestamppb"
)
type (
jsonpbEncoderSuite struct {
suite.Suite
encoder JSONPBEncoder
}
)
var (
history = &historypb.History{
Events: []*historypb.HistoryEvent{
{
Version: 1234,
EventId: 130,
EventTime: timestamppb.New(time.Date(1978, 8, 22, 0, 0, 0, 0, time.UTC)),
EventType: enumspb.EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED,
Attributes: &historypb.HistoryEvent_RequestCancelExternalWorkflowExecutionInitiatedEventAttributes{RequestCancelExternalWorkflowExecutionInitiatedEventAttributes: &historypb.RequestCancelExternalWorkflowExecutionInitiatedEventAttributes{
Namespace: "some random target namespace",
WorkflowExecution: &commonpb.WorkflowExecution{
WorkflowId: "some random target workflow ID",
RunId: "some random target run ID",
},
ChildWorkflowOnly: true,
Control: "some random control",
}},
},
},
}
encodedHistory = `{"events": [{"eventId":"130","eventTime":"1978-08-22T00:00:00Z","eventType":"EVENT_TYPE_REQUEST_CANCEL_EXTERNAL_WORKFLOW_EXECUTION_INITIATED","version":"1234","requestCancelExternalWorkflowExecutionInitiatedEventAttributes":{"namespace":"some random target namespace","workflowExecution":{"workflowId":"some random target workflow ID","runId":"some random target run ID"},"control":"some random control","childWorkflowOnly":true}}]}`
oldEncodedHistory = `{"events": [{"eventId":"130","eventTime":"1978-08-22T00:00:00Z","eventType":"RequestCancelExternalWorkflowExecutionInitiated","version":"1234","requestCancelExternalWorkflowExecutionInitiatedEventAttributes":{"namespace":"some random target namespace","workflowExecution":{"workflowId":"some random target workflow ID","runId":"some random target run ID"},"control":"some random control","childWorkflowOnly":true}}]}`
)
func TestJSONPBEncoderSuite(t *testing.T) {
s := new(jsonpbEncoderSuite)
suite.Run(t, s)
}
func (s *jsonpbEncoderSuite) SetupSuite() {
s.encoder = NewJSONPBEncoder()
}
func (s *jsonpbEncoderSuite) TestEncode() {
json, err := s.encoder.Encode(history)
s.Require().NoError(err)
s.JSONEq(encodedHistory, string(json))
}
func (s *jsonpbEncoderSuite) TestDecode() {
var val historypb.History
err := s.encoder.Decode([]byte(encodedHistory), &val)
s.Require().NoError(err)
protoassert.ProtoEqual(s.T(), &val, history)
}
func (s *jsonpbEncoderSuite) TestEncodeHistories() {
var histories []*historypb.History
histories = append(histories, history)
histories = append(histories, history)
histories = append(histories, history)
json, err := s.encoder.EncodeHistories(histories)
s.Require().NoError(err)
s.JSONEq(fmt.Sprintf("[%[1]s,%[1]s,%[1]s]", encodedHistory), string(json))
}
func (s *jsonpbEncoderSuite) TestEncodeEmptyHistories() {
var histories []*historypb.History
json, err := s.encoder.EncodeHistories(histories)
s.Require().NoError(err)
s.JSONEq("[]", string(json))
}
func (s *jsonpbEncoderSuite) TestDecodeHistories() {
historyJSON := fmt.Sprintf("[%[1]s,%[1]s,%[1]s]", encodedHistory)
var histories []*historypb.History
histories = append(histories, history)
histories = append(histories, history)
histories = append(histories, history)
decodedHistories, err := s.encoder.DecodeHistories([]byte(historyJSON))
s.Require().NoError(err)
protoassert.ProtoSliceEqual(s.T(), histories, decodedHistories)
}
func (s *jsonpbEncoderSuite) TestDecodeOldHistories() {
historyJSON := fmt.Sprintf("[%[1]s,%[1]s,%[1]s]", oldEncodedHistory)
var historyEvents []*historypb.History
historyEvents = append(historyEvents, history)
historyEvents = append(historyEvents, history)
historyEvents = append(historyEvents, history)
decodedHistories, err := s.encoder.DecodeHistories([]byte(historyJSON))
s.Require().NoError(err)
protoassert.ProtoSliceEqual(s.T(), historyEvents, decodedHistories)
}