forked from danielgtaylor/huma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenapi_test.go
48 lines (38 loc) · 1.61 KB
/
openapi_test.go
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
package huma
import (
"reflect"
"testing"
"github.com/danielgtaylor/huma/schema"
"github.com/stretchr/testify/assert"
)
type componentFoo struct {
Field string `json:"field"`
Another string `json:"another" readOnly:"true"`
}
type componentBar struct {
Field string `json:"field"`
}
func TestComponentSchemas(t *testing.T) {
components := oaComponents{
Schemas: map[string]*schema.Schema{},
}
// Adding two different versions of the same component.
ref := components.AddSchema(reflect.TypeOf(&componentFoo{}), schema.ModeRead, "hint", true)
assert.Equal(t, ref, "#/components/schemas/componentFoo")
assert.NotNil(t, components.Schemas["componentFoo"])
ref = components.AddSchema(reflect.TypeOf(&componentFoo{}), schema.ModeWrite, "hint", true)
assert.Equal(t, ref, "#/components/schemas/componentFoo2")
assert.NotNil(t, components.Schemas["componentFoo2"])
// Re-adding the second should not create a third.
ref = components.AddSchema(reflect.TypeOf(&componentFoo{}), schema.ModeWrite, "hint", true)
assert.Equal(t, ref, "#/components/schemas/componentFoo2")
assert.Nil(t, components.Schemas["componentFoo3"])
// Adding a list of pointers to a struct.
ref = components.AddSchema(reflect.TypeOf([]*componentBar{}), schema.ModeAll, "hint", true)
assert.Equal(t, ref, "#/components/schemas/componentBarList")
assert.NotNil(t, components.Schemas["componentBarList"])
// Adding an anonymous empty struct, should use the hint.
ref = components.AddSchema(reflect.TypeOf(struct{}{}), schema.ModeAll, "hint", true)
assert.Equal(t, ref, "#/components/schemas/hint")
assert.NotNil(t, components.Schemas["hint"])
}