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 array query params with enum are always sent after try out with non empty params #5176

Closed
yunhailuo opened this issue Feb 12, 2019 · 30 comments

Comments

@yunhailuo
Copy link

Q&A (please complete the following information)

  • OS: macOS
  • Browser: chrom
  • Version: Chrome Version 72.0.3626.96 (Official Build) (64-bit)
  • Method of installation: https://editor.swagger.io/
  • Swagger-UI version: Not sure
  • Swagger/OpenAPI version: OpenAPI 3.0

Content & configuration

Example Swagger/OpenAPI definition:

openapi: '3.0.0'
info:
  description: >-
    Repro API
  title: Repro API
  version: '1.0'
paths:
  /test:
    get:
      summary: Test get
      parameters:
        - name: fields
          in: query
          required: false
          allowEmptyValue: false
          schema:
            type: array
            items:
              type: string
              enum:
                - friends
                - family
          example:
            - friends
          style: form
      responses:
        200:
          description: Success!
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: integer
                  name:
                    type: string

Swagger-UI configuration options:

Not sure; default?
No QueryStringConfig

Describe the bug you're encountering

To reproduce...

Steps to reproduce the behavior:

  1. Click on 'GET /test Test get'
  2. Click on 'Try it out'
  3. Deselect "friends" by command+click; Or select '--'
  4. Click on 'Execute'
  5. Check 'Request URL'

Expected behavior

Expect 'Request URL' being: https://editor.swagger.io/test

Screenshots

screen shot 2019-02-12 at 11 41 38 am

Additional context or thoughts

This issue is similar to but different from #4223. There will be no problem if array items don't have enum. The example is not required. It is there just to give a non-empty value to start with. As mentioned in the title, I do need to do a try out with some value(s) like "friends" if there is no example. In another word, it seems I couldn't clear the field after the first, non-empty, try out.

@hkosova
Copy link
Contributor

hkosova commented Aug 8, 2019

Related: #4749

@mu-majid
Copy link

mu-majid commented Jun 4, 2020

Is this PR going to be merged in the near future ?

@hkosova
Copy link
Contributor

hkosova commented Jun 4, 2020

@mu-majid this is not a pull request, it's a bug report.

@gongul
Copy link

gongul commented Feb 8, 2021

the problem occurs when collectionFormat: "muiti" is not a problem.
I hope this problem can be solved soon.

@h4sohail
Copy link

This issue forced us to modify our validation and allow an empty string (which is a sort of a hack) as a query param, it would be great if selecting "--" option would just simply not send that param instead of sending it empty.

@pyce-lb
Copy link

pyce-lb commented Aug 5, 2021

@h4sohail as a work around you can Ctrl+Left Click on any previously selected values to unselect them, nevertheless I agree with your suggestion:

it would be great if selecting "--" option would just simply not send that param instead of sending it empty.

@jeffreyvdhondel
Copy link

Why is this still open it looks kinda straight forward what the behaviour has to be.
"allowEmptyValue": false, is default false so why is it possible to send a empty value with this setting on false.

@iraxaxaidi
Copy link

iraxaxaidi commented Jan 10, 2022

has any solution been provided for this issue?

ctrl+ left click on previously selected values still sends empty value.

"allowEmptyValue": false is also not working.

@jeffreyvdhondel
Copy link

jeffreyvdhondel commented Jan 10, 2022

has any solution been provided for this issue?

ctrl+ left click on previously selected values still sends empty value.

"allowEmptyValue": false is also not working.

I have a work around for this problem when the empty field is send i alter the request to not send this empty value.

(res: any) => {
    var url = new URL(res.url);
    if (url.searchParams.has('fields')) {
        const fields = url.searchParams.get('fields');
        if (fields.length === 0) {
            url.searchParams.delete('fields');
        } else {
            let fixedFields = fields.split(',').filter((x) => x.length !== 0);
            url.searchParams.set('fields', fixedFields.join(','));
        }
        res.url = url.href;
    }
    return res;
}

This method is ran on the requestIntercepter

@iraxaxaidi
Copy link

has any solution been provided for this issue?
ctrl+ left click on previously selected values still sends empty value.
"allowEmptyValue": false is also not working.

I have a work around for this problem when the empty field is send i alter the request to not send this empty value.

(res) => { //Method to alter the fields param to do not send empty value ',' var url = new URL(res.url); if (url.searchParams.has('fields')) { const fields = url.searchParams.get('fields'); if (fields.length === 0) { url.searchParams.delete('fields'); } else { let fixedFields = fields.split(',').filter((x) => x.length !== 0); url.searchParams.set('fields', fixedFields.join(',')); } res.url = url.href; } return res; }

Hmm i dont know why my code is not getting correctly formated. But this methods is ran on the requestIntercepter

thank you for this. it works and I had to dynamically check and delete keys if they are null so with little changes I ended up with the following code:

    (res: any) => {
      var url = new URL(res.url);
      url.searchParams.forEach(function(value, key) {
          
      
          if (url.searchParams.has(key))
          {
            const values = url.searchParams.get(key);
            if (values.length === 0) {
              url.searchParams.delete(key);
              } else {
                let fixedValues = values.split(',').filter((x) => x.length !== 0);
                url.searchParams.set(key, fixedValues.join(','));
              }
            res.url = url.href;
          }
        })
        return res;
     }

@rsnell-usgs
Copy link

Also experiencing problems with -- being an available option to select in SwaggerUI for an enum parameter that has been specifically set to not allow empty values. This issue has been running for quite a while. Any chance of a fix in the near future? It is allowing users to send invalid requests.

@yzinc
Copy link

yzinc commented Jun 24, 2022

Quite annoying problem. I'll be much appreciated if it can be fixed asap.

@dcyuksel
Copy link

I also have the same problem, at least we need to able to remove '--' from the list.

@rohit1kumar
Copy link

It's been more than 3 years still not solved.

@PaulVrugt
Copy link

So, any progress? We would also like an option to remove the -- option

@ogunoz
Copy link

ogunoz commented Mar 2, 2023

Any progress? I'm gonna cry. Why is this option -- available. I had to add a middleware and could not get rid of it because of that. It is super annoying. Is there any plan to drop the option from UI or arrange the curl call while trying out? I am willing to help, please let me know.

@gor8808
Copy link

gor8808 commented May 1, 2023

It's annoying when you have "--" option in list but can't remove it. It does make sense to show it in documentation if there is no need

@albertodiazdorado
Copy link

Bumping this so that none gets tempted to think that this is a no-issue.
It's actually broken :)

@MonteePoke
Copy link

Bump

@Djentinga
Copy link

Please fix :)

@marmiha
Copy link

marmiha commented Sep 16, 2023

Bump

@MarMatuszewski
Copy link

Please fix :)

@vlauciani
Copy link

Please, fix ;-)

@Nicolas26
Copy link

Please fix :)

@jp1987
Copy link

jp1987 commented Jan 19, 2024

Any update on this?

@jeffrey-bishop
Copy link

jeffrey-bishop commented Jan 29, 2024

I recently ran into this issue as well. Either of the following solutions would be great:

  1. Remove the -- option.
    • When nothing is selected, the query parameter is not in the request.
  2. Keep the -- option.
    • When nothing is selected OR -- is selected, the query parameter is not in the request

Currently if I select the -- option, the query parameter is included in the request as empty.

@glowcloud
Copy link
Contributor

Addressed by #9511

@juske00
Copy link

juske00 commented May 21, 2024

Any updates? Still doesn't work.

@glowcloud
Copy link
Contributor

Hi @juske00, could you provide an example specification where this still happens?

@chrisandrewcl
Copy link

I am using the latest version (5.17.14), but the issue still persists. Here is an example specification to reproduce it:

{
      openapi: '3.1.0',
      info: {
        version: '1.0.0',
        title: 'My API',
      },
      paths: {
        '/api/v1/episodes/{id}': {
          get: {
            tags: ['episodes'],
            summary: 'Get episode by ID',
            parameters: [
              {
                schema: {
                  type: 'string',
                  example: '123456789',
                },
                required: true,
                in: 'path',
                name: 'id',
              },
              {
                schema: {
                  type: 'array',
                  items: {
                    type: 'string',
                    enum: ['value1', 'value2'],
                  },
                },
                required: false,
                description:
                  'Comma separated list of extra fields to include in the response',
                style: 'form',
                explode: false,
                name: 'include',
                in: 'query',
              },
            ],
            responses: {
              '204': {},
            },
          },
        },
      },
    }

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