-
Notifications
You must be signed in to change notification settings - Fork 14
/
example_oneofexposer_test.go
64 lines (52 loc) · 1.39 KB
/
example_oneofexposer_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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package jsonschema_test
import (
"fmt"
"github.com/swaggest/assertjson"
"github.com/swaggest/jsonschema-go"
)
// ParentOfOneOfExposer is an example structure.
type ParentOfOneOfExposer struct {
Bar OneOfExposer `json:"bar"`
}
// OneOfExposer is an example structure.
type OneOfExposer struct{}
type OneOf1 struct {
Foo string `json:"foo" required:"true"`
}
type OneOf2 struct {
Baz string `json:"baz" required:"true"`
}
var _ jsonschema.OneOfExposer = OneOfExposer{}
func (OneOfExposer) JSONSchemaOneOf() []interface{} {
return []interface{}{
OneOf1{}, OneOf2{},
}
}
func ExampleOneOfExposer() {
reflector := jsonschema.Reflector{}
s, err := reflector.Reflect(ParentOfOneOfExposer{}, jsonschema.StripDefinitionNamePrefix("JsonschemaGoTest"))
if err != nil {
panic(err)
}
j, err := assertjson.MarshalIndentCompact(s, "", " ", 80)
if err != nil {
panic(err)
}
fmt.Println(string(j))
// Output:
// {
// "definitions":{
// "OneOf1":{
// "required":["foo"],"properties":{"foo":{"type":"string"}},"type":"object"
// },
// "OneOf2":{
// "required":["baz"],"properties":{"baz":{"type":"string"}},"type":"object"
// },
// "OneOfExposer":{
// "type":"object",
// "oneOf":[{"$ref":"#/definitions/OneOf1"},{"$ref":"#/definitions/OneOf2"}]
// }
// },
// "properties":{"bar":{"$ref":"#/definitions/OneOfExposer"}},"type":"object"
// }
}