diff --git a/_examples/advanced/json_map_body.go b/_examples/advanced/json_map_body.go index 5e80d7e..33b56d6 100644 --- a/_examples/advanced/json_map_body.go +++ b/_examples/advanced/json_map_body.go @@ -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 { @@ -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) diff --git a/_examples/advanced/router.go b/_examples/advanced/router.go index 901432d..5a9f27a 100644 --- a/_examples/advanced/router.go +++ b/_examples/advanced/router.go @@ -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" @@ -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( diff --git a/_examples/go.mod b/_examples/go.mod index 262175c..44bb09f 100644 --- a/_examples/go.mod +++ b/_examples/go.mod @@ -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 diff --git a/_examples/go.sum b/_examples/go.sum index d27f95b..c2fe48e 100644 --- a/_examples/go.sum +++ b/_examples/go.sum @@ -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=