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

fix: @ApiSchema inheritance #3164

Merged
merged 1 commit into from
Nov 15, 2024
Merged
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
2 changes: 1 addition & 1 deletion lib/services/schema-object-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export class SchemaObjectFactory {
return type.name;
}

const schemaName = customSchema[0].name;
const schemaName = customSchema[customSchema.length - 1].name;
return schemaName ?? type.name;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/utils/get-schema-path.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function getSchemaNameByClass(target: Function): string {
return target.name;
}

return customSchema[0].name ?? target.name;
return customSchema[customSchema.length - 1].name ?? target.name;
}

export function refs(...models: Function[]) {
Expand Down
18 changes: 18 additions & 0 deletions test/services/schema-object-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,24 @@ describe('SchemaObjectFactory', () => {
expect(Object.keys(schemas)).toContain('UpdateUserDto');
});

it('should override the schema name of base class', () => {
@ApiSchema({
name: 'CreateUser'
})
class CreateUserDto {}

@ApiSchema({
name: 'UpdateUser'
})
Comment on lines +421 to +423
Copy link
Contributor Author

@arabold arabold Nov 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting enough, the case without an @ApiSchema decorator in the child class had been tested. Just not the case when both the parent and the child class have a decorator set.

class UpdateUserDto extends CreateUserDto {}

const schemas: Record<string, SchemasObject> = {};

schemaObjectFactory.exploreModelSchema(UpdateUserDto, schemas);

expect(Object.keys(schemas)).toContain('UpdateUser');
});

it('should include extension properties', () => {
@ApiExtension('x-test', 'value')
class CreatUserDto {
Expand Down