-
Notifications
You must be signed in to change notification settings - Fork 25
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
Set response and request schemas without declared structure? #96
Comments
Please check if virtual structure works for your case. |
Thanks, I found this useful! There is a way to add response structure with raw yaml or json? Like:
|
Hi, you can use type rawExposer func() ([]byte, error)
func (r rawExposer) JSONSchemaBytes() ([]byte, error) {
return r()
}
func TestReflector_AddOperation_rawSchema(t *testing.T) {
r := openapi31.NewReflector()
oc, err := r.NewOperationContext(http.MethodPost, "/foo")
require.NoError(t, err)
oc.AddRespStructure(rawExposer(func() ([]byte, error) {
return []byte(`{"type":"object","properties":{"foo":{"type":"integer"}}}`), nil
}), openapi.WithHTTPStatus(http.StatusAccepted))
require.NoError(t, r.AddOperation(oc))
assertjson.EqMarshal(t, `{
"openapi":"3.1.0","info":{"title":"","version":""},
"paths":{
"/foo":{
"post":{
"responses":{
"202":{
"description":"Accepted",
"content":{
"application/json":{
"schema":{"$ref":"#/components/schemas/Openapi31TestRawExposer"}
}
}
}
}
}
}
},
"components":{
"schemas":{
"Openapi31TestRawExposer":{"properties":{"foo":{"type":"integer"}},"type":"object"}
}
}
}`, r.SpecSchema())
} (Beware of definition name collisions when using same Go type to expose different schemas, perhaps this can be improved on |
But it only works for JSON? Any way to do the same with YAML? |
hmhm, I considered YAML as an unnecessary "convenience" format that does not need first-class support, being just another form of JSON, so nothing like but I think it is easy to make a helper function that would take YAML, convert it to JSON and expose as |
Okey, thank you very much for answers, you helped me a lot! Maybe it's a good point to add new examples in the documentation :) |
Hi, very nice lib!
My app knows about response format only during runtime(it takes it from config file). So I cant declare response format with struct.
Here is any way how I can set response schema with json, yaml, map or something else?
The text was updated successfully, but these errors were encountered: