-
Notifications
You must be signed in to change notification settings - Fork 14
/
example_rawexposer_test.go
52 lines (42 loc) · 1.21 KB
/
example_rawexposer_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
package jsonschema_test
import (
"fmt"
"github.com/swaggest/assertjson"
"github.com/swaggest/jsonschema-go"
)
// ParentOfRawExposer is an example structure.
type ParentOfRawExposer struct {
Bar RawExposer `json:"bar"`
}
// RawExposer is an example structure.
type RawExposer struct {
Foo string `json:"foo"`
}
var _ jsonschema.RawExposer = RawExposer{}
// JSONSchemaBytes returns raw JSON Schema bytes.
// Fields and tags of structure are ignored.
func (s RawExposer) JSONSchemaBytes() ([]byte, error) {
return []byte(`{"description":"Custom description.","type":"object","properties":{"foo":{"type":"string"}}}`), nil
}
func ExampleRawExposer() {
reflector := jsonschema.Reflector{}
s, err := reflector.Reflect(ParentOfRawExposer{}, 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":{
// "RawExposer":{
// "description":"Custom description.",
// "properties":{"foo":{"type":"string"}},"type":"object"
// }
// },
// "properties":{"bar":{"$ref":"#/definitions/RawExposer"}},"type":"object"
// }
}