8000 Add contentType field tag for bodies by vearutop · Pull Request #135 · swaggest/openapi-go · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add contentType field tag for bodies #135

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

Merged
merged 3 commits into from
Feb 14, 2025
Merged
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ For automated HTTP REST service framework built with this library please check [
* `query`, `path` for parameters in URL
* `header`, `cookie`, `formData`, `file` for other parameters
* `form` acts as `query` and `formData`
* `contentType` indicates body content type
* [field tags](https://github.com/swaggest/jsonschema-go#field-tags) named after JSON Schema/OpenAPI 3 Schema constraints
* `collectionFormat` to unpack slices from string
* `csv` comma-separated values,
Expand Down
25 changes: 25 additions & 0 deletions openapi3/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@
); err != nil {
return err
}

r.parseRawRequestBody(o, cu)
case mimeJSON:
if err := joinErrors(
r.parseParameters(o, oc, cu),
Expand Down Expand Up @@ -314,6 +316,7 @@
tagFormData = "formData"
tagForm = "form"
tagHeader = "header"
tagContentType = "contentType"
mimeJSON = "application/json"
mimeFormUrlencoded = "application/x-www-form-urlencoded"
mimeMultipart = "multipart/form-data"
Expand Down Expand Up @@ -346,6 +349,16 @@
o.RequestBodyEns().RequestBodyEns().WithContentItem(mime, mediaType(format))
}

func (r *Reflector) parseRawRequestBody(o *Operation, cu openapi.ContentUnit) {
if cu.Structure == nil {
return
}

Check notice on line 355 in openapi3/reflect.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

1 statement(s) on lines 353:355 are not covered by tests.

refl.WalkTaggedFields(reflect.ValueOf(cu.Structure), func(_ reflect.Value, _ reflect.StructField, tag string) {
r.stringRequestBody(o, tag, "")
}, tagContentType)
}

func (r *Reflector) parseRequestBody(
o *Operation,
oc openapi.OperationContext,
Expand Down Expand Up @@ -617,6 +630,16 @@
return nil
}

func (r *Reflector) parseRawResponseBody(resp *Response, cu openapi.ContentUnit) {
if cu.Structure == nil {
return
}

refl.WalkTaggedFields(reflect.ValueOf(cu.Structure), func(_ reflect.Value, _ reflect.StructField, tag string) {
r.ensureResponseContentType(resp, tag, "")
}, tagContentType)
}

func (r *Reflector) setupResponse(o *Operation, oc openapi.OperationContext) error {
for _, cu := range oc.Response() {
if cu.HTTPStatus == 0 && !cu.IsDefault {
Expand Down Expand Up @@ -654,6 +677,8 @@
return err
}

r.parseRawResponseBody(resp, cu)

if cu.ContentType != "" {
r.ensureResponseContentType(resp, cu.ContentType, cu.Format)
}
Expand Down
47 changes: 47 additions & 0 deletions openapi3/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1355,3 +1355,50 @@ func TestWithCustomize(t *testing.T) {
}
}`, r.SpecSchema())
}

func TestRawBody(t *testing.T) {
r := openapi3.NewReflector()

oc, err := r.NewOperationContext(http.MethodPost, "/")
require.NoError(t, err)

type req struct {
TextBody string `contentType:"text/plain"`
CSVBody string `contentType:"text/csv"`
}

type resp struct {
TextBody string `contentType:"text/plain"`
CSVBody string `contentType:"text/csv"`
}

oc.AddReqStructure(req{})
oc.AddRespStructure(resp{})

require.NoError(t, r.AddOperation(oc))

assertjson.EqMarshal(t, `{
"openapi":"3.0.3","info":{"title":"","version":""},
"paths":{
"/":{
"post":{
"requestBody":{
"content":{
"text/csv":{"schema":{"type":"string"}},
"text/plain":{"schema":{"type":"string"}}
}
},
"responses":{
"200":{
"description":"OK",
"content":{
"text/csv":{"schema":{"type":"string"}},
"text/plain":{"schema":{"type":"string"}}
}
}
}
}
}
}
}`, r.SpecSchema())
}
25 changes: 25 additions & 0 deletions openapi31/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@
); err != nil {
return err
}

r.parseRawRequestBody(o, cu)
case mimeJSON:
if err := joinErrors(
r.parseParameters(o, oc, cu),
Expand Down Expand Up @@ -260,6 +262,7 @@
tagJSON = "json"
tagFormData = "formData"
tagForm = "form"
tagContentType = "contentType"
mimeJSON = "application/json"
mimeFormUrlencoded = "application/x-www-form-urlencoded"
mimeMultipart = "multipart/form-data"
Expand Down Expand Up @@ -293,6 +296,16 @@
o.RequestBodyEns().RequestBodyEns().WithContentItem(mime, mediaType(format))
}

func (r *Reflector) parseRawRequestBody(o *Operation, cu openapi.ContentUnit) {
if cu.Structure == nil {
return
}

Check notice on line 302 in openapi31/reflect.go

View workflow job for this annotation

GitHub Actions / test (1.22.x)

1 statement(s) on lines 300:302 are not covered by tests.

refl.WalkTaggedFields(reflect.ValueOf(cu.Structure), func(_ reflect.Value, _ reflect.StructField, tag string) {
r.stringRequestBody(o, tag, "")
}, tagContentType)
}

func (r *Reflector) parseRequestBody(
o *Operation,
oc openapi.OperationContext,
Expand Down Expand Up @@ -568,6 +581,16 @@
return nil
}

func (r *Reflector) parseRawResponseBody(resp *Response, cu openapi.ContentUnit) {
if cu.Structure == nil {
return
}

refl.WalkTaggedFields(reflect.ValueOf(cu.Structure), func(_ reflect.Value, _ reflect.StructField, tag string) {
resp.WithContentItem(tag, mediaType(""))
}, tagContentType)
}

func (r *Reflector) setupResponse(o *Operation, oc openapi.OperationContext) error {
for _, cu := range oc.Response() {
if cu.HTTPStatus == 0 && !cu.IsDefault {
Expand Down Expand Up @@ -605,6 +628,8 @@
return err
}

r.parseRawResponseBody(resp, cu)

if cu.ContentType != "" {
r.ensureResponseContentType(resp, cu.ContentType, cu.Format)
}
Expand Down
47 changes: 47 additions & 0 deletions openapi31/reflect_test.go
9E19
Original file line number Diff line number Diff line change
Expand Up @@ -1483,3 +1483,50 @@ func TestWithCustomize(t *testing.T) {
}
}`, r.SpecSchema())
}

func TestRawBody(t *testing.T) {
r := openapi31.NewReflector()

oc, err := r.NewOperationContext(http.MethodPost, "/")
require.NoError(t, err)

type req struct {
TextBody string `contentType:"text/plain"`
CSVBody string `contentType:"text/csv"`
}

type resp struct {
TextBody string `contentType:"text/plain"`
CSVBody string `contentType:"text/csv"`
}

oc.AddReqStructure(req{})
oc.AddRespStructure(resp{})

require.NoError(t, r.AddOperation(oc))

assertjson.EqMarshal(t, `{
"openapi":"3.1.0","info":{"title":"","version":""},
"paths":{
"/":{
"post":{
"requestBody":{
"content":{
"text/csv":{"schema":{"type":"string"}},
"text/plain":{"schema":{"type":"string"}}
}
},
"responses":{
"200":{
"description":"OK",
"content":{
"text/csv":{"schema":{"type":"string"}},
"text/plain":{"schema":{"type":"string"}}
}
}
}
}
}
}
}`, r.SpecSchema())
}
Loading
0