Skip to content
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

Consumes */* in OpenAPI breaks Swagger UI #6742

Open
zerodayz opened this issue Dec 22, 2020 · 9 comments
Open

Consumes */* in OpenAPI breaks Swagger UI #6742

zerodayz opened this issue Dec 22, 2020 · 9 comments

Comments

@zerodayz
Copy link

Consumes set to / in OpenAPI spec, breaks swagger ui request.

For example in Kubernetes:

curl -X DELETE "HOST/api/v1/namespaces/test-namespace/pods/test-pod?propagationPolicy=Background" -H "accept: application/json" -H "Content-Type: /" -d "{ "propagationPolicy": "Background"}"

breaks in app because it takes / literally, instead you should be able to choose from:

  • application/json
  • application/yaml
  • application/vnd.kubernetes.protobuf

Such curl requests will results in:

"message": "the body of the request was in an unknown format - accepted media types include: application/json, application/yaml, application/vnd.kubernetes.protobuf",
"reason": "UnsupportedMediaType",

Is there a way to add custom content-types when / is matched?

@zerodayz
Copy link
Author

Workaround using injector:

    requestInterceptor: function(req) {
      req.headers['Content-Type'] = 'application/json'
      return req
  }

@zerodayz
Copy link
Author

It would be great if its possible to set the header to application/json if consumes is set to / only. As the workaround breaks other APIs that have proper consumes.

@hkosova
Copy link
Contributor

hkosova commented Dec 22, 2020

Please post your OpenAPI YAML/JSON file.
Also, what version of Swagger UI do you use? (check the versions value in the browser dev tools)

@zerodayz
Copy link
Author

zerodayz commented Dec 22, 2020

@hkosova I am using Kubernetes API: https://raw.githubusercontent.com/kubernetes/kubernetes/release-1.20/api/openapi-spec/swagger.json

Problem occurs with any API that has consumes: */*

  "/api/v1/namespaces/{namespace}/events/{name}": {
     "delete": {
       "consumes": [
         "*/*"
       ],

Swagger UI curl requests automatically adds: curl -H "Content-Type: */*" which by the API is rejected as Unknown Media Type.

@zerodayz
Copy link
Author

I am using master swagger-ui from https://github.com/swagger-api/swagger-ui, cloned today.

@hkosova
Copy link
Contributor

hkosova commented Dec 22, 2020

The problem is with Kubernetes OpenAPI definition. If their API only accepts request bodies with specific media types, such as

accepted media types include: application/json, application/yaml, application/vnd.kubernetes.protobuf",

then consumes must specify these exact media types instead of the */* wildcard:

        "consumes": [
          "application/json",
          "application/yaml",
          "application/vnd.kubernetes.protobuf",
        ],

@zerodayz
Copy link
Author

zerodayz commented Dec 22, 2020

However consumes with wildcard is valid OpenAPI definition

@hkosova
Copy link
Contributor

hkosova commented Dec 22, 2020

Yes - provided that the API server actually accepts Content-Type: */*. The API definition should match the implementation.

@zerodayz
Copy link
Author

But doesnt wildcard just represent anytype? for example if I send the application/json it works of course, so type wildcard works, the problem is that Swagger UI sent literally wildcard. Which I think is incorrect. In such situations at best I should be able to definy type myself as Server accepts wildcard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants