Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SchemaFactory trigger an error when a schema doesn't have the @Schema decorator #1703

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/factories/definitions.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ export class DefinitionsFactory {
break;
}
const schemaMetadata = TypeMetadataStorage.getSchemaMetadataByTarget(
parent as Type<unknown>,
target as Type<unknown>,
);
if (!schemaMetadata) {
parent = Object.getPrototypeOf(parent);
continue;
throw new Error(
`Target class "${target}" passed in to the "DefinitionsFactory#createForClass()" method isn't a @Schema.`,
kamilmysliwiec marked this conversation as resolved.
Show resolved Hide resolved
);
}
schemaMetadata.properties?.forEach((item) => {
const options = this.inspectTypeDefinition(item.options as any);
Expand Down
12 changes: 12 additions & 0 deletions tests/e2e/mongoose.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ describe('Mongoose', () => {
});
});

it(`should return updated document`, (done) => {
const updateDto = { name: 'Nest', breed: 'Maine coon', age: Date.now() };
request(server)
.put('/cats')
.send(updateDto)
.expect(200)
.end((err, { body }) => {
expect(body.modifiedCount).toBeGreaterThan(0);
done();
});
});

afterEach(async () => {
await app.close();
});
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/schema.factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ class ExampleClass {
array: Array<any>;
}

class BrokenClass {
@Prop({ required: true })
children: ChildClass;

@Prop([ChildClass])
nodes: ChildClass[];

@Prop()
array: Array<any>;
}

describe('SchemaFactory', () => {
it('should populate the schema options', () => {
const schema = SchemaFactory.createForClass(ExampleClass) as any;
Expand All @@ -50,4 +61,7 @@ describe('SchemaFactory', () => {
}),
);
});
it('should break the schema', () => {
expect(() => SchemaFactory.createForClass(BrokenClass)).toThrow(Error);
});
});
9 changes: 7 additions & 2 deletions tests/src/cats/cats.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Body, Controller, Get, Post } from '@nestjs/common';
import { Body, Controller, Get, Post, Put } from '@nestjs/common';
import { CatsService } from './cats.service';
import { CreateCatDto } from './dto/create-cat.dto';
import { CreateCatDto, UpdateCatDto } from './dto';
import { Cat } from './schemas/cat.schema';

@Controller('cats')
Expand All @@ -12,6 +12,11 @@ export class CatsController {
return this.catsService.create(createCatDto);
}

@Put()
async update(@Body() updateCatDto: UpdateCatDto) {
return this.catsService.update(updateCatDto);
}

@Get()
async findAll(): Promise<Cat[]> {
return this.catsService.findAll();
Expand Down
10 changes: 8 additions & 2 deletions tests/src/cats/cats.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { Model } from 'mongoose';
import { Model, UpdateWriteOpResult } from 'mongoose';
import { InjectModel } from '../../../lib';
import { CreateCatDto } from './dto/create-cat.dto';
import { CreateCatDto, UpdateCatDto } from './dto';
import { Cat } from './schemas/cat.schema';

@Injectable()
Expand All @@ -13,6 +13,12 @@ export class CatsService {
return createdCat.save();
}

async update(updateCat: UpdateCatDto): Promise<UpdateWriteOpResult> {
const filter = { name: updateCat.name };
const updatedCat = this.catModel.updateOne(filter, updateCat);
return updatedCat;
}

async findAll(): Promise<Cat[]> {
return this.catModel.find().exec();
}
Expand Down
2 changes: 2 additions & 0 deletions tests/src/cats/dto/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './create-cat.dto';
export * from './update-cat.dto';
5 changes: 5 additions & 0 deletions tests/src/cats/dto/update-cat.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class UpdateCatDto {
readonly name: string;
readonly age: number;
readonly breed: string;
}