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

Optional header parameter: Javascript error when not filling the header #9550

Closed
fengelniederhammer opened this issue Feb 5, 2024 · 5 comments

Comments

@fengelniederhammer
Copy link

Q&A (please complete the following information)

  • OS: Ubuntu 22.04
  • Browser: Firefox
  • Version: 122
  • Method of installation: org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0
  • Swagger-UI version: 5.10.3 (at least that's what I infer from springdoc giving me org.webjars:swagger-ui:5.10.3)
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

Example Swagger/OpenAPI definition:

{
  "openapi": "3.0.1",
  "info": {
    "title": "OpenAPI definition",
    "version": "v0"
  },
  "servers": [
    {
      "url": "http://localhost:8080",
      "description": "Generated server url"
    }
  ],
  "paths": {
    "/dummy": {
      "get": {
        "tags": [
          "Dummy-controller"
        ],
        "description": "Dummy",
        "operationId": "dummy_1",
        "parameters": [
          {
            "name": "X-Request-ID",
            "in": "header",
            "description": "a dummy header",
            "required": false,
            "content": {
              "text/plain": {
                "schema": {}
              }
            }
          }
        ],
        "responses": {
          "200": {
            "description": "Dummy",
            "content": {
              "application/json": {
                "schema": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  }
}

Swagger-UI configuration options:

None that I did myself - maybe Springdoc does something.

Describe the bug you're encountering

To reproduce...

Given an endpoint definition with an optional header parameter (example is above)
When I click on "Try out" and "Execute" of that endpoint without filling the header parameter:
The "Loading" spinner keeps spinning infinitely and there is a JS error in the browser console.

The error from the console as text:

TypeError: i is undefined
    serialize content-serializer.js:12
    parameter_builders_header parameter-builders.js:89
    execute_buildRequest index.js:234
    execute_buildRequest index.js:201
    executeRequest actions.js:454
    systemThunkMiddleware index.js:174
    bindActionCreator Redux
    wrap_actions_executeRequest wrap-actions.js:33
    newAction system.js:175
    wrapWithTryCatch system.js:487
    actions_execute actions.js:501
    systemThunkMiddleware index.js:174
    bindActionCreator Redux
    execute wrap-actions.js:9
    newAction system.js:175
    wrapWithTryCatch system.js:487
    handleValidationResultPass execute.jsx:66
    handleValidationResult execute.jsx:80
    onClick execute.jsx:90
    React 11
        Rb
        Xb
        Yb
        Ze
        se
        jd
        lt
        Nb
        jd
        yc
        hd
    unstable_runWithPriority scheduler.production.min.js:18
    React 3
        gg
        Hb
        gd
system.js:490:16

Expected behavior

The request should be sent.

Screenshots

grafik

grafik

Additional context or thoughts

@char0n
Copy link
Member

char0n commented Mar 25, 2024

Hi everybody,

@glowcloud as discussed please test this also for OpenAPI 2.0, so that we know how SwaggerUI behaves.

@glowcloud
Copy link
Contributor

glowcloud commented Mar 25, 2024

Looking at OpenAPI 2.0, if we somehow get an undefined value for path, we'll replace the parameter in path with undefined, ex.: https://petstore.swagger.io/v2/pet/undefined/uploadImage.

For body, it looks like we'll also have undefined, from buildRequest test:

{
  url: 'http://swagger.io/me',
  credentials: 'same-origin',
  headers: {},
  method: 'POST',
  body: undefined
}

It's because we don't check if we have undefined for them:
https://github.com/swagger-api/swagger-js/blob/0fa7806772afc0ab5b4e3428468756e5e07f2a57/src/execute/swagger2/parameter-builders.js#L13-L15
and
https://github.com/swagger-api/swagger-js/blob/0fa7806772afc0ab5b4e3428468756e5e07f2a57/src/execute/swagger2/parameter-builders.js#L38-L40

We might also want to change the check in formDataBuilder
https://github.com/swagger-api/swagger-js/blob/0fa7806772afc0ab5b4e3428468756e5e07f2a57/src/execute/swagger2/parameter-builders.js#L19
to check if we have undefined, as right now, if we have allowEmptyValue: true but we don't select that option in Swagger UI, ex:

Screenshot 2024-03-25 at 12 32 25

we'll send an empty value anyway:

curl -X 'POST' \
  'https://petstore.swagger.io/v2/pet/undefined/uploadImage' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'additionalMetadata=' \
  -F 'file='

@char0n
Copy link
Member

char0n commented Mar 25, 2024

Fixes to OpenAPI 3 definitions addressed upstream in swagger-client@3.26.1.

@char0n
Copy link
Member

char0n commented Mar 25, 2024

This is going to be available in next SwaggerUI release. Please reopen if it's still manifesting in new release.

@char0n
Copy link
Member

char0n commented Mar 26, 2024

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

3 participants