MediaType (Content-Type) should be based on ProducesMediaTypes rather than ConsumesMediaTypes
jrboelens opened this issue · comments
The following code is part of runtime.go
.
It appears as though the intent is to ensure Content-Type
is not left empty.
I believe that the for loop should be iterating over ProducesMediaTypes
rather than ConsumesMediaTypes
.
The fact that the second block of code checks against the existence of the value in the Produces
map seems to highlight there is an issue.
// TODO: pick appropriate media type
cmt := r.DefaultMediaType
for _, mediaType := range operation.ConsumesMediaTypes {
// Pick first non-empty media type
if mediaType != "" {
cmt = mediaType
break
}
}
if _, ok := r.Producers[cmt]; !ok && cmt != runtime.MultipartFormMime && cmt != runtime.URLencodedFormMime {
return nil, fmt.Errorf("none of producers: %v registered. try %s", r.Producers, cmt)
}
There do not appear to be any tests for this bit of logic as no tests that break when changing the code from ConsumesMediaTypes
to ProducesMediaTypes
.
I am happy to submit a patch with a simple change and hopefully a test if this is in fact a bug.
For which side is this because on clients, that wording is reversed because consumes/produces meanings are inverted in the spec when looked at from the client side. It's the consumes for the server
I am looking at this from the client side.
Then this code is correct. The client produces the media types in the consumes section because that's what the server can consume. Inversely the client consumes the media types in the produces spec because it's the server doing the producing.
Thank you for clarifying.
I realized after you clarified that at some point a consumes
of application/json
snuck into the spec I am referencing. That incorrectly led me to opening this issue.
After removing the errant consumes
, I had to work through another issue which I will outline in case it helps anyone in the future.
I have a spec which does not contain a consumes option and yields generated code like this:
ProducesMediaTypes: []string{""},
ConsumesMediaTypes: []string{""}
The call I am making is a DELETE
that neither expects to send nor receive a body.
I am interacting with a Flask server that does not like DELETE
s with empty bodies in conjunction with a Content-Type: application/json
header.
If there are no consumes
specified, the default mime type is set via..
rt.DefaultMediaType = runtime.JSONMime <-- application/json
Overriding the DefaultMediaType
with application/octet-stream
was enough to make the Flask server happy.