docs: improved auto-generated openapi docs

With these additional annotations the openapi docs under `/apidoc` and `/private/apidoc` will be improved by adding errors that the requests can return

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2022-01-17 11:35:57 +01:00
parent 2bc8c0d6da
commit 796b8294cf
10 changed files with 183 additions and 34 deletions

View file

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
@ -15,9 +15,14 @@ import {
UnauthorizedException,
UseGuards,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
ApiBadRequestResponse,
ApiConflictResponse,
ApiNotFoundResponse,
ApiTags,
ApiUnauthorizedResponse,
ApiUnprocessableEntityResponse,
} from '@nestjs/swagger';
import { SessionGuard } from '../../../identity/session.guard';
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
@ -29,6 +34,13 @@ import { NotesService } from '../../../notes/notes.service';
import { PermissionsService } from '../../../permissions/permissions.service';
import { User } from '../../../users/user.entity';
import { UsersService } from '../../../users/users.service';
import {
badRequestDescription,
conflictDescription,
notFoundDescription,
unauthorizedDescription,
unprocessableEntityDescription,
} from '../../utils/descriptions';
import { RequestUser } from '../../utils/request-user.decorator';
@UseGuards(SessionGuard)
@ -46,6 +58,9 @@ export class AliasController {
}
@Post()
@ApiConflictResponse({ description: conflictDescription })
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
async addAlias(
@RequestUser() user: User,
@Body() newAliasDto: AliasCreateDto,
@ -64,6 +79,9 @@ export class AliasController {
}
@Put(':alias')
@ApiBadRequestResponse({ description: badRequestDescription })
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
async makeAliasPrimary(
@RequestUser() user: User,
@Param('alias') alias: string,
@ -84,6 +102,11 @@ export class AliasController {
@Delete(':alias')
@HttpCode(204)
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
@ApiUnprocessableEntityResponse({
description: unprocessableEntityDescription,
})
async removeAlias(
@RequestUser() user: User,
@Param('alias') alias: string,

View file

@ -1,12 +1,11 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import {
BadRequestException,
Body,
ConflictException,
Controller,
Delete,
Post,
@ -14,10 +13,14 @@ import {
Req,
UseGuards,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
ApiBadRequestResponse,
ApiConflictResponse,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { Session } from 'express-session';
import { AlreadyInDBError } from '../../../errors/errors';
import { IdentityService } from '../../../identity/identity.service';
import { LocalAuthGuard } from '../../../identity/local/local.strategy';
import { LoginDto } from '../../../identity/local/login.dto';
@ -27,6 +30,11 @@ import { SessionGuard } from '../../../identity/session.guard';
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
import { User } from '../../../users/user.entity';
import { UsersService } from '../../../users/users.service';
import {
badRequestDescription,
conflictDescription,
unauthorizedDescription,
} from '../../utils/descriptions';
import { LoginEnabledGuard } from '../../utils/login-enabled.guard';
import { RegistrationEnabledGuard } from '../../utils/registration-enabled.guard';
import { RequestUser } from '../../utils/request-user.decorator';
@ -44,29 +52,21 @@ export class AuthController {
@UseGuards(RegistrationEnabledGuard)
@Post('local')
@ApiBadRequestResponse({ description: badRequestDescription })
@ApiConflictResponse({ description: conflictDescription })
async registerUser(@Body() registerDto: RegisterDto): Promise<void> {
try {
const user = await this.usersService.createUser(
registerDto.username,
registerDto.displayname,
);
// ToDo: Figure out how to rollback user if anything with this calls goes wrong
await this.identityService.createLocalIdentity(
user,
registerDto.password,
);
return;
} catch (e) {
// This special handling can't be omitted since AlreadyInDBErrors get mapped to BadRequestException usually.
if (e instanceof AlreadyInDBError) {
throw new ConflictException(e.message);
}
throw e;
}
await this.identityService.createLocalIdentity(user, registerDto.password);
}
@UseGuards(LoginEnabledGuard, SessionGuard)
@Put('local')
@ApiBadRequestResponse({ description: badRequestDescription })
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
async updatePassword(
@RequestUser() user: User,
@Body() changePasswordDto: UpdatePasswordDto,
@ -84,6 +84,7 @@ export class AuthController {
@UseGuards(LoginEnabledGuard, LocalAuthGuard)
@Post('local/login')
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
login(
@Req() request: Request & { session: { user: string } },
@Body() loginDto: LoginDto,
@ -94,6 +95,7 @@ export class AuthController {
@UseGuards(SessionGuard)
@Delete('logout')
@ApiBadRequestResponse({ description: badRequestDescription })
logout(@Req() request: Request & { session: Session }): void {
request.session.destroy((err) => {
if (err) {

View file

@ -13,7 +13,11 @@ import {
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
ApiNotFoundResponse,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { HistoryEntryImportDto } from '../../../../history/history-entry-import.dto';
import { HistoryEntryUpdateDto } from '../../../../history/history-entry-update.dto';
@ -23,6 +27,10 @@ import { SessionGuard } from '../../../../identity/session.guard';
import { ConsoleLoggerService } from '../../../../logger/console-logger.service';
import { Note } from '../../../../notes/note.entity';
import { User } from '../../../../users/user.entity';
import {
notFoundDescription,
unauthorizedDescription,
} from '../../../utils/descriptions';
import { GetNoteInterceptor } from '../../../utils/get-note.interceptor';
import { RequestNote } from '../../../utils/request-note.decorator';
import { RequestUser } from '../../../utils/request-user.decorator';
@ -39,6 +47,8 @@ export class HistoryController {
}
@Get()
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
async getHistory(@RequestUser() user: User): Promise<HistoryEntryDto[]> {
const foundEntries = await this.historyService.getEntriesByUser(user);
return await Promise.all(
@ -47,6 +57,8 @@ export class HistoryController {
}
@Post()
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
async setHistory(
@RequestUser() user: User,
@Body('history') history: HistoryEntryImportDto[],
@ -55,11 +67,15 @@ export class HistoryController {
}
@Delete()
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
async deleteHistory(@RequestUser() user: User): Promise<void> {
await this.historyService.deleteHistory(user);
}
@Put(':noteIdOrAlias')
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
@UseInterceptors(GetNoteInterceptor)
async updateHistoryEntry(
@RequestNote() note: Note,
@ -75,6 +91,8 @@ export class HistoryController {
}
@Delete(':noteIdOrAlias')
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
@UseInterceptors(GetNoteInterceptor)
async deleteHistoryEntry(
@RequestNote() note: Note,

View file

@ -12,7 +12,12 @@ import {
Post,
UseGuards,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
ApiInternalServerErrorResponse,
ApiNotFoundResponse,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { SessionGuard } from '../../../identity/session.guard';
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
@ -21,6 +26,11 @@ import { MediaService } from '../../../media/media.service';
import { UserInfoDto } from '../../../users/user-info.dto';
import { User } from '../../../users/user.entity';
import { UsersService } from '../../../users/users.service';
import {
internalServerErrorDescription,
notFoundDescription,
unauthorizedDescription,
} from '../../utils/descriptions';
import { RequestUser } from '../../utils/request-user.decorator';
@UseGuards(SessionGuard)
@ -35,11 +45,13 @@ export class MeController {
this.logger.setContext(MeController.name);
}
@Get()
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
getMe(@RequestUser() user: User): UserInfoDto {
return this.userService.toUserDto(user);
}
@Get('media')
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
async getMyMedia(@RequestUser() user: User): Promise<MediaUploadDto[]> {
const media = await this.mediaService.listUploadsByUser(user);
return await Promise.all(
@ -49,6 +61,11 @@ export class MeController {
@Delete()
@HttpCode(204)
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
@ApiInternalServerErrorResponse({
description: internalServerErrorDescription,
})
async deleteUser(@RequestUser() user: User): Promise<void> {
const mediaUploads = await this.mediaService.listUploadsByUser(user);
for (const mediaUpload of mediaUploads) {
@ -61,6 +78,7 @@ export class MeController {
@Post('profile')
@HttpCode(200)
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
async updateDisplayName(
@RequestUser() user: User,
@Body('name') newDisplayName: string,

View file

@ -16,12 +16,15 @@ import {
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import {
ApiBadRequestResponse,
ApiBody,
ApiConsumes,
ApiCreatedResponse,
ApiForbiddenResponse,
ApiHeader,
ApiInternalServerErrorResponse,
ApiNoContentResponse,
ApiNotFoundResponse,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
@ -36,7 +39,10 @@ import { Note } from '../../../notes/note.entity';
import { NotesService } from '../../../notes/notes.service';
import { User } from '../../../users/user.entity';
import {
badRequestDescription,
forbiddenDescription,
internalServerErrorDescription,
notFoundDescription,
successfullyDeletedDescription,
unauthorizedDescription,
} from '../../utils/descriptions';
@ -72,14 +78,19 @@ export class MediaController {
name: 'HedgeDoc-Note',
description: 'ID or alias of the parent note',
})
@UseInterceptors(FileInterceptor('file'))
@HttpCode(201)
@ApiCreatedResponse({
description: 'The file was uploaded successfully',
type: MediaUploadUrlDto,
})
@ApiBadRequestResponse({ description: badRequestDescription })
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiForbiddenResponse({ description: forbiddenDescription })
@UseInterceptors(FileInterceptor('file'))
@HttpCode(201)
@ApiNotFoundResponse({ description: notFoundDescription })
@ApiInternalServerErrorResponse({
description: internalServerErrorDescription,
})
async uploadMedia(
@UploadedFile() file: MulterFile,
@Headers('HedgeDoc-Note') noteId: string,
@ -99,6 +110,9 @@ export class MediaController {
@HttpCode(204)
@ApiNoContentResponse({ description: successfullyDeletedDescription })
@FullApi
@ApiInternalServerErrorResponse({
description: internalServerErrorDescription,
})
async deleteMedia(
@RequestUser() user: User,
@Param('filename') filename: string,

View file

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
@ -14,7 +14,14 @@ import {
UseGuards,
UseInterceptors,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
ApiBadRequestResponse,
ApiConflictResponse,
ApiInternalServerErrorResponse,
ApiNotFoundResponse,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { HistoryService } from '../../../history/history.service';
import { SessionGuard } from '../../../identity/session.guard';
@ -32,6 +39,13 @@ import { RevisionDto } from '../../../revisions/revision.dto';
import { RevisionsService } from '../../../revisions/revisions.service';
import { User } from '../../../users/user.entity';
import { UsersService } from '../../../users/users.service';
import {
badRequestDescription,
conflictDescription,
internalServerErrorDescription,
notFoundDescription,
unauthorizedDescription,
} from '../../utils/descriptions';
import { GetNoteInterceptor } from '../../utils/get-note.interceptor';
import { MarkdownBody } from '../../utils/markdownbody-decorator';
import { PermissionsGuard } from '../../utils/permissions.guard';
@ -54,6 +68,7 @@ export class NotesController {
}
@Get(':noteIdOrAlias')
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@Permissions(Permission.READ)
@UseInterceptors(GetNoteInterceptor)
@UseGuards(PermissionsGuard)
@ -66,6 +81,7 @@ export class NotesController {
}
@Get(':noteIdOrAlias/media')
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@Permissions(Permission.READ)
@UseInterceptors(GetNoteInterceptor)
@UseGuards(PermissionsGuard)
@ -78,6 +94,7 @@ export class NotesController {
@Post()
@HttpCode(201)
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@Permissions(Permission.CREATE)
@UseGuards(PermissionsGuard)
async createNote(
@ -92,6 +109,10 @@ export class NotesController {
@Post(':noteAlias')
@HttpCode(201)
@ApiBadRequestResponse({ description: badRequestDescription })
@ApiConflictResponse({ description: conflictDescription })
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
@Permissions(Permission.CREATE)
@UseGuards(PermissionsGuard)
async createNamedNote(
@ -107,6 +128,11 @@ export class NotesController {
@Delete(':noteIdOrAlias')
@HttpCode(204)
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
@ApiInternalServerErrorResponse({
description: internalServerErrorDescription,
})
@Permissions(Permission.OWNER)
@UseInterceptors(GetNoteInterceptor)
@UseGuards(PermissionsGuard)
@ -130,6 +156,8 @@ export class NotesController {
}
@Get(':noteIdOrAlias/revisions')
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
@Permissions(Permission.READ)
@UseInterceptors(GetNoteInterceptor)
@UseGuards(PermissionsGuard)
@ -147,6 +175,8 @@ export class NotesController {
@Delete(':noteIdOrAlias/revisions')
@HttpCode(204)
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
@Permissions(Permission.READ)
@UseInterceptors(GetNoteInterceptor)
@UseGuards(PermissionsGuard)
@ -167,6 +197,8 @@ export class NotesController {
}
@Get(':noteIdOrAlias/revisions/:revisionId')
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
@Permissions(Permission.READ)
@UseInterceptors(GetNoteInterceptor)
@UseGuards(PermissionsGuard)

View file

@ -14,7 +14,11 @@ import {
UnauthorizedException,
UseGuards,
} from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import {
ApiNotFoundResponse,
ApiTags,
ApiUnauthorizedResponse,
} from '@nestjs/swagger';
import { AuthTokenWithSecretDto } from '../../../auth/auth-token-with-secret.dto';
import { AuthTokenDto } from '../../../auth/auth-token.dto';
@ -23,6 +27,10 @@ import { SessionGuard } from '../../../identity/session.guard';
import { ConsoleLoggerService } from '../../../logger/console-logger.service';
import { User } from '../../../users/user.entity';
import { TimestampMillis } from '../../../utils/timestamp';
import {
notFoundDescription,
unauthorizedDescription,
} from '../../utils/descriptions';
import { RequestUser } from '../../utils/request-user.decorator';
@UseGuards(SessionGuard)
@ -37,6 +45,7 @@ export class TokensController {
}
@Get()
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
async getUserTokens(@RequestUser() user: User): Promise<AuthTokenDto[]> {
return (await this.authService.getTokensByUser(user)).map((token) =>
this.authService.toAuthTokenDto(token),
@ -44,6 +53,7 @@ export class TokensController {
}
@Post()
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
async postTokenRequest(
@Body('label') label: string,
@Body('validUntil') validUntil: TimestampMillis,
@ -54,6 +64,8 @@ export class TokensController {
@Delete('/:keyId')
@HttpCode(204)
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
async deleteToken(
@RequestUser() user: User,
@Param('keyId') keyId: string,

View file

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
@ -20,6 +20,7 @@ import {
ApiOkResponse,
ApiSecurity,
ApiTags,
ApiUnprocessableEntityResponse,
} from '@nestjs/swagger';
import { TokenAuthGuard } from '../../../auth/token.strategy';
@ -31,6 +32,7 @@ import { AliasService } from '../../../notes/alias.service';
import { NotesService } from '../../../notes/notes.service';
import { PermissionsService } from '../../../permissions/permissions.service';
import { User } from '../../../users/user.entity';
import { unprocessableEntityDescription } from '../../utils/descriptions';
import { FullApi } from '../../utils/fullapi-decorator';
import { RequestUser } from '../../utils/request-user.decorator';
@ -101,6 +103,9 @@ export class AliasController {
description: 'The alias was deleted',
})
@FullApi
@ApiUnprocessableEntityResponse({
description: unprocessableEntityDescription,
})
async removeAlias(
@RequestUser() user: User,
@Param('alias') alias: string,

View file

@ -16,12 +16,15 @@ import {
} from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import {
ApiBadRequestResponse,
ApiBody,
ApiConsumes,
ApiCreatedResponse,
ApiForbiddenResponse,
ApiHeader,
ApiInternalServerErrorResponse,
ApiNoContentResponse,
ApiNotFoundResponse,
ApiSecurity,
ApiTags,
ApiUnauthorizedResponse,
@ -37,7 +40,10 @@ import { Note } from '../../../notes/note.entity';
import { NotesService } from '../../../notes/notes.service';
import { User } from '../../../users/user.entity';
import {
badRequestDescription,
forbiddenDescription,
internalServerErrorDescription,
notFoundDescription,
successfullyDeletedDescription,
unauthorizedDescription,
} from '../../utils/descriptions';
@ -78,8 +84,13 @@ export class MediaController {
description: 'The file was uploaded successfully',
type: MediaUploadUrlDto,
})
@ApiBadRequestResponse({ description: badRequestDescription })
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiForbiddenResponse({ description: forbiddenDescription })
@ApiNotFoundResponse({ description: notFoundDescription })
@ApiInternalServerErrorResponse({
description: internalServerErrorDescription,
})
@UseInterceptors(FileInterceptor('file'))
@HttpCode(201)
async uploadMedia(
@ -100,6 +111,9 @@ export class MediaController {
@Delete(':filename')
@HttpCode(204)
@ApiNoContentResponse({ description: successfullyDeletedDescription })
@ApiInternalServerErrorResponse({
description: internalServerErrorDescription,
})
@FullApi
async deleteMedia(
@RequestUser() user: User,

View file

@ -1,5 +1,5 @@
/*
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
@ -17,8 +17,11 @@ import {
UseInterceptors,
} from '@nestjs/common';
import {
ApiBadRequestResponse,
ApiConflictResponse,
ApiCreatedResponse,
ApiForbiddenResponse,
ApiInternalServerErrorResponse,
ApiNoContentResponse,
ApiOkResponse,
ApiProduces,
@ -48,7 +51,10 @@ import { RevisionDto } from '../../../revisions/revision.dto';
import { RevisionsService } from '../../../revisions/revisions.service';
import { User } from '../../../users/user.entity';
import {
badRequestDescription,
conflictDescription,
forbiddenDescription,
internalServerErrorDescription,
successfullyDeletedDescription,
unauthorizedDescription,
} from '../../utils/descriptions';
@ -115,6 +121,8 @@ export class NotesController {
description: 'Get information about the newly created note',
type: NoteDto,
})
@ApiBadRequestResponse({ description: badRequestDescription })
@ApiConflictResponse({ description: conflictDescription })
@ApiUnauthorizedResponse({ description: unauthorizedDescription })
@ApiForbiddenResponse({ description: forbiddenDescription })
async createNamedNote(
@ -135,6 +143,9 @@ export class NotesController {
@HttpCode(204)
@ApiNoContentResponse({ description: successfullyDeletedDescription })
@FullApi
@ApiInternalServerErrorResponse({
description: internalServerErrorDescription,
})
async deleteNote(
@RequestUser() user: User,
@RequestNote() note: Note,