swagger-api / swagger-ui

Swagger UI is a collection of HTML, JavaScript, and CSS assets that dynamically generate beautiful documentation from a Swagger-compliant API.
https://swagger.io
Apache License 2.0
26.6k stars 8.97k forks source link

Error on swagger-ui: Cannot read property 'toString' of undefined #7160

Open bnasslahsen opened 3 years ago

bnasslahsen commented 3 years ago

Q&A (please complete the following information)

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:
  /hello:
    get:
      tags:
        - hello
      operationId: hello
      parameters:
        - name: user
          in: query
          required: true
          schema:
            $ref: '#/components/schemas/User'
        - name: someEnums
          in: query
          description: SomeEum decs
          content:
            '*/*':
              schema:
                type: array
                items:
                  type: string
                  enum:
                    - FIRST
                    - SECOND
        - name: textSet
          in: query
          description: First decs
          content:
            '*/*':
              schema:
                type: array
                items:
                  type: string
        - name: someText
          in: query
          description: Second decs
          content:
            '*/*':
              schema:
                type: string
      responses:
        '200':
          description: OK
          content:
            '*/*':
              schema:
                type: string
components:
  schemas:
    User:
      type: object
      properties:
        someText:
          type: string
        textSet:
          uniqueItems: true
          type: array
          items:
            type: string
        someEnums:
          uniqueItems: true
          type: array
          items:
            type: string
            enum:
              - FIRST
              - SECOND

Swagger-UI configuration options: Default swagger-ui configuration

url=http://localhost:8080/v3/api-docs

Describe the bug you're encountering

Swagger UI fails to execute request when one of query parameters is not filled out using the yaml sample. It works only when you fill out all the query parameters (then you can remove any of the filled out parameters and no error would occur).

To reproduce...

Steps to reproduce the behavior:

  1. Go to the swagger-ui'
  2. Click on 'Try it'
  3. Click on 'Execute'
  4. See error on the browser console:
TypeError: Cannot read property 'toString' of undefined
    at Xn (content-serializer.js:18)
    at er (parameter-builders.js:43)
    at index.js:241
    at Array.forEach (<anonymous>)
    at Object.dr [as buildRequest] (index.js:196)
    at actions.js:452
    at utils.js:177
    at bindActionCreators.js:3
    at wrap-actions.js:33
    at Object.r (system.js:174)
(anonymous) @ system.js:464

Expected behavior

Requests executed as expected.

Screenshots

Capture d’écran 2021-04-11 à 15 15 41

Additional context or thoughts

As a workaround, when the OpenAPI definition doesn't contain content: element, but only schema: referenced directly, it works.

mathis-m commented 3 years ago

@tim-lai @char0n I think this is a swagger-js issue realted to buildRequest.

hkosova commented 3 years ago

@bnasslahsen unfortunately your definition is not valid:

How is the /hello request supposed to look with parameters specified? If you give us an example, we could show how the OpenAPI definition is supposed to look like.

bnasslahsen commented 3 years ago

@hkosova,

Thank you for your analysis. Here are my answers:

As per the OpenAPI spec, Parameter Object can accept content. For more complex scenarios, the content property can define the media type and schema of the parameter.

I agree, there are other alternatives for the parameters description, as i proposed here https://github.com/springdoc/springdoc-openapi/issues/1137, but i think this is just a workaround...

I believe that if the openAPI spec is correct (option 1), we shouldn't have the JS error that is shown in the console of the swagger-ui.

Reproducible with 3.49.0 today. Please note also, this issue is not reproducible with previous versions of swagger-ui. For example:3.25.1

This is a minimal description that reproduces the issue (option 1):

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
  - url: 'http://localhost:8080'
    description: Generated server url
paths:
  /hello:
    get:
      tags:
        - hello
      operationId: hello
      parameters:
        - name: sort
          in: query
          description: 'Sorting criteria: name,asc|desc'
          content:
            'application/json':
              schema:
                type: array
                items:
                  type: string
      responses:
        '200':
          description: OK
          content:
            '*/*':
              schema:
                type: string
components: {}

This is a workaround (option 2):

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
  - url: 'http://localhost:8080'
    description: Generated server url
paths:
  /hello:
    get:
      tags:
        - hello
      operationId: hello
      parameters:
        - name: sort
          in: query
          description: 'Sorting criteria: name,asc|desc'
          schema:
            type: array
            items:
              type: string
      responses:
        '200':
          description: OK
          content:
            '*/*':
              schema:
                type: string
components: {}
hkosova commented 3 years ago

@bnasslahsen clarification about

Parameters cannot have content.*/*.

Media type wildcard */* does not make sense in parameter content, because it doesn't tell how to serialize the content. Parameter content should have a specific media type e.g. application/json. Wildcard media types make sense in response definitions only.

bnasslahsen commented 3 years ago

@hkosova,

Even with specific media type application/json, the issue still remains the same: Content is allowed by the OpenAPI spec to define the media type and schema of the parameter, but this is not working with swagger-ui.