Skip to content

Commit 37e565a

Browse files
authored
Add example for contentType encoder/decoder (#224)
1 parent 6b80a5d commit 37e565a

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

_examples/advanced-generic-openapi31/_testdata/openapi.json

+23
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,29 @@
655655
"x-forbid-unknown-query":true
656656
}
657657
},
658+
"/raw-body":{
659+
"parameters":[
660+
{
661+
"name":"X-Umbrella-Header","in":"header","description":"This request header is supported in all operations.",
662+
"schema":{"type":"string"}
663+
}
664+
],
665+
"post":{
666+
"tags":["Request","Response"],"summary":"Request/response With Raw Body",
667+
"description":"The `contentType` tag acts as a discriminator of where to read/write the body.",
668+
"operationId":"_examples/advanced-generic-openapi31.rawBody",
669+
"requestBody":{"content":{"text/csv":{"schema":{"type":"string"}},"text/plain":{"schema":{"type":"string"}}}},
670+
"responses":{
671+
"200":{
672+
"description":"OK",
673+
"content":{
674+
"application/json":{"schema":{"type":"string"}},"text/csv":{"schema":{"type":"string"}},
675+
"text/plain":{"schema":{"type":"string"}}
676+
}
677+
}
678+
}
679+
}
680+
},
658681
"/req-resp-mapping":{
659682
"parameters":[
660683
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"github.com/swaggest/usecase"
6+
)
7+
8+
func rawBody() usecase.Interactor {
9+
type rawBody struct {
10+
TextBody string `contentType:"text/plain"`
11+
CSVBody string `contentType:"text/csv"`
12+
}
13+
14+
u := usecase.NewInteractor(func(ctx context.Context, input rawBody, output *rawBody) error {
15+
*output = input
16+
17+
return nil
18+
})
19+
20+
u.SetTitle("Request/response With Raw Body")
21+
u.SetDescription("The `contentType` tag acts as a discriminator of where to read/write the body.")
22+
u.SetTags("Request", "Response")
23+
24+
return u
25+
}

_examples/advanced-generic-openapi31/router.go

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ func NewRouter() *web.Service {
154154

155155
s.Get("/query-object", queryObject())
156156
s.Post("/form", form())
157+
s.Post("/raw-body", rawBody())
157158

158159
s.Post("/file-upload", fileUploader())
159160
s.Post("/file-multi-upload", fileMultiUploader())

0 commit comments

Comments
 (0)