Skip to content

Commit

Permalink
fix: @ApiSchema inheritance
Browse files Browse the repository at this point in the history
Ensure the correct @ApiSchema name is used when inheriting from a base class.
  • Loading branch information
arabold committed Nov 13, 2024
1 parent 7acedba commit f8ba5ef
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
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'
})
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

0 comments on commit f8ba5ef

Please sign in to comment.