Skip to content

Commit

Permalink
test: include external metadata in e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jun 5, 2023
1 parent 27d9e9a commit bf4a27e
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
19 changes: 18 additions & 1 deletion e2e/api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@
"/api/cats/bulk": {
"get": {
"operationId": "CatsController_findAllBulk",
"summary": "Find all cats in bulk",
"parameters": [
{
"name": "header",
Expand All @@ -607,7 +608,17 @@
],
"responses": {
"200": {
"description": ""
"description": "",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Cat"
}
}
}
}
}
},
"tags": [
Expand Down Expand Up @@ -992,6 +1003,7 @@
"type": "object",
"properties": {
"name": {
"description": "Name of the cat",
"type": "string"
},
"age": {
Expand Down Expand Up @@ -1080,6 +1092,11 @@
"description": "The breed of the Cat"
},
"_tags": {
"description": "Tags of the cat",
"example": [
"tag1",
"tag2"
],
"type": "array",
"items": {
"type": "string"
Expand Down
45 changes: 43 additions & 2 deletions e2e/validate-schema.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { INestApplication } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { writeFileSync } from 'fs';
import { OpenAPIV3 } from 'openapi-types';
import { join } from 'path';
import * as SwaggerParser from 'swagger-parser';
import {
Expand All @@ -12,7 +13,6 @@ import {
import { ApplicationModule } from './src/app.module';
import { Cat } from './src/cats/classes/cat.class';
import { TagDto } from './src/cats/dto/tag.dto';
import { OpenAPIV3 } from 'openapi-types';

describe('Validate OpenAPI schema', () => {
let app: INestApplication;
Expand Down Expand Up @@ -49,7 +49,48 @@ describe('Validate OpenAPI schema', () => {
});

it('should produce a valid OpenAPI 3.0 schema', async () => {
const document = SwaggerModule.createDocument(app, options);
const document = SwaggerModule.createDocument(app, options, {
metadata: {
'@nestjs/swagger': {
models: [
[
require('./src/cats/classes/cat.class'),
{
Cat: {
tags: {
description: 'Tags of the cat',
example: ['tag1', 'tag2']
}
}
}
],
[
require('./src/cats/dto/create-cat.dto'),
{
CreateCatDto: {
name: {
description: 'Name of the cat'
}
}
}
]
],
controllers: [
[
require('./src/cats/cats.controller'),
{
CatsController: {
findAllBulk: {
type: [require('./src/cats/classes/cat.class').Cat],
summary: 'Find all cats in bulk'
}
}
}
]
]
}
}
});

const doc = JSON.stringify(document, null, 2);
writeFileSync(join(__dirname, 'api-spec.json'), doc);
Expand Down
2 changes: 1 addition & 1 deletion lib/explorers/api-operation.explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function applyMetadataFactory(prototype: Type<unknown>) {
if (Object.keys(operationMeta).length === 0) {
return;
}
ApiOperation(metadata[key], { overrideExisting: false })(
ApiOperation(operationMeta, { overrideExisting: false })(
classPrototype,
key,
Object.getOwnPropertyDescriptor(classPrototype, key)
Expand Down

0 comments on commit bf4a27e

Please sign in to comment.