-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathempty_test.go
More file actions
49 lines (42 loc) · 1.61 KB
/
Copy pathempty_test.go
File metadata and controls
49 lines (42 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package openapi_test
import (
"encoding/json/jsontext"
"encoding/json/v2"
"testing"
"github.com/MarkRosemaker/openapi"
)
func TestJSON_EmptyMeansAbsent(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
name string
v any
want string
}{
{"schema required", &openapi.Schema{Type: openapi.TypeObject, Required: []string{}}, `{"type":"object"}`},
{"schema properties", &openapi.Schema{Type: openapi.TypeObject, Properties: openapi.Schemas{}}, `{"type":"object"}`},
{"schema allOf", &openapi.Schema{AllOf: openapi.SchemaList{}}, `{}`},
{"operation parameters and tags", &openapi.Operation{Parameters: openapi.ParameterList{}, Tags: []string{}}, `{}`},
{"path item parameters", &openapi.PathItem{Parameters: openapi.ParameterList{}}, `{}`},
{"response content", &openapi.Response{Description: "OK", Content: openapi.Content{}}, `{"description":"OK"}`},
{"server variables", &openapi.Server{URL: "/", Variables: openapi.ServerVariables{}}, `{"url":"/"}`},
// an empty value that means something is still written.
{"schema enum", &openapi.Schema{Enum: []jsontext.Value{}}, `{"enum":[]}`},
{"operation security", &openapi.Operation{Security: openapi.SecurityRequirements{}}, `{"security":[]}`},
{"operation servers", &openapi.Operation{Servers: openapi.Servers{}}, `{"servers":[]}`},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
b, err := json.Marshal(tc.v, jsonOpts)
if err != nil {
t.Fatal(err)
}
got := jsontext.Value(b)
if err := got.Compact(); err != nil {
t.Fatal(err)
}
if string(got) != tc.want {
t.Fatalf("got %s, want %s", got, tc.want)
}
})
}
}