|
| 1 | +package apischema |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// TestConvertJSON_InvalidInput tests the ConvertJSON function with invalid input. |
| 11 | +// It checks if the function returns the expected error when given invalid JSON data. |
| 12 | +func TestConvertJSON(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + input any |
| 16 | + wantErr error |
| 17 | + check func(defs map[string]map[string]any) error |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "invalid JSON", |
| 21 | + input: "not a json", |
| 22 | + wantErr: ErrUnmarshalJSON, |
| 23 | + }, |
| 24 | + { |
| 25 | + name: "remove defaults and rewrite refs", |
| 26 | + input: map[string]any{ |
| 27 | + "components": map[string]any{ |
| 28 | + "schemas": map[string]any{ |
| 29 | + "Foo": map[string]any{ |
| 30 | + "default": map[string]any{}, |
| 31 | + "allOf": []any{map[string]any{"$ref": "#/components/schemas/Bar"}}, |
| 32 | + "properties": map[string]any{ |
| 33 | + "nested": map[string]any{ |
| 34 | + "allOf": []any{map[string]any{"$ref": "#/components/schemas/Baz"}}, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + wantErr: nil, |
| 42 | + check: func(defs map[string]map[string]any) error { |
| 43 | + foo, ok := defs["Foo"] |
| 44 | + if !ok { |
| 45 | + return errors.New("missing Foo definition") |
| 46 | + } |
| 47 | + if _, exists := foo["default"]; exists { |
| 48 | + return errors.New("default key should be removed") |
| 49 | + } |
| 50 | + ref, ok := foo["$ref"].(string) |
| 51 | + if !ok || !strings.Contains(ref, "definitions/Bar") { |
| 52 | + return errors.New("invalid $ref for Foo: " + ref) |
| 53 | + } |
| 54 | + props, ok := foo["properties"].(map[string]any) |
| 55 | + if !ok { |
| 56 | + return errors.New("properties not a map") |
| 57 | + } |
| 58 | + nested, ok := props["nested"].(map[string]any) |
| 59 | + if !ok { |
| 60 | + return errors.New("nested not a map") |
| 61 | + } |
| 62 | + nref, ok := nested["$ref"].(string) |
| 63 | + if !ok || !strings.Contains(nref, "definitions/Baz") { |
| 64 | + return errors.New("invalid nested $ref: " + nref) |
| 65 | + } |
| 66 | + return nil |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for _, tc := range tests { |
| 72 | + t.Run(tc.name, func(t *testing.T) { |
| 73 | + var raw []byte |
| 74 | + var err error |
| 75 | + switch in := tc.input.(type) { |
| 76 | + case string: |
| 77 | + raw = []byte(in) |
| 78 | + default: |
| 79 | + raw, err = json.Marshal(in) |
| 80 | + if err != nil { |
| 81 | + t.Fatalf("failed to marshal input %s: %v", tc.name, err) |
| 82 | + } |
| 83 | + } |
| 84 | + out, err := ConvertJSON(raw) |
| 85 | + if tc.wantErr != nil { |
| 86 | + if err == nil || !errors.Is(err, tc.wantErr) { |
| 87 | + t.Fatalf("%s: expected error %v, got %v", tc.name, tc.wantErr, err) |
| 88 | + } |
| 89 | + return |
| 90 | + } |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("%s: unexpected error: %v", tc.name, err) |
| 93 | + } |
| 94 | + var w v2RootWrapper |
| 95 | + if err := json.Unmarshal(out, &w); err != nil { |
| 96 | + t.Fatalf("%s: failed to unmarshal output: %v", tc.name, err) |
| 97 | + } |
| 98 | + if tc.check != nil { |
| 99 | + defs := map[string]map[string]any{} |
| 100 | + for k, v := range w.Definitions { |
| 101 | + if m, ok := v.(map[string]any); ok { |
| 102 | + defs[k] = m |
| 103 | + } |
| 104 | + } |
| 105 | + if err := tc.check(defs); err != nil { |
| 106 | + t.Errorf("%s: check failed: %v", tc.name, err) |
| 107 | + } |
| 108 | + } |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
0 commit comments