nestjs / swagger

OpenAPI (Swagger) module for Nest framework (node.js) :earth_americas:
https://nestjs.com
MIT License
1.69k stars 474 forks source link

Requests from Swagger-UI include optional not filled params #544

Closed seclace closed 4 years ago

seclace commented 4 years ago

I'm submitting a...


[?] Regression 
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Request from Swagger-UI sends optional parameters anyway. So then the server throws an error that optional fields are validated with errors and responds with 400 status code. From new Swagger-UI: From new Swagger-UI

From old Swagger-UI: From old Swagger-UI

Expected behavior

Swagger-UI should not send optional parameters if they are not filled. (In previous release of swagger 2.0 it was working correctly)

Minimal reproduction of the problem with instructions

Add use of @ApiBody() decorator to any controller's endpoint:

dto/my-type.dto.ts:

import { ApiPropertyOptional } from '@nestjs/swagger';
import { IsNumberString, IsOptional, IsString } from 'class-validator';

export class MyTypeDto {
  @ApiPropertyOptional()
  @IsOptional()
  @IsString()
  name?: string;

  @ApiPropertyOptional()
  @IsOptional()
  @IsNumberString()
  age?: string;
}

my-controller.controller.ts:

import { Controller, Post } from '@nestjs/common';
import { ApiBody, ApiConsumes } from '@nestjs/swagger';
import { MyTypeDto } from './dto/my-type.dto';

@Controller('my-controller')
export class MyController {

  @Post()
  @ApiBody({ type: MyTypeDto })
  @ApiConsumes('multipart/form-data')
  async myMethod() {
    return 'ok';
  }
}

What is the motivation / use case for changing the behavior?

Environment


platform-express version : 6.11.4
passport version         : 6.1.0
swagger version          : 4.2.3
common version           : 6.11.4
core version             : 6.11.4
jwt version              : 6.1.1

For Tooling issues:
- Node version: v10.18.1
- Platform:  macOS Mojave

Others:

Other helpful infos

I'm using Nest.js' monorepo feature. Swagger plugin is enabled in nest-cli.json

  "compilerOptions": {
    "webpack": true,
    "tsConfigPath": "apps/public-api/tsconfig.app.json",
    "plugins": [
      {
        "name": "@nestjs/swagger/plugin",
        "options": {
          "dtoFileNameSuffix": [
            ".dto.ts"
          ],
          "classValidatorShim": false
        }
      }
    ]
  }

P.S. Should I create this issue for @nestjs/swagger repo or swagger-ui-express?

kamilmysliwiec commented 4 years ago

P.S. Should I create this issue for @nestjs/swagger repo or swagger-ui-express?

This issue should be reported in the swagger-ui-express repo

lock[bot] commented 4 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.