Skip to content

Commit 3d80b6f

Browse files
committed
fix: simplified json_converter_test
1 parent 590d116 commit 3d80b6f

File tree

1 file changed

+46
-90
lines changed

1 file changed

+46
-90
lines changed

listener/apischema/json_converter_test.go

Lines changed: 46 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3,105 +3,61 @@ package apischema
33
import (
44
"encoding/json"
55
"errors"
6-
"strings"
6+
"reflect"
77
"testing"
8-
9-
"github.com/stretchr/testify/assert"
10-
"github.com/stretchr/testify/require"
118
)
129

13-
// TestConvertJSON_InvalidInput tests the ConvertJSON function with invalid input.
14-
// It checks if the function returns the expected error when given invalid JSON data.
15-
func TestConvertJSON(t *testing.T) {
16-
tests := []struct {
17-
name string
18-
input any
19-
wantErr error
20-
check func(defs map[string]map[string]any) error
21-
}{
22-
{
23-
name: "invalid_JSON",
24-
input: "not a json",
25-
wantErr: ErrUnmarshalJSON,
26-
},
27-
{
28-
name: "remove_defaults_and_rewrite_refs",
29-
input: map[string]any{
30-
"components": map[string]any{
31-
"schemas": map[string]any{
32-
"Foo": map[string]any{
33-
"default": map[string]any{},
34-
"allOf": []any{map[string]any{"$ref": "#/components/schemas/Bar"}},
35-
"properties": map[string]any{
36-
"nested": map[string]any{
37-
"allOf": []any{map[string]any{"$ref": "#/components/schemas/Baz"}},
38-
},
39-
},
40-
},
41-
},
42-
},
43-
},
44-
wantErr: nil,
45-
check: func(defs map[string]map[string]any) error {
46-
foo, ok := defs["Foo"]
47-
if !ok {
48-
return errors.New("missing Foo definition")
49-
}
50-
if _, exists := foo["default"]; exists {
51-
return errors.New("default key should be removed")
52-
}
53-
ref, ok := foo["$ref"].(string)
54-
if !ok || !strings.Contains(ref, "definitions/Bar") {
55-
return errors.New("invalid $ref for Foo: " + ref)
56-
}
57-
props, ok := foo["properties"].(map[string]any)
58-
if !ok {
59-
return errors.New("properties not a map")
60-
}
61-
nested, ok := props["nested"].(map[string]any)
62-
if !ok {
63-
return errors.New("nested not a map")
64-
}
65-
nref, ok := nested["$ref"].(string)
66-
if !ok || !strings.Contains(nref, "definitions/Baz") {
67-
return errors.New("invalid nested $ref: " + nref)
68-
}
69-
return nil
70-
},
71-
},
10+
func TestConvertJSON_InvalidInput(t *testing.T) {
11+
_, err := ConvertJSON([]byte("not a json"))
12+
if !errors.Is(err, ErrUnmarshalJSON) {
13+
t.Errorf("expected ErrUnmarshalJSON, got %v", err)
7214
}
15+
}
7316

74-
for _, tc := range tests {
75-
t.Run(tc.name, func(t *testing.T) {
76-
var raw []byte
77-
var err error
78-
switch in := tc.input.(type) {
79-
case string:
80-
raw = []byte(in)
81-
default:
82-
raw, err = json.Marshal(in)
83-
require.NoError(t, err, "failed to marshal input %s", tc.name)
84-
}
85-
out, err := ConvertJSON(raw)
86-
if tc.wantErr != nil {
87-
assert.ErrorIs(t, err, tc.wantErr, "error mismatch")
88-
return
17+
func TestConvertJSON_Transforms(t *testing.T) {
18+
input := []byte(`{
19+
"components": {
20+
"schemas": {
21+
"Foo": {
22+
"default": {},
23+
"allOf": [{ "$ref": "#/components/schemas/Bar" }],
24+
"properties": {
25+
"nested": {
26+
"allOf": [{ "$ref": "#/components/schemas/Baz" }]
27+
}
28+
}
29+
}
8930
}
90-
assert.NoError(t, err, "unexpected error")
91-
var w v2RootWrapper
92-
err = json.Unmarshal(out, &w)
93-
require.NoErrorf(t, err, "%s: failed to unmarshal output", tc.name)
31+
}
32+
}`)
9433

95-
if tc.check != nil {
96-
defs := map[string]map[string]any{}
97-
for k, v := range w.Definitions {
98-
if m, ok := v.(map[string]any); ok {
99-
defs[k] = m
34+
expected := `{
35+
"definitions": {
36+
"Foo": {
37+
"$ref": "#/definitions/Bar",
38+
"properties": {
39+
"nested": {
40+
"$ref": "#/definitions/Baz"
10041
}
10142
}
102-
err := tc.check(defs)
103-
assert.NoErrorf(t, err, "%s: check failed", tc.name)
10443
}
105-
})
44+
}
45+
}`
46+
47+
out, err := ConvertJSON(input)
48+
if err != nil {
49+
t.Fatalf("unexpected error: %v", err)
50+
}
51+
52+
var got, want map[string]any
53+
if err := json.Unmarshal(out, &got); err != nil {
54+
t.Fatalf("unmarshal output: %v", err)
55+
}
56+
if err := json.Unmarshal([]byte(expected), &want); err != nil {
57+
t.Fatalf("unmarshal expected: %v", err)
58+
}
59+
60+
if !reflect.DeepEqual(got, want) {
61+
t.Errorf("output mismatch:\n got: %v\nwant: %v", got, want)
10662
}
10763
}

0 commit comments

Comments
 (0)