Skip to content

Commit 69f63d5

Browse files
committed
improve ofrep metadata handling
Signed-off-by: Kavindu Dodanduwa <[email protected]>
1 parent 57cd493 commit 69f63d5

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

providers/ofrep/internal/evaluate/resolver.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (g *OutboundResolver) resolveSingle(ctx context.Context, key string, evalCt
5151
resErr := of.NewGeneralResolutionError(fmt.Sprintf("error parsing the response: %v", err))
5252
return nil, &resErr
5353
}
54-
return toSuccessDto(success), nil
54+
return toSuccessDto(success)
5555
case 400:
5656
return nil, parseError400(rsp.Body)
5757
case 401, 403:
@@ -145,15 +145,19 @@ type successDto struct {
145145
Metadata map[string]interface{}
146146
}
147147

148-
func toSuccessDto(e evaluationSuccess) *successDto {
149-
m, _ := e.Metadata.(map[string]interface{})
148+
func toSuccessDto(e evaluationSuccess) (*successDto, *of.ResolutionError) {
149+
m, ok := e.Metadata.(map[string]interface{})
150+
if !ok {
151+
resErr := of.NewParseErrorResolutionError("metadata must be a map of string keys and arbitrary values")
152+
return nil, &resErr
153+
}
150154

151155
return &successDto{
152156
Value: e.Value,
153157
Reason: e.Reason,
154158
Variant: e.Variant,
155159
Metadata: m,
156-
}
160+
}, nil
157161
}
158162

159163
type request struct {

providers/ofrep/internal/evaluate/resolver_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ var success = evaluationSuccess{
2828
},
2929
}
3030

31+
var successWithInvalidMetadata = evaluationSuccess{
32+
Value: true,
33+
Key: "flagA",
34+
Reason: string(of.StaticReason),
35+
Variant: "true",
36+
Metadata: "metadata",
37+
}
38+
3139
func TestSuccess200(t *testing.T) {
3240
t.Run("success evaluation response", func(t *testing.T) {
3341
successBytes, err := json.Marshal(success)
@@ -80,6 +88,23 @@ func TestSuccess200(t *testing.T) {
8088

8189
validateErrorCode(success, resolutionError, of.GeneralCode, t)
8290
})
91+
92+
t.Run("invalid metadata results in a parsing error", func(t *testing.T) {
93+
b, err := json.Marshal(successWithInvalidMetadata)
94+
if err != nil {
95+
t.Fatal(err)
96+
}
97+
98+
resolver := OutboundResolver{client: mockOutbound{
99+
rsp: http.Response{
100+
StatusCode: http.StatusOK,
101+
Body: io.NopCloser(bytes.NewReader(b)),
102+
},
103+
}}
104+
success, resolutionError := resolver.resolveSingle(context.Background(), "", make(map[string]interface{}))
105+
106+
validateErrorCode(success, resolutionError, of.ParseErrorCode, t)
107+
})
83108
}
84109

85110
func TestResolveGeneralErrors(t *testing.T) {

0 commit comments

Comments
 (0)