Skip to content

Commit d3c23d7

Browse files
authored
Add control of response content-type (#15)
1 parent 6f69602 commit d3c23d7

File tree

3 files changed

+51
-46
lines changed

3 files changed

+51
-46
lines changed

openapi3/entities.go

Lines changed: 20 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openapi3/reflect.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type OperationContext struct {
5858

5959
Output interface{}
6060
HTTPStatus int
61+
RespContentType string
6162
RespHeaderMapping map[string]string
6263
}
6364

@@ -345,7 +346,9 @@ func (r *Reflector) SetupResponse(oc OperationContext) error {
345346
resp := Response{}
346347

347348
if oc.Output != nil {
348-
err := r.parseJSONResponse(&resp, oc.Output)
349+
oc.RespContentType = strings.Split(oc.RespContentType, ";")[0]
350+
351+
err := r.parseJSONResponse(&resp, oc.Output, oc.RespContentType)
349352
if err != nil {
350353
return err
351354
}
@@ -354,6 +357,10 @@ func (r *Reflector) SetupResponse(oc OperationContext) error {
354357
if err != nil {
355358
return err
356359
}
360+
361+
if oc.RespContentType != "" {
362+
r.ensureResponseContentType(&resp, oc.RespContentType)
363+
}
357364
}
358365

359366
if resp.Description == "" {
@@ -367,7 +374,19 @@ func (r *Reflector) SetupResponse(oc OperationContext) error {
367374
return nil
368375
}
369376

370-
func (r *Reflector) parseJSONResponse(resp *Response, output interface{}) error {
377+
func (r *Reflector) ensureResponseContentType(resp *Response, contentType string) {
378+
if _, ok := resp.Content[contentType]; !ok {
379+
if resp.Content == nil {
380+
resp.Content = map[string]MediaType{}
381+
}
382+
383+
resp.Content[contentType] = MediaType{
384+
Schema: &SchemaOrRef{Schema: &Schema{}},
385+
}
386+
}
387+
}
388+
389+
func (r *Reflector) parseJSONResponse(resp *Response, output interface{}, contentType string) error {
371390
// Check if output structure exposes meaningful schema.
372391
if hasJSONBody, err := r.hasJSONBody(output); err == nil && !hasJSONBody {
373392
return nil
@@ -393,7 +412,11 @@ func (r *Reflector) parseJSONResponse(resp *Response, output interface{}) error
393412
resp.Content = map[string]MediaType{}
394413
}
395414

396-
resp.Content[mimeJSON] = MediaType{
415+
if contentType == "" {
416+
contentType = mimeJSON
417+
}
418+
419+
resp.Content[contentType] = MediaType{
397420
Schema: &oaiSchema,
398421
Example: nil,
399422
Examples: nil,

openapi3/reflect_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,9 @@ func TestReflector_SetupResponse(t *testing.T) {
339339
s.Info.Version = apiVersion
340340

341341
require.NoError(t, reflector.SetupResponse(openapi3.OperationContext{
342-
Operation: &op,
343-
HTTPStatus: http.StatusNoContent,
342+
Operation: &op,
343+
RespContentType: "text/csv; charset=utf-8",
344+
HTTPStatus: http.StatusNoContent,
344345
Output: new(struct {
345346
Val1 int
346347
Val2 string
@@ -366,7 +367,8 @@ func TestReflector_SetupResponse(t *testing.T) {
366367
"headers":{
367368
"X-Value-1":{"style":"simple","schema":{"type":"integer"}},
368369
"X-Value-2":{"style":"simple","schema":{"type":"string"}}
369-
}
370+
},
371+
"content":{"text/csv":{"schema":{}}}
370372
}
371373
}
372374
}

0 commit comments

Comments
 (0)