最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - empty JSON parameters for array POST in swagger - Stack Overflow

programmeradmin0浏览0评论

I'm using nest.js and I have a post route to add new news to database so I used postman to send array of objects like this:

[
{ 
    "newsTitle" : "title1",
    "newsDescription": "description1"
},
{ 
    "newsTitle" : "title2",
    "newsDescription": "description2"
}
]

and this the code for post in my controller:

@Post()
  async create(@Body() body: NewsDto[]) {

    const len = body.length;

    if (len == 1) {
    }

    else if (len > 1) {
    }

    return this.newsService.createNews(body);
  }

so everything work fine in post and saving data in database but when I use swagger I get the Model of for the dto of this controller like this:

You can see that the the parameters of dto not displayed here and I get the "Array" title instead because I use @Body() body: NewsDto[] and it's array as you see

also here in the post I can't get the JSON so I can add it or post it in another word

so how to handle this so when the length of array only 1 object then I return NewsDto parameters and if the length of array more than 1 object so return the NewsDto parameters too instead of Array?

I'm using nest.js and I have a post route to add new news to database so I used postman to send array of objects like this:

[
{ 
    "newsTitle" : "title1",
    "newsDescription": "description1"
},
{ 
    "newsTitle" : "title2",
    "newsDescription": "description2"
}
]

and this the code for post in my controller:

@Post()
  async create(@Body() body: NewsDto[]) {

    const len = body.length;

    if (len == 1) {
    }

    else if (len > 1) {
    }

    return this.newsService.createNews(body);
  }

so everything work fine in post and saving data in database but when I use swagger I get the Model of for the dto of this controller like this:

You can see that the the parameters of dto not displayed here and I get the "Array" title instead because I use @Body() body: NewsDto[] and it's array as you see

also here in the post I can't get the JSON so I can add it or post it in another word

so how to handle this so when the length of array only 1 object then I return NewsDto parameters and if the length of array more than 1 object so return the NewsDto parameters too instead of Array?

Share Improve this question edited Feb 3, 2019 at 20:48 Kim Kern 60.7k20 gold badges218 silver badges214 bronze badges asked Feb 3, 2019 at 20:05 muklahmuklah 8652 gold badges11 silver badges20 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

You have to add the @ApiModelProperty() decorator to your DTO's properties:

export class NewsDto {
  @ApiModelProperty()
  newsTitle: string;

  @ApiModelProperty()
  newsDescription: string;
}

Then add @ApiImplicitBody() to the controller method:

@Post()
@ApiImplicitBody({ name: 'news', type: [NewsDto]})
async create(@Body('news') body: NewsDto[]) {
发布评论

评论列表(0)

  1. 暂无评论