forked from swaggest/openapi-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_misc_test.go
More file actions
51 lines (41 loc) · 1.41 KB
/
example_misc_test.go
File metadata and controls
51 lines (41 loc) · 1.41 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
50
51
package openapi31_test
import (
"fmt"
"net/http"
"reflect"
"github.com/swaggest/assertjson"
"github.com/swaggest/jsonschema-go"
"github.com/swaggest/openapi-go/openapi31"
)
func ExampleReflector_options() {
r := openapi31.Reflector{}
// Reflector embeds jsonschema.Reflector and it is possible to configure optional behavior.
r.DefaultOptions = append(r.DefaultOptions,
jsonschema.InterceptNullability(func(params jsonschema.InterceptNullabilityParams) {
// Removing nullability from non-pointer slices (regardless of omitempty).
if params.Type.Kind() != reflect.Ptr && params.Schema.HasType(jsonschema.Null) && params.Schema.HasType(jsonschema.Array) {
*params.Schema.Type = jsonschema.Array.Type()
}
}))
type req struct {
Foo []int `json:"foo"`
}
oc, _ := r.NewOperationContext(http.MethodPost, "/foo")
oc.AddReqStructure(new(req))
_ = r.AddOperation(oc)
j, _ := assertjson.MarshalIndentCompact(r.Spec, "", " ", 120)
fmt.Println(string(j))
// Output:
// {
// "openapi":"3.1.0","info":{"title":"","version":""},
// "paths":{
// "/foo":{
// "post":{
// "requestBody":{"content":{"application/json":{"schema":{"$ref":"#/components/schemas/Openapi31TestReq"}}}},
// "responses":{"204":{"description":"No Content"}}
// }
// }
// },
// "components":{"schemas":{"Openapi31TestReq":{"properties":{"foo":{"items":{"type":"integer"},"type":"array"}},"type":"object"}}}
// }
}