swagger-ui icon indicating copy to clipboard operation
swagger-ui copied to clipboard

Optional array query params with enum are always sent after try out with non empty params

Open yunhailuo opened this issue 5 years ago • 12 comments

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.

yunhailuo avatar Feb 12 '19 19:02 yunhailuo

Related: #4749

hkosova avatar Aug 08 '19 10:08 hkosova

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

mu-majid avatar Jun 04 '20 09:06 mu-majid

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

hkosova avatar Jun 04 '20 17:06 hkosova

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

gongul avatar Feb 08 '21 02:02 gongul

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.

h4sohail avatar Jul 27 '21 13:07 h4sohail

@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.

pyce-lb avatar Aug 05 '21 09:08 pyce-lb

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.

jeffreyvdhondel avatar Dec 06 '21 10:12 jeffreyvdhondel

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.

iraxaxaidi avatar Jan 10 '22 07:01 iraxaxaidi

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

jeffreyvdhondel avatar Jan 10 '22 07:01 jeffreyvdhondel

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;
     }

iraxaxaidi avatar Jan 10 '22 11:01 iraxaxaidi

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.

rsnell-usgs avatar Apr 21 '22 23:04 rsnell-usgs

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

yzinc avatar Jun 24 '22 15:06 yzinc

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

dcyuksel avatar Sep 27 '22 07:09 dcyuksel

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

rohit1kumar avatar Oct 08 '22 18:10 rohit1kumar

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

PaulVrugt avatar Jan 31 '23 10:01 PaulVrugt

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.

ogunoz avatar Mar 02 '23 19:03 ogunoz

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

gor8808 avatar May 01 '23 12:05 gor8808

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

albertodiazdorado avatar May 17 '23 15:05 albertodiazdorado

Bump

MonteePoke avatar Jun 22 '23 09:06 MonteePoke

Please fix :)

Djentinga avatar Jun 29 '23 12:06 Djentinga

Bump

marmiha avatar Sep 16 '23 16:09 marmiha

Please fix :)

MarMatuszewski avatar Oct 06 '23 06:10 MarMatuszewski

Please, fix ;-)

vlauciani avatar Jan 02 '24 15:01 vlauciani

Please fix :)

Nicolas26 avatar Jan 10 '24 17:01 Nicolas26

Any update on this?

jp1987 avatar Jan 19 '24 09:01 jp1987

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.

jeffrey-bishop avatar Jan 29 '24 17:01 jeffrey-bishop

Addressed by #9511

glowcloud avatar Feb 15 '24 08:02 glowcloud