From f8ba5efcff40011fa9552d8189f5674c4d708d21 Mon Sep 17 00:00:00 2001 From: Andre Rabold Date: Wed, 13 Nov 2024 08:36:41 -0800 Subject: [PATCH] fix: @ApiSchema inheritance Ensure the correct @ApiSchema name is used when inheriting from a base class. --- lib/services/schema-object-factory.ts | 2 +- lib/utils/get-schema-path.util.ts | 2 +- test/services/schema-object-factory.spec.ts | 18 ++++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/services/schema-object-factory.ts b/lib/services/schema-object-factory.ts index a44f89e9a..df1381549 100644 --- a/lib/services/schema-object-factory.ts +++ b/lib/services/schema-object-factory.ts @@ -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; } diff --git a/lib/utils/get-schema-path.util.ts b/lib/utils/get-schema-path.util.ts index f248aa64c..d418aacca 100644 --- a/lib/utils/get-schema-path.util.ts +++ b/lib/utils/get-schema-path.util.ts @@ -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[]) { diff --git a/test/services/schema-object-factory.spec.ts b/test/services/schema-object-factory.spec.ts index 767cc14bd..d09714b9a 100644 --- a/test/services/schema-object-factory.spec.ts +++ b/test/services/schema-object-factory.spec.ts @@ -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 = {}; + + schemaObjectFactory.exploreModelSchema(UpdateUserDto, schemas); + + expect(Object.keys(schemas)).toContain('UpdateUser'); + }); + it('should include extension properties', () => { @ApiExtension('x-test', 'value') class CreatUserDto {