Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of slice types documentation #75

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions _examples/advanced/json_map_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@ package main
import (
"context"
"encoding/json"
"net"

"github.com/google/uuid"
"github.com/swaggest/jsonschema-go"
"github.com/swaggest/usecase"
)

type URL string

func (URL) PrepareJSONSchema(schema *jsonschema.Schema) error {
schema.WithFormat("uri")

return nil
}

type JSONMapPayload map[string]float64

type jsonMapReq struct {
Expand All @@ -19,13 +30,25 @@ func (j *jsonMapReq) UnmarshalJSON(data []byte) error {
return json.Unmarshal(data, &j.JSONMapPayload)
}

func jsonMapBody() usecase.Interactor {
type jsonOutput struct {
Header string `json:"inHeader"`
Query int `json:"inQuery"`
Data JSONMapPayload `json:"data"`
}
type jsonOutput struct {
Header string `json:"inHeader"`
Query int `json:"inQuery"`
Data JSONMapPayload `json:"data"`
UUIDs []uuid.UUID `json:"uuids"` // Schema provided via type mapping globally.
IPs []net.IP `json:"ips"` // Schema provided via type mapping globally.
URLs1 []URL `json:"urls1"` // Schema prepared for named type.
URLs2 []string `json:"urls2"` // Schema updated in parent, no named type needed.
}

func (jsonOutput) PrepareJSONSchema(schema *jsonschema.Schema) error {
prop := schema.Properties["urls2"]
items := prop.TypeObjectEns().ItemsEns()
items.SchemaOrBoolEns().TypeObjectEns().WithFormat("uri").WithMinLength(5)

return nil
}

func jsonMapBody() usecase.Interactor {
u := usecase.NewIOI(new(jsonMapReq), new(jsonOutput), func(ctx context.Context, input, output interface{}) (err error) {
var (
in = input.(*jsonMapReq)
Expand Down
17 changes: 17 additions & 0 deletions _examples/advanced/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package main
import (
"context"
"errors"
"net"
"net/http"
"reflect"

"github.com/google/uuid"
"github.com/swaggest/jsonschema-go"
"github.com/swaggest/openapi-go/openapi3"
"github.com/swaggest/rest"
Expand Down Expand Up @@ -46,6 +48,21 @@ func NewRouter() http.Handler {
}
})

// Foreign types can be documented with type mapping to schema (uuid.UUID would be {"type":"string", "format":"uuid"}).
uuidSchema := jsonschema.String.ToSchemaOrBool()
uuidSchema.TypeObjectEns().WithFormat("uuid")

s.OpenAPICollector.Reflector().AddTypeMapping(
uuid.UUID{},
uuidSchema,
)

// Or with type mapping to type (net.IP would be string).
s.OpenAPICollector.Reflector().AddTypeMapping(
net.IP{},
"",
)

s.OpenAPICollector.CombineErrors = "anyOf"

s.Use(
Expand Down
1 change: 1 addition & 0 deletions _examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/bool64/httpmock v0.1.1
github.com/bool64/httptestbench v0.1.3
github.com/go-chi/chi/v5 v5.0.7
github.com/google/uuid v1.3.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/stretchr/testify v1.7.1
github.com/swaggest/assertjson v1.6.8
Expand Down
2 changes: 2 additions & 0 deletions _examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ github.com/golang/protobuf v1.4.3/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/iancoleman/orderedmap v0.2.0 h1:sq1N/TFpYH++aViPcaKjys3bDClUEU7s5B+z6jq8pNA=
github.com/iancoleman/orderedmap v0.2.0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
Expand Down