diff --git a/package.json b/package.json index fd9e697a..b6399cd5 100644 --- a/package.json +++ b/package.json @@ -53,10 +53,10 @@ "test:update": "jest --updateSnapshot", "test:watch": "jest --watch", "test:coverage": "jest --coverage", - "eslint": "eslint \"./src/**/*.{js,ts}\" \"./test/**/*.{js,ts}\" \"./bin/index.js\"", - "eslint:fix": "eslint \"./src/**/*.{js,ts}\" \"./test/**/*.{js,ts}\" \"./bin/index.js\" --fix", - "prettier": "prettier \"./src/**/*.{js,ts}\" \"./test/**/*.{js,ts}\" \"./bin/index.js\" --check", - "prettier:fix": "prettier \"./src/**/*.{js,ts}\" \"./test/**/*.{js,ts}\" \"./bin/index.js\" --write", + "eslint": "eslint \"./src/**/*.ts\" \"./test/**/*.ts\" \"./bin/index.js\"", + "eslint:fix": "eslint \"./src/**/*.ts\" \"./test/**/*.ts\" \"./bin/index.js\" --fix", + "prettier": "prettier \"./src/**/*.ts\" \"./test/**/*.ts\" \"./bin/index.js\" --check", + "prettier:fix": "prettier \"./src/**/*.ts\" \"./test/**/*.ts\" \"./bin/index.js\" --write", "prepublish": "yarn run clean && yarn run release", "codecov": "codecov --token=66c30c23-8954-4892-bef9-fbaed0a2e42b" }, diff --git a/test/server/src/AppModule.ts b/test/server/src/AppModule.ts index 5cd8d95d..6181a8bb 100644 --- a/test/server/src/AppModule.ts +++ b/test/server/src/AppModule.ts @@ -1,12 +1,11 @@ import { Module } from '@nestjs/common'; -import { DefaultsController } from './controllers/DefaultsController'; import { ParametersController } from './controllers/ParametersController'; import { ResponseController } from './controllers/ResponseController'; import { SimpleController } from './controllers/SimpleController'; @Module({ - controllers: [SimpleController, ParametersController, DefaultsController, ResponseController], + controllers: [SimpleController, ParametersController, ResponseController], }) export class AppModule { // diff --git a/test/server/src/controllers/DefaultsController.ts b/test/server/src/controllers/DefaultsController.ts deleted file mode 100644 index 5bbe96bb..00000000 --- a/test/server/src/controllers/DefaultsController.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { Controller, Get, Param } from '@nestjs/common'; -import { ApiResponse, ApiTags } from '@nestjs/swagger'; - -import { ModelWithString } from '../models/ModelWithString'; - -/* eslint-disable @typescript-eslint/no-unused-vars */ -@ApiTags('defaults') -@Controller('defaults') -export class DefaultsController { - @Get('monkey') - @ApiResponse({ - status: 200, - type: ModelWithString, - }) - monkey(@Param('id') id: string): ModelWithString { - return { - prop: 'Hello World!', - }; - } -} diff --git a/test/server/src/controllers/ParametersController.ts b/test/server/src/controllers/ParametersController.ts index 4ac90101..71f9eae2 100644 --- a/test/server/src/controllers/ParametersController.ts +++ b/test/server/src/controllers/ParametersController.ts @@ -1,4 +1,4 @@ -import { Controller, Get, Param } from '@nestjs/common'; +import { Body, Controller, Get, Param, Post } from '@nestjs/common'; import { ApiResponse, ApiTags } from '@nestjs/swagger'; import { ModelWithString } from '../models/ModelWithString'; @@ -7,14 +7,72 @@ import { ModelWithString } from '../models/ModelWithString'; @ApiTags('parameters') @Controller('parameters') export class ParametersController { - @Get('monkey') + @Get('callWithParameters') + public callWithParameters( + @Param('parameterString') parameterString: string, + @Param('parameterNumber') parameterNumber: number, + @Param('parameterBoolean') parameterBoolean: boolean, + @Param('parameterDictionary') parameterDictionary: Record + ): any { + return { + parameterString, + parameterNumber, + parameterBoolean, + parameterDictionary, + }; + } + + @Get('callWithWeirdParameterNames') + public callWithWeirdParameterNames( + @Param('parameter.1') parameter1: string, + @Param('parameter-2') parameter2: string, + @Param('parameter_3') parameter3: string, + @Param('PARAMETER.4') parameter4: string, + @Param('PARAMETER+5') parameter5: string, + @Param('PARAMETER&6') parameter6: string + ): any { + return { + parameter1, + parameter2, + parameter3, + parameter4, + parameter5, + parameter6, + }; + } + + @Get('callWithDefaultParameters') @ApiResponse({ status: 200, type: ModelWithString, }) - monkey(@Param('id') id: string): ModelWithString { + public callWithDefaultParameters( + @Param('parameterNumberWithDefault') parameterNumberWithDefault: number = 123, + @Param('parameterBooleanWithDefault') parameterBooleanWithDefault: boolean = true, + @Param('parameterStringWithDefault') parameterStringWithDefault: string = 'Hello World!' + ): any { return { - prop: 'Hello World!', + parameterNumberWithDefault, + parameterBooleanWithDefault, + parameterStringWithDefault, }; } + + @Get('getCallWithBody') + @ApiResponse({ + status: 200, + type: ModelWithString, + }) + public getCallWithBody(@Body() body: ModelWithString): ModelWithString { + return body; + } + + @Post('postCallWithBody') + @ApiResponse({ + status: 200, + type: ModelWithString, + }) + public postCallWithBody(@Body() body: ModelWithString): ModelWithString { + return body; + } } diff --git a/test/server/src/controllers/ResponseController.ts b/test/server/src/controllers/ResponseController.ts index 2e31fd1c..cbda6d6c 100644 --- a/test/server/src/controllers/ResponseController.ts +++ b/test/server/src/controllers/ResponseController.ts @@ -1,20 +1,129 @@ -import { Controller, Get, Param } from '@nestjs/common'; +import { Controller, Get } from '@nestjs/common'; import { ApiResponse, ApiTags } from '@nestjs/swagger'; +import { ModelThatExtends } from '../models/ModelThatExtends'; +import { ModelThatExtendsExtends } from '../models/ModelThatExtendsExtends'; +import { ModelWithArray } from '../models/ModelWithArray'; +import { ModelWithBoolean } from '../models/ModelWithBoolean'; +import { ModelWithDictionary } from '../models/ModelWithDictionary'; +import { ModelWithInteger } from '../models/ModelWithInteger'; +import { ModelWithProperties } from '../models/ModelWithProperties'; +import { ModelWithReference } from '../models/ModelWithReference'; import { ModelWithString } from '../models/ModelWithString'; /* eslint-disable @typescript-eslint/no-unused-vars */ @ApiTags('response') @Controller('response') export class ResponseController { - @Get('monkey') - @ApiResponse({ - status: 200, - type: ModelWithString, - }) - monkey(@Param('id') id: string): ModelWithString { + @Get('getString') + @ApiResponse({ status: 200, type: String }) + public getString(): string { + return 'Hello World!'; + } + + @Get('getNumber') + @ApiResponse({ status: 200, type: Number }) + public getNumber(): number { + return 123; + } + + @Get('getBoolean') + @ApiResponse({ status: 200, type: Boolean }) + public getBoolean(): boolean { + return true; + } + + @Get('getModelWithString') + @ApiResponse({ status: 200, type: ModelWithString }) + public getModelWithString(): ModelWithString { return { prop: 'Hello World!', }; } + + @Get('getModelWithInteger') + @ApiResponse({ status: 200, type: ModelWithInteger }) + public getModelWithInteger(): ModelWithInteger { + return { + prop: 123, + }; + } + + @Get('getModelWithBoolean') + @ApiResponse({ status: 200, type: ModelWithBoolean }) + public getModelWithBoolean(): ModelWithBoolean { + return { + prop: true, + }; + } + + @Get('getModelWithArray') + @ApiResponse({ status: 200, type: ModelWithArray }) + public getModelWithArray(): ModelWithArray { + return { + prop: ['foo', 'bar'], + }; + } + + @Get('getModelWithDictionary') + @ApiResponse({ status: 200, type: ModelWithDictionary }) + public getModelWithDictionary(): ModelWithDictionary { + return { + prop: { + foo: 'bar', + }, + }; + } + + @Get('getModelWithReference') + @ApiResponse({ status: 200, type: ModelWithReference }) + public getModelWithReference(): ModelWithReference { + return { + prop: { + prop: 'Hello World!', + }, + }; + } + + @Get('getModelWithProperties') + @ApiResponse({ status: 200, type: ModelWithProperties }) + public getModelWithProperties(): ModelWithProperties { + return { + string: 'Hello World!', + number: 123, + boolean: true, + array: ['foo', 'bar'], + dictionary: { + foo: 'bar', + }, + }; + } + + @Get('getModelThatExtends') + @ApiResponse({ status: 200, type: ModelThatExtends }) + public getModelThatExtends(): ModelThatExtends { + return { + prop: 'prop', + propertyA: 'propertyA', + propertyB: { + prop: 'propertyB', + }, + }; + } + + @Get('getModelThatExtendsExtends') + @ApiResponse({ status: 200, type: ModelThatExtendsExtends }) + public getModelThatExtendsExtends(): ModelThatExtendsExtends { + return { + prop: 'prop', + propertyA: 'propertyA', + propertyB: { + prop: 'propertyB', + }, + propertyC: 'propertyC', + propertyD: { + prop: 'propertyD', + }, + }; + } } diff --git a/test/server/src/controllers/SimpleController.ts b/test/server/src/controllers/SimpleController.ts index 944ddaf2..f375ef64 100644 --- a/test/server/src/controllers/SimpleController.ts +++ b/test/server/src/controllers/SimpleController.ts @@ -1,20 +1,42 @@ -import { Controller, Get, Param } from '@nestjs/common'; -import { ApiResponse, ApiTags } from '@nestjs/swagger'; - -import { ModelWithString } from '../models/ModelWithString'; +import { Controller, Delete, Get, Head, Options, Patch, Post, Put } from '@nestjs/common'; +import { ApiTags } from '@nestjs/swagger'; /* eslint-disable @typescript-eslint/no-unused-vars */ @ApiTags('simple') @Controller('simple') export class SimpleController { - @Get('monkey') - @ApiResponse({ - status: 200, - type: ModelWithString, - }) - monkey(@Param('id') id: string): ModelWithString { - return { - prop: 'Hello World!', - }; + @Get('getCallWithoutParametersAndResponse') + public getCallWithoutParametersAndResponse(): void { + // + } + + @Put('putCallWithoutParametersAndResponse') + public putCallWithoutParametersAndResponse(): void { + // + } + + @Post('postCallWithoutParametersAndResponse') + public postCallWithoutParametersAndResponse(): void { + // + } + + @Delete('deleteCallWithoutParametersAndResponse') + public deleteCallWithoutParametersAndResponse(): void { + // + } + + @Options('optionsCallWithoutParametersAndResponse') + public optionsCallWithoutParametersAndResponse(): void { + // + } + + @Head('headCallWithoutParametersAndResponse') + public headCallWithoutParametersAndResponse(): void { + // + } + + @Patch('patchCallWithoutParametersAndResponse') + public patchCallWithoutParametersAndResponse(): void { + // } } diff --git a/test/server/src/main.ts b/test/server/src/main.ts index 0648b311..74fda9f7 100644 --- a/test/server/src/main.ts +++ b/test/server/src/main.ts @@ -3,10 +3,17 @@ import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger'; import { AppModule } from './AppModule'; +/** + * This is a simple OpenAPI test server that we use to run e2e tests. You can find + * more information inside the controllers and models of this server. + * When you run this server the following urls are available: + * - Swagger UI: http://localhost:3000/api + * - Swagger Specification: http://localhost:3000/api-json + */ async function bootstrap(): Promise { const app = await NestFactory.create(AppModule); - const options = new DocumentBuilder().setTitle('OpenAPI').setDescription('The OpenAPI description').setVersion('1.0').build(); + const options = new DocumentBuilder().setTitle('OpenAPI').setVersion('1.0').build(); const document = SwaggerModule.createDocument(app, options); diff --git a/test/server/src/models/ModelThatExtends.ts b/test/server/src/models/ModelThatExtends.ts index b845a5a9..0353cd54 100644 --- a/test/server/src/models/ModelThatExtends.ts +++ b/test/server/src/models/ModelThatExtends.ts @@ -1,6 +1,11 @@ import { ApiProperty } from '@nestjs/swagger'; -export class ModelThatExtends { +import { ModelWithString } from './ModelWithString'; + +export class ModelThatExtends extends ModelWithString { @ApiProperty() - prop?: number; + public readonly propertyA?: string; + + @ApiProperty() + public readonly propertyB?: ModelWithString; } diff --git a/test/server/src/models/ModelThatExtendsExtends.ts b/test/server/src/models/ModelThatExtendsExtends.ts index 2b872581..fe43cf3e 100644 --- a/test/server/src/models/ModelThatExtendsExtends.ts +++ b/test/server/src/models/ModelThatExtendsExtends.ts @@ -1,6 +1,12 @@ import { ApiProperty } from '@nestjs/swagger'; -export class ModelThatExtendsExtends { +import { ModelThatExtends } from './ModelThatExtends'; +import { ModelWithString } from './ModelWithString'; + +export class ModelThatExtendsExtends extends ModelThatExtends { @ApiProperty() - prop?: number; + public readonly propertyC?: string; + + @ApiProperty() + public readonly propertyD?: ModelWithString; } diff --git a/test/server/src/models/ModelWithArray.ts b/test/server/src/models/ModelWithArray.ts index 02fb56d7..ba9ed947 100644 --- a/test/server/src/models/ModelWithArray.ts +++ b/test/server/src/models/ModelWithArray.ts @@ -2,5 +2,5 @@ import { ApiProperty } from '@nestjs/swagger'; export class ModelWithArray { @ApiProperty() - prop?: number; + public readonly prop?: string[]; } diff --git a/test/server/src/models/ModelWithBoolean.ts b/test/server/src/models/ModelWithBoolean.ts index ee5348a3..21e9fdd8 100644 --- a/test/server/src/models/ModelWithBoolean.ts +++ b/test/server/src/models/ModelWithBoolean.ts @@ -2,5 +2,5 @@ import { ApiProperty } from '@nestjs/swagger'; export class ModelWithBoolean { @ApiProperty() - prop?: number; + public readonly prop?: boolean; } diff --git a/test/server/src/models/ModelWithDictionary.ts b/test/server/src/models/ModelWithDictionary.ts index e75053eb..8c69e6c6 100644 --- a/test/server/src/models/ModelWithDictionary.ts +++ b/test/server/src/models/ModelWithDictionary.ts @@ -2,5 +2,5 @@ import { ApiProperty } from '@nestjs/swagger'; export class ModelWithDictionary { @ApiProperty() - prop?: number; + public readonly prop?: Record; } diff --git a/test/server/src/models/ModelWithInteger.ts b/test/server/src/models/ModelWithInteger.ts index 5a1773e9..b5d37554 100644 --- a/test/server/src/models/ModelWithInteger.ts +++ b/test/server/src/models/ModelWithInteger.ts @@ -2,5 +2,5 @@ import { ApiProperty } from '@nestjs/swagger'; export class ModelWithInteger { @ApiProperty() - prop?: number; + public readonly prop?: number; } diff --git a/test/server/src/models/ModelWithPattern.ts b/test/server/src/models/ModelWithPattern.ts deleted file mode 100644 index 19e24f4c..00000000 --- a/test/server/src/models/ModelWithPattern.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { ApiProperty } from '@nestjs/swagger'; - -export class ModelWithPattern { - @ApiProperty() - prop?: number; -} diff --git a/test/server/src/models/ModelWithProperties.ts b/test/server/src/models/ModelWithProperties.ts index a7f04d5d..f958c9eb 100644 --- a/test/server/src/models/ModelWithProperties.ts +++ b/test/server/src/models/ModelWithProperties.ts @@ -2,5 +2,17 @@ import { ApiProperty } from '@nestjs/swagger'; export class ModelWithProperties { @ApiProperty() - prop?: number; + public readonly number?: number; + + @ApiProperty() + public readonly string?: string; + + @ApiProperty() + public readonly boolean?: boolean; + + @ApiProperty() + public readonly array?: string[]; + + @ApiProperty() + public readonly dictionary?: Record; } diff --git a/test/server/src/models/ModelWithReference.ts b/test/server/src/models/ModelWithReference.ts index db83819a..51e03e23 100644 --- a/test/server/src/models/ModelWithReference.ts +++ b/test/server/src/models/ModelWithReference.ts @@ -1,6 +1,8 @@ import { ApiProperty } from '@nestjs/swagger'; +import { ModelWithString } from './ModelWithString'; + export class ModelWithReference { @ApiProperty() - prop?: number; + public readonly prop?: ModelWithString; } diff --git a/test/server/src/models/ModelWithString.ts b/test/server/src/models/ModelWithString.ts index eaecc169..450aef19 100644 --- a/test/server/src/models/ModelWithString.ts +++ b/test/server/src/models/ModelWithString.ts @@ -2,5 +2,5 @@ import { ApiProperty } from '@nestjs/swagger'; export class ModelWithString { @ApiProperty() - prop?: string; + public readonly prop?: string; }