Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions e2e/express.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
} from '@nestjs/platform-express';
import path from 'path';
import request from 'supertest';
import SwaggerParser from 'swagger-parser';
import { DocumentBuilder, SwaggerModule } from '../lib';
import { ApplicationModule } from './src/app.module';
import { ExpressController } from './src/express.controller';
import { DocumentBuilder, SwaggerModule } from '../lib/index.js';
import { ApplicationModule } from './src/app.module.js';
import { ExpressController } from './src/express.controller.js';
import SwaggerParser = require('@apidevtools/swagger-parser');

describe('Express Swagger', () => {
let app: NestExpressApplication;
Expand Down
8 changes: 4 additions & 4 deletions e2e/fastify.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
} from '@nestjs/platform-fastify';
import path from 'path';
import request from 'supertest';
import SwaggerParser from 'swagger-parser';
import { DocumentBuilder, SwaggerModule } from '../lib';
import { ApplicationModule } from './src/app.module';
import { FastifyController } from './src/fastify.controller';
import { DocumentBuilder, SwaggerModule } from '../lib/index.js';
import { ApplicationModule } from './src/app.module.js';
import { FastifyController } from './src/fastify.controller.js';
import SwaggerParser = require('@apidevtools/swagger-parser');

describe('Fastify Swagger', () => {
let app: NestFastifyApplication;
Expand Down
4 changes: 2 additions & 2 deletions e2e/manual-e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import {
NestFastifyApplication
} from '@nestjs/platform-fastify';
import { join } from 'path';
import { DocumentBuilder, SwaggerModule } from '../lib';
import { ApplicationModule } from './src/app.module';
import { DocumentBuilder, SwaggerModule } from '../lib/index.js';
import { ApplicationModule } from './src/app.module.js';

const port = 4001;
const host = 'localhost';
Expand Down
4 changes: 2 additions & 2 deletions e2e/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { CatsModule } from './cats/cats.module';
import { AppController } from './app.controller.js';
import { CatsModule } from './cats/cats.module.js';

@Module({
imports: [CatsModule],
Expand Down
14 changes: 7 additions & 7 deletions e2e/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import {
ApiSecurity,
ApiTags,
getSchemaPath
} from '../../../lib';
import { CatsService } from './cats.service';
import { Cat } from './classes/cat.class';
import { CreateCatDto } from './dto/create-cat.dto';
import { LettersEnum, PaginationQuery } from './dto/pagination-query.dto';
import { TagDto } from './dto/tag.dto';
import { CatBreed } from './enums/cat-breed.enum';
} from '../../../lib/index.js';
import { CatsService } from './cats.service.js';
import { Cat } from './classes/cat.class.js';
import { CreateCatDto } from './dto/create-cat.dto.js';
import { LettersEnum, PaginationQuery } from './dto/pagination-query.dto.js';
import { TagDto } from './dto/tag.dto.js';
import { CatBreed } from './enums/cat-breed.enum.js';

@ApiSecurity('basic')
@ApiBearerAuth()
Expand Down
4 changes: 2 additions & 2 deletions e2e/src/cats/cats.module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Module } from '@nestjs/common';
import { CatsController } from './cats.controller';
import { CatsService } from './cats.service';
import { CatsController } from './cats.controller.js';
import { CatsService } from './cats.service.js';

@Module({
controllers: [CatsController],
Expand Down
4 changes: 2 additions & 2 deletions e2e/src/cats/cats.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { Cat } from './classes/cat.class';
import { CreateCatDto } from './dto/create-cat.dto';
import { Cat } from './classes/cat.class.js';
import { CreateCatDto } from './dto/create-cat.dto.js';

@Injectable()
export class CatsService {
Expand Down
9 changes: 3 additions & 6 deletions e2e/src/cats/classes/cat.class.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ApiExtension, ApiProperty } from '../../../../lib';
import { LettersEnum } from '../dto/pagination-query.dto';
import { ApiExtension, ApiProperty } from '../../../../lib/index.js';
import { LettersEnum } from '../dto/pagination-query.dto.js';

@ApiExtension('x-schema-extension', { test: 'test' })
@ApiExtension('x-schema-extension-multiple', { test: 'test*2' })
Expand Down Expand Up @@ -102,10 +102,7 @@ export class Cat {
@ApiProperty({
enum: LettersEnum,
enumName: 'LettersEnum',
oneOf: [
{ type: 'string' },
{ type: 'number' }
],
oneOf: [{ type: 'string' }, { type: 'number' }],
description: 'Enum named reference combined with oneOf combinators'
})
enumNamedWithOneOf?: LettersEnum | string | number;
Expand Down
20 changes: 14 additions & 6 deletions e2e/src/cats/dto/create-cat.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { ApiExtension, ApiExtraModels, ApiProperty } from '../../../../lib';
import { XEnumTest } from '../enums/x-enum-test.enum';
import { ExtraModelDto } from './extra-model.dto';
import { LettersEnum } from './pagination-query.dto';
import { TagDto } from './tag.dto';
import {
ApiExtension,
ApiExtraModels,
ApiProperty
} from '../../../../lib/index.js';
import { XEnumTest } from '../enums/x-enum-test.enum.js';
import { ExtraModelDto } from './extra-model.dto.js';
import { LettersEnum } from './pagination-query.dto.js';
import { TagDto } from './tag.dto.js';

@ApiExtraModels(ExtraModelDto)
@ApiExtension('x-tags', ['foo', 'bar'])
Expand Down Expand Up @@ -96,7 +100,11 @@ export class CreateCatDto {
@ApiProperty({ description: 'tag', required: false })
readonly tag: TagDto;

@ApiProperty({ description: 'nullable tag', nullable: true, type: () => TagDto })
@ApiProperty({
description: 'nullable tag',
nullable: true,
type: () => TagDto
})
readonly nullableTag: TagDto;

nested: {
Expand Down
2 changes: 1 addition & 1 deletion e2e/src/cats/dto/extra-model.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiProperty, ApiSchema } from '../../../../lib';
import { ApiProperty, ApiSchema } from '../../../../lib/index.js';

@ApiSchema({
name: 'ExtraModel',
Expand Down
2 changes: 1 addition & 1 deletion e2e/src/cats/dto/pagination-query.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiProperty } from '../../../../lib';
import { ApiProperty } from '../../../../lib/index.js';

export enum LettersEnum {
A = 'A',
Expand Down
2 changes: 1 addition & 1 deletion e2e/src/cats/dto/tag.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiProperty } from '../../../../lib';
import { ApiProperty } from '../../../../lib/index.js';

export class TagDto {
@ApiProperty({ description: 'name' })
Expand Down
2 changes: 1 addition & 1 deletion e2e/src/common/dto/validation-error.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiProperty } from '../../../../lib';
import { ApiProperty } from '../../../../lib/index.js';

export class ValidationErrorDto {
@ApiProperty({
Expand Down
2 changes: 1 addition & 1 deletion e2e/swagger-module-webhooks.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller, Module, Post } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { ApiWebhook, DocumentBuilder, SwaggerModule } from '../lib';
import { ApiWebhook, DocumentBuilder, SwaggerModule } from '../lib/index.js';

describe('SwaggerModule webhooks handling', () => {
@Controller()
Expand Down
60 changes: 39 additions & 21 deletions e2e/validate-schema.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,32 @@ import { NestFactory } from '@nestjs/core';
import { writeFileSync } from 'fs';
import { OpenAPIV3 } from 'openapi-types';
import { join } from 'path';
import SwaggerParser from 'swagger-parser';
import {
DocumentBuilder,
getSchemaPath,
OpenAPIObject,
SwaggerModule
} from '../lib';
import { ParameterObject, SchemaObject } from '../lib/interfaces/open-api-spec.interface';
import { ApplicationModule } from './src/app.module';
import { Cat } from './src/cats/classes/cat.class';
import { TagDto } from './src/cats/dto/tag.dto';
import { ValidationErrorDto } from './src/common/dto/validation-error.dto';
import { ExpressController } from './src/express.controller';
} from '../lib/index.js';
import {
ParameterObject,
ResponseObject,
SchemaObject
} from '../lib/interfaces/open-api-spec.interface.js';
import { ApplicationModule } from './src/app.module.js';
import { Cat } from './src/cats/classes/cat.class.js';
import { TagDto } from './src/cats/dto/tag.dto.js';
import { ValidationErrorDto } from './src/common/dto/validation-error.dto.js';
import { ExpressController } from './src/express.controller.js';
import SwaggerParser = require('@apidevtools/swagger-parser');

function asResponseObject(
response: ResponseObject | { $ref: string } | undefined
) {
if (!response || '$ref' in response) {
throw new Error('Expected an inlined response object');
}
return response;
}

describe('Validate OpenAPI schema', () => {
let app: INestApplication;
Expand Down Expand Up @@ -208,28 +221,33 @@ describe('Validate OpenAPI schema', () => {
it('should preserve example/examples for built-in scalar response types', () => {
const document = SwaggerModule.createDocument(app, options);

const scalarExample =
document.paths['/api/cats/scalar-with-example']['get']['responses']['200'];
expect(scalarExample.content['application/json'].example).toEqual(42);
const scalarExample = asResponseObject(
document.paths['/api/cats/scalar-with-example']['get']['responses']['200']
);
expect(scalarExample.content!['application/json'].example).toEqual(42);
expect((scalarExample as any).example).toBeUndefined();

const scalarExamples =
document.paths['/api/cats/scalar-with-examples']['get']['responses']['200'];
expect(scalarExamples.content['application/json'].examples).toEqual({
const scalarExamples = asResponseObject(
document.paths['/api/cats/scalar-with-examples']['get']['responses'][
'200'
]
);
expect(scalarExamples.content!['application/json'].examples).toEqual({
adult: { value: 5, summary: 'Adult cat age' },
kitten: { value: 1, summary: 'Kitten age' }
});
expect((scalarExamples as any).examples).toBeUndefined();

const arrayExample =
const arrayExample = asResponseObject(
document.paths['/api/cats/array-of-scalar-with-example']['get'][
'responses'
]['200'];
expect(arrayExample.content['application/json'].schema).toEqual({
]['200']
);
expect(arrayExample.content!['application/json'].schema).toEqual({
type: 'array',
items: { type: 'string' }
});
expect(arrayExample.content['application/json'].example).toEqual([
expect(arrayExample.content!['application/json'].example).toEqual([
'Mau',
'Persian'
]);
Expand Down Expand Up @@ -428,9 +446,9 @@ describe('Validate OpenAPI schema', () => {
const createCatOperation = document.paths['/api/cats']?.post;
expect(createCatOperation?.tags).toBeDefined();
expect(Array.isArray(createCatOperation?.tags)).toBe(true);
expect(createCatOperation?.tags?.every((tag) => typeof tag === 'string')).toBe(
true
);
expect(
createCatOperation?.tags?.every((tag) => typeof tag === 'string')
).toBe(true);
});
});
});
1 change: 0 additions & 1 deletion index.ts

This file was deleted.

2 changes: 1 addition & 1 deletion lib/decorators/api-basic.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiSecurity } from './api-security.decorator';
import { ApiSecurity } from './api-security.decorator.js';

/**
* @publicApi
Expand Down
2 changes: 1 addition & 1 deletion lib/decorators/api-bearer.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiSecurity } from './api-security.decorator';
import { ApiSecurity } from './api-security.decorator.js';

/**
* @publicApi
Expand Down
10 changes: 5 additions & 5 deletions lib/decorators/api-body.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Type } from '@nestjs/common';
import { omit } from 'lodash';
import { omit } from 'es-toolkit/compat';
import {
EncodingObject,
ExamplesObject,
ReferenceObject,
RequestBodyObject,
SchemaObject
} from '../interfaces/open-api-spec.interface';
import { SwaggerEnumType } from '../types/swagger-enum.type';
} from '../interfaces/open-api-spec.interface.js';
import { SwaggerEnumType } from '../types/swagger-enum.type.js';
import {
addEnumArraySchema,
addEnumSchema,
isEnumArray,
isEnumDefined
} from '../utils/enum.utils';
import { createParamDecorator, getTypeIsArrayTuple } from './helpers';
} from '../utils/enum.utils.js';
import { createParamDecorator, getTypeIsArrayTuple } from './helpers.js';

type RequestBodyOptions = Omit<RequestBodyObject, 'content'>;

Expand Down
6 changes: 3 additions & 3 deletions lib/decorators/api-callbacks.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DECORATORS } from '../constants';
import { createMixedDecorator } from './helpers';
import { CallBackObject } from '../interfaces/callback-object.interface';
import { DECORATORS } from '../constants.js';
import { createMixedDecorator } from './helpers.js';
import { CallBackObject } from '../interfaces/callback-object.interface.js';

/**
* @publicApi
Expand Down
4 changes: 2 additions & 2 deletions lib/decorators/api-consumes.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DECORATORS } from '../constants';
import { createMixedDecorator } from './helpers';
import { DECORATORS } from '../constants.js';
import { createMixedDecorator } from './helpers.js';

/**
* @publicApi
Expand Down
2 changes: 1 addition & 1 deletion lib/decorators/api-cookie.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ApiSecurity } from './api-security.decorator';
import { ApiSecurity } from './api-security.decorator.js';

/**
* @publicApi
Expand Down
2 changes: 1 addition & 1 deletion lib/decorators/api-default-getter.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Type } from '@nestjs/common';
import { DECORATORS } from '../constants';
import { DECORATORS } from '../constants.js';

/**
* Set the default getter for the given type to the decorated method
Expand Down
4 changes: 2 additions & 2 deletions lib/decorators/api-exclude-controller.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DECORATORS } from '../constants';
import { createClassDecorator } from './helpers';
import { DECORATORS } from '../constants.js';
import { createClassDecorator } from './helpers.js';

/**
* @publicApi
Expand Down
4 changes: 2 additions & 2 deletions lib/decorators/api-exclude-endpoint.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DECORATORS } from '../constants';
import { createMethodDecorator } from './helpers';
import { DECORATORS } from '../constants.js';
import { createMethodDecorator } from './helpers.js';

/**
* @publicApi
Expand Down
8 changes: 4 additions & 4 deletions lib/decorators/api-extension.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { METHOD_METADATA } from '@nestjs/common/constants';
import { DECORATORS } from '../constants';
import { clone, merge } from 'lodash';
import { isConstructor } from '@nestjs/common/utils/shared.utils';
import { METHOD_METADATA } from '@nestjs/common/constants.js';
import { isConstructor } from '@nestjs/common/utils/shared.utils.js';
import { clone } from 'es-toolkit/compat';
import { DECORATORS } from '../constants.js';

function applyExtension(target: any, key: string, value: any): void {
const extensions =
Expand Down
2 changes: 1 addition & 1 deletion lib/decorators/api-extra-models.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DECORATORS } from '../constants';
import { DECORATORS } from '../constants.js';

/**
* @publicApi
Expand Down
12 changes: 6 additions & 6 deletions lib/decorators/api-header.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { isNil, isUndefined, negate, pickBy } from 'lodash';
import { DECORATORS } from '../constants';
import { isNil, isUndefined, negate, pickBy } from 'es-toolkit/compat';
import { DECORATORS } from '../constants.js';
import {
ParameterLocation,
ParameterObject
} from '../interfaces/open-api-spec.interface';
import { SwaggerEnumType } from '../types/swagger-enum.type';
import { getEnumType, getEnumValues } from '../utils/enum.utils';
import { createClassDecorator, createParamDecorator } from './helpers';
} from '../interfaces/open-api-spec.interface.js';
import { SwaggerEnumType } from '../types/swagger-enum.type.js';
import { getEnumType, getEnumValues } from '../utils/enum.utils.js';
import { createClassDecorator, createParamDecorator } from './helpers.js';

export interface ApiHeaderOptions extends Omit<ParameterObject, 'in'> {
enum?: SwaggerEnumType;
Expand Down
Loading