Skip to content

Commit

Permalink
test: add explicit body examples test
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Oct 23, 2024
1 parent 9b61193 commit e8a8453
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 7 deletions.
64 changes: 64 additions & 0 deletions e2e/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,70 @@
]
}
},
"/api/cats/explicit-body": {
"post": {
"operationId": "CatsController_createExplicitBody",
"parameters": [
{
"name": "header",
"in": "header",
"description": "Test",
"required": false,
"schema": {
"type": "string",
"default": "test"
}
},
{
"name": "x-tenant-id",
"in": "header",
"schema": {
"type": "string"
}
}
],
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CreateCatDto"
},
"examples": {
"mau": {
"summary": "Mau example",
"value": {
"name": "Mau cat",
"age": 5,
"breed": "Mau"
}
}
}
}
}
},
"responses": {
"201": {
"description": ""
}
},
"tags": [
"cats"
],
"security": [
{
"key2": [],
"key1": []
},
{
"bearer": []
},
{
"basic": []
}
]
}
},
"/api/cats/{id}": {
"get": {
"operationId": "CatsController_findOne",
Expand Down
27 changes: 23 additions & 4 deletions e2e/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Body, Controller, Get, Param, Post, Query } from '@nestjs/common';
import {
ApiBearerAuth,
ApiBody,
ApiConsumes,
ApiExtension,
ApiHeader,
Expand Down Expand Up @@ -44,6 +45,24 @@ export class CatsController {
return this.catsService.create(createCatDto);
}

@Post('explicit-body')
@ApiBody({
type: CreateCatDto,
examples: {
mau: {
summary: 'Mau example',
value: {
name: 'Mau cat',
age: 5,
breed: 'Mau'
}
}
}
})
async createExplicitBody(@Body() createCatDto: CreateCatDto): Promise<Cat> {
return this.catsService.create(createCatDto);
}

@Get(':id')
@ApiResponse({
status: 200,
Expand All @@ -59,9 +78,9 @@ export class CatsController {
@ApiExtension('x-codeSamples', [
{ lang: 'JavaScript', source: "console.log('Hello World');" }
])
@ApiExtension('x-multiple', { test: "test" })
@ApiTags("tag1")
@ApiTags("tag2")
@ApiExtension('x-multiple', { test: 'test' })
@ApiTags('tag1')
@ApiTags('tag2')
findAll(@Query() paginationQuery: PaginationQuery) {}

@ApiQuery({ type: PaginationQuery })
Expand Down Expand Up @@ -95,7 +114,7 @@ export class CatsController {
@Get('with-enum/:type')
@ApiParam({
name: 'type',
enum: LettersEnum,
enum: LettersEnum
})
getWithEnumParam(@Param('type') type: LettersEnum) {}

Expand Down
14 changes: 11 additions & 3 deletions test/explorer/swagger-explorer.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('SwaggerExplorer', () => {

@ApiProperty({
enum: () => LettersEnum,
enumName: 'LettersEnum',
enumName: 'LettersEnum'
})
enumFunction: LettersEnum;
}
Expand Down Expand Up @@ -979,7 +979,11 @@ describe('SwaggerExplorer', () => {
})
@ApiQuery({ name: 'order', enum: QueryEnum })
@ApiQuery({ name: 'page', enum: ['d', 'e', 'f'] })
find(@Param('objectId') objectId: ParamEnum, @Query('order') order: QueryEnum, @Query('page') page: 'd' | 'e' | 'f'): Promise<Foo[]> {
find(
@Param('objectId') objectId: ParamEnum,
@Query('order') order: QueryEnum,
@Query('page') page: 'd' | 'e' | 'f'
): Promise<Foo[]> {
return Promise.resolve([]);
}
}
Expand All @@ -999,7 +1003,11 @@ describe('SwaggerExplorer', () => {
enumName: 'QueryEnum',
isArray: true
})
findBar(@Param('objectId') objectId: ParamEnum, @Query('order') order: QueryEnum, @Query('page') page: QueryEnum[]): Promise<Foo> {
findBar(
@Param('objectId') objectId: ParamEnum,
@Query('order') order: QueryEnum,
@Query('page') page: QueryEnum[]
): Promise<Foo> {
return Promise.resolve(null);
}
}
Expand Down

0 comments on commit e8a8453

Please sign in to comment.