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

Allow to skip submitting empty values in form data #5830

Merged
merged 3 commits into from
May 29, 2020
Merged

Allow to skip submitting empty values in form data #5830

merged 3 commits into from
May 29, 2020

Conversation

abcang
Copy link
Contributor

@abcang abcang commented Feb 10, 2020

Description

Adds an option to send an empty value when submitting values in form data. This only works for OpenAPI 3 if the content type starts with multipart/ or is application/x-www-form-urlencoded.

Motivation and Context

Fixes #5303

Two implementations were suggested, but I chose the way to add the options.
Since it is not possible to explicitly send an empty string when automatically skipping empty values, I thought an implementation that could choose whether or not to send an empty string would be better. (Skip empty values by default.)

How Has This Been Tested?

Using OpenAPI 3 petstore definition, I checked if POST /pet/{petId} skips empty values.

Screenshots (if appropriate):

image

Checklist

My PR contains...

  • No code changes (src/ is unmodified: changes to documentation, CI, metadata, etc.)
  • Dependency changes (any modification to dependencies in package.json)
  • Bug fixes (non-breaking change which fixes an issue)
  • Improvements (misc. changes to existing features)
  • Features (non-breaking change which adds functionality)

My changes...

  • are breaking changes to a public API (config options, System API, major UI change, etc).
  • are breaking changes to a private API (Redux, component props, utility functions, etc.).
  • are breaking changes to a developer API (npm script behavior changes, new dev system dependencies, etc).
  • are not breaking changes.

Documentation

  • My changes do not require a change to the project documentation.
  • My changes require a change to the project documentation.
  • If yes to above: I have updated the documentation accordingly.

Automated tests

  • My changes can not or do not need to be tested.
  • My changes can and should be tested by unit and/or integration tests.
  • If yes to above: I have added tests to cover my changes.
  • If yes to above: I have taken care to cover edge cases in my tests.
  • All new and existing tests passed.

@tim-lai
Copy link
Contributor

tim-lai commented May 29, 2020

please build

@tim-lai tim-lai merged commit b9b32c9 into swagger-api:master May 29, 2020
@abcang abcang deleted the skip_submitting_empty_values branch May 30, 2020 03:46
@zkatemor
Copy link

zkatemor commented Jun 5, 2020

is it possible to use this functionality in rswag? if so, tell me how to do this?

@abcang
Copy link
Contributor Author

abcang commented Jun 5, 2020

I think the new swagger-ui will allow you to use this feature with the non-required parameters of the requestBody.

@zkatemor
Copy link

zkatemor commented Jun 5, 2020

@abcang, sorry, but I don’t understand what I’m doing wrong

requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                name:
                  type: string
                  allowEmptyValue: true
                  required: false
                area:
                  type: string
                  allowEmptyValue: true
                  required: false
                country:
                  type: string
                  allowEmptyValue: true
                  required: false

but still no checkboxes...

@abcang
Copy link
Contributor Author

abcang commented Jun 5, 2020

Hmmm... this feature came in from version 3.26.0, is that the right version?
It looks correct, except that the allowEmptyValue and required fields are not needed.

@Chrno21
Copy link

Chrno21 commented Jun 24, 2020

Hi,

I do not get it to work... is there something missing?

put:
      security:
        - basicAuth: []
      description: Chenge directory
      tags:
      - folder
      parameters:
        - in: query
          name: id
          required: true
          schema:
            type: integer
      requestBody:
        required: true
        content:
          application/x-www-form-urlencoded:
            schema:
              type: object
              properties:
                name:
                  type: string
                  maxLength: 80
                  example: new folder
                order:
                  type: integer
                  minimum: 1
      responses:
        301:
          $ref: "#/components/responses/folders"
        401:
          $ref: "#/components/responses/UnauthorizedError"

here too no checkboxes...

@abcang
Copy link
Contributor Author

abcang commented Jun 24, 2020

I have confirmed that a checkbox is displayed in POST ​/pet​/{petId} of this petstore definition (you have to click the button of Try it out).

  1. Visit http://petstore.swagger.io/
  2. Enter the URL in the input field
  1. Open POST ​/pet​/{petId}
  2. Click Try it out

image

(The URL of the petstore definition in the pull request description was broken, so I replaced it with a valid link.)

@Chrno21
Copy link

Chrno21 commented Jun 24, 2020

Thanks, works great. I forgot to update the swagger-editor.
But is it possible to set "Send empty values" as default?

@abcang
Copy link
Contributor Author

abcang commented Jun 24, 2020

Sorry, this pull request doesn't implement the ability to change the default behavior.

char0n added a commit that referenced this pull request Jul 7, 2020
char0n added a commit that referenced this pull request Jul 7, 2020
char0n added a commit that referenced this pull request Jul 7, 2020
tim-lai added a commit to tim-lai/swagger-ui that referenced this pull request Jul 15, 2020
tim-lai added a commit that referenced this pull request Jul 15, 2020
@diavrank
Copy link

diavrank commented Jan 8, 2022

I am looking for something similar but for arrays. How to skip submitting empty arrays for a query parameter?

I'am using Swagger Module for NestJS.

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

Successfully merging this pull request may close these issues.

Omit empty and optional values in form data
5 participants