Comments (5)
Hi! I'm facing the same issue, represent a google UUID as a string on my schema.
What is the best way of doing it? I see that jsonschema.InterceptType has been deprecated
from openapi-go.
I append jsonschema.InterceptSchema
to openapi3.Reflector
's DefaultOptions
with
// jsonschema.InterceptSchema(func(params jsonschema.InterceptSchemaParams) (stop bool, err error)
t := params.Schema.ReflectType
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t == reflect.TypeOf(uuid.New()) {
params.Schema.Type = &jsonschema.Type{SimpleTypes: pointers.New(jsonschema.String)}
params.Schema.Pattern = pointers.New("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
params.Schema.Items = &jsonschema.Items{}
// oapi-codegen
params.Schema.ExtraProperties = map[string]any{
"x-go-type": "uuid.UUID",
"x-go-type-import": map[string]any{
"name": "uuid",
"path": "github.com/google/uuid",
},
"x-is-generated": true,
}
}
using:
- github.com/swaggest/jsonschema-go v0.3.64
- github.com/swaggest/openapi-go v0.2.44
from openapi-go.
Thank you so much @danicc097, your approach worked like a charm. I noticed that when doing this you will need to add an example manually since the struct example for path parameters won't be used on the spec generation.
This is how my code looks like now:
reflector.DefaultOptions = append(reflector.DefaultOptions, jsonschema.InterceptSchema(
func(params jsonschema.InterceptSchemaParams) (stop bool, err error) {
t := params.Schema.ReflectType
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t == reflect.TypeOf(uuid.New()) {
params.Schema.Type = &jsonschema.Type{SimpleTypes: &strSchema}
params.Schema.Pattern = &uuidPattern
params.Schema.Items = &jsonschema.Items{}
// oapi-codegen
params.Schema.Examples = []interface{}{"cdb15f83-1c5d-4727-98d1-8924ccd1fc01"}
params.Schema.ExtraProperties = map[string]any{
"x-go-type": "uuid.UUID",
"x-go-type-import": map[string]any{
"name": "uuid",
"path": "github.com/google/uuid",
},
"x-is-generated": true,
}
}
return false, nil
}))
using:
- github.com/swaggest/openapi-go v0.2.45
- github.com/swaggest/jsonschema-go v0.3.64
from openapi-go.
I'm sorry this issue already took me so long, I'll try to come up with a proper solution soon. In meanwhile, one of the workarounds is to declare desired schema with type mapping:
https://github.com/swaggest/rest/blob/v0.2.61/_examples/advanced-generic-openapi31/router.go#L84-L89
// Create custom schema mapping for 3rd party type.
uuidDef := jsonschema.Schema{}
uuidDef.AddType(jsonschema.String)
uuidDef.WithFormat("uuid")
uuidDef.WithExamples("248df4b7-aa70-47b8-a036-33ac447e668d")
jsr.AddTypeMapping(uuid.UUID{}, uuidDef)
from openapi-go.
@vearutop Thanks for the example, your approach looks cleaner. It would be nice to have a native uuid integration but this is good enough for me.
from openapi-go.
Related Issues (20)
- Error with UnmarshalYAML() using http/bearer auth scheme HOT 9
- Binary response content should not have schema
- Support OAS3.1 ? HOT 5
- Is there any support for oneOf, anyOf etc... If so how would we do it HOT 1
- How would you set a field as required? HOT 3
- XML Response Supprt
- Pointer primitives does not end up nullable
- Improving the regular expression used to remove gorilla.mux-style regexp in paths HOT 3
- pathItem has a wrong key when using regexes. HOT 1
- Security schemes validation error HOT 2
- [Question] Is there a way to select if a struct schema is a ref or not ? HOT 3
- How to use custom x- tags in a schema HOT 3
- req/resp struct using generic could cause semantic error in openapi output HOT 4
- Doc page gets 404 when getting swagger-ui assets HOT 9
- Better discriminator support? HOT 1
- Issue with self-referencing entites HOT 1
- Suggestion: support exported `json:`-tag-less fields to fully reflect / align-with Go's actual `json.Marshal` behavior HOT 4
- Method designed without params/placeholders throws "missing path parameter placeholder in url"? HOT 3
- Set response and request schemas without declared structure? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from openapi-go.