Skip to content

Commit

Permalink
Add more tests, fix description and no content handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Aug 3, 2023
1 parent b6fef4e commit dfca1eb
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 2 deletions.
2 changes: 1 addition & 1 deletion openapi3/_testdata/openapi_req.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{"name":"in_cookie","in":"cookie","deprecated":true,"schema":{"type":"string","deprecated":true}},
{"name":"in_header","in":"header","schema":{"type":"number"}}
],
"requestBody":{"content":{"text/csv":{"schema":{"type":"string"}}}},
"requestBody":{"description":"Request body in CSV format.","content":{"text/csv":{"schema":{"type":"string"}}}},
"responses":{"204":{"description":"No Content"}}
}
}
Expand Down
2 changes: 1 addition & 1 deletion openapi3/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (s *Spec) AddOperation(method, path string, operation Operation) error {
}

// Add "No Content" response if there are no responses configured.
if len(operation.Responses.MapOfResponseOrRefValues) == 0 {
if len(operation.Responses.MapOfResponseOrRefValues) == 0 && operation.Responses.Default == nil {
operation.Responses.WithMapOfResponseOrRefValuesItem(strconv.Itoa(http.StatusNoContent), ResponseOrRef{
Response: &Response{
Description: http.StatusText(http.StatusNoContent),
Expand Down
8 changes: 8 additions & 0 deletions openapi3/reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ func (r *Reflector) setupRequest(o *Operation, oc openapi.OperationContext) erro
default:
r.stringRequestBody(o, cu.ContentType, cu.Format)
}

if cu.Description != "" && o.RequestBody != nil && o.RequestBody.RequestBody != nil {
o.RequestBody.RequestBody.WithDescription(cu.Description)
}
}

return nil
Expand Down Expand Up @@ -735,6 +739,10 @@ func (r *Reflector) setupResponse(o *Operation, oc openapi.OperationContext) err
}
}

if cu.Description != "" {
resp.Description = cu.Description
}

if resp.Description == "" {
resp.Description = http.StatusText(cu.HTTPStatus)
}
Expand Down
39 changes: 39 additions & 0 deletions openapi3/reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,3 +989,42 @@ func TestReflector_AddOperation_request_queryObject_deepObject(t *testing.T) {
}
}`, reflector.Spec)
}

type textCSV struct{}

func (t textCSV) SetupContentUnit(cu *openapi.ContentUnit) {
cu.ContentType = "text/csv"
cu.Description = "This is CSV."
cu.IsDefault = true
}

func TestReflector_AddOperation_contentUnitPreparer(t *testing.T) {
r := openapi3.NewReflector()
oc, err := r.NewOperationContext(http.MethodPost, "/foo")
require.NoError(t, err)

oc.AddReqStructure(textCSV{})
oc.AddRespStructure(textCSV{})

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

assertjson.EqMarshal(t, `{
"openapi":"3.0.3","info":{"title":"","version":""},
"paths":{
"/foo":{
"post":{
"requestBody":{
"description":"This is CSV.",
"content":{"text/csv":{"schema":{"type":"string"}}}
},
"responses":{
"default":{
"description":"This is CSV.",
"content":{"text/csv":{"schema":{"type":"string"}}}
}
}
}
}
}
}`, r.SpecSchema())
}
18 changes: 18 additions & 0 deletions operation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package openapi_test

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"
"github.com/swaggest/openapi-go"
)

func TestContentUnit_options(t *testing.T) {
cu := openapi.ContentUnit{}
openapi.WithContentType("text/csv")(&cu)
openapi.WithHTTPStatus(http.StatusConflict)(&cu)

assert.Equal(t, "text/csv", cu.ContentType)
assert.Equal(t, http.StatusConflict, cu.HTTPStatus)
}

0 comments on commit dfca1eb

Please sign in to comment.