Swagger-ui: requestInterceptor that returns promise breaks curlify output

Created on 2 Aug 2018  路  8Comments  路  Source: swagger-api/swagger-ui

Q&A (please complete the following information)

  • OS: Windows 10
  • Browser: Chrome
  • Version: 67.0.3396.99
  • Method of installation: dist assets
  • Swagger-UI version: 3.17.6
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

Example Swagger/OpenAPI definition:

openapi: 3.0.0
info:
  version: 1.0.0
  title: Swagger Petstore
  license:
    name: MIT
servers:
  - url: 'http://petstore.swagger.io/v1'
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      responses:
        '200':
          description: An paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string

Swagger-UI configuration options:

SwaggerUI({
          url: "https://api.myjson.com/bins/1h15yo",
          dom_id: '#swagger-ui',
          deepLinking: true,
          presets: [
            SwaggerUIBundle.presets.apis,
          ],
          plugins: [
            SwaggerUIBundle.plugins.DownloadUrl,
          ],
          requestInterceptor: req => {
            //return req;
            return new Promise((resolve, reject) => resolve(req));
          }
})

Describe the bug you're encountering

When using a requestInterceptor that returns a promise (that resolves to the request), instead of the pure request, the curl statement generated with curlify results in curl -X "undefined". Although the modified request is being used to send the http request.

To reproduce...

Steps to reproduce the behavior:

  1. See this jsfiddle: https://jsfiddle.net/falxfalx/6xy24zL9/32/
  2. Press play
  3. Try it out on GET /pets
  4. Execute
  5. Observe generated curl request
  6. Now comment requestInterceptor return statement and uncomment the return req; line
  7. Repeat step 2 through 5 (this is expected behaviour)

Expected behavior

The displayed curl request should not be undefined.

Additional context or thoughts

Since I saw that curlify.js has a curl function that is to be called with the request as argument, I suspect that when returning a promise, the curlify call becomes: curl(promise) while it should be something like:

const req = await promise;
curl(req);
customization try-it-out bug

Most helpful comment

If the request url isn't changed by requestInterceptor asynchronously this issue can be worked around like this:

SwaggerUI({
          // ...
          requestInterceptor: req => {
            //return req;
            const promise = new Promise((resolve, reject) => resolve(req));
            promise.url = req.url
            return promise
          }
})

All 8 comments

Can anyone please confirm this? If not, then I will have to fix this in more hacky way, by rewriting parts of the DOM or something like that..

@Falx, bug confirmed - looks to me like we need to await the mutated request in the wrapper we pass to Swagger Client:

https://github.com/swagger-api/swagger-ui/blob/c9e8a67579abcf2b87224b0f058c02fa63561fba/src/core/plugins/spec/actions.js#L407

Thank you for confirming this @shockey . Right now I worked around it by showing the unmutated request in the curl section and then adding my request mutations (the same ones I do in requestInterceptor) to the request property of the Curl component, with a wrapComponent plugin.

It would of course be nicer to use the built-in features once this is fixed.

Any update on fixing this? It's biting me too, if I set requestInterceptor the curl request is hosed.

It鈥檚 in our queue to be worked on, no promises on a fix ETA.

If anyone would like to make a PR for this, I鈥檇 be happy to go into more detail on what needs to be done.

If the request url isn't changed by requestInterceptor asynchronously this issue can be worked around like this:

SwaggerUI({
          // ...
          requestInterceptor: req => {
            //return req;
            const promise = new Promise((resolve, reject) => resolve(req));
            promise.url = req.url
            return promise
          }
})

Adding showMutatedRequest: false to the config seems to work for me. Without this option I'm seeing the same issue with the curl output.

It鈥檚 in our queue to be worked on, no promises on a fix ETA.

If anyone would like to make a PR for this, I鈥檇 be happy to go into more detail on what needs to be done.

@shockey Could you outline where all the code needs to change to make this happen? I'm working on a project where this would be beneficial. Right now I've got a fork that updates the code to work for ONLY promise based requestIntercept functions.

(That is to say the code seems to work for both Promise/Non Promise types, but one test fails)
https://github.com/swagger-api/swagger-ui/pull/5607

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LaysDragon picture LaysDragon  路  3Comments

easyest picture easyest  路  3Comments

nulltoken picture nulltoken  路  3Comments

Deraen picture Deraen  路  4Comments

liuya05 picture liuya05  路  3Comments