Skip to content

Commit 2effa30

Browse files
committed
fix(repository): make sure model definition is built correctly
See #2002
1 parent 0a42541 commit 2effa30

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright IBM Corp. 2018. All Rights Reserved.
2+
// Node module: @loopback/openapi-v3.
3+
// This file is licensed under the MIT License.
4+
// License text available at https://opensource.org/licenses/MIT
5+
6+
import {getFilterSchemaFor} from '../..';
7+
import {Entity, model, property} from '@loopback/repository';
8+
import {expect} from '@loopback/testlab';
9+
10+
describe('filterSchema', () => {
11+
@model({
12+
name: 'my-user-model',
13+
})
14+
class MyUserModel extends Entity {
15+
@property()
16+
id: string;
17+
18+
@property()
19+
age: number;
20+
}
21+
22+
it('generate filter schema', () => {
23+
const schema = getFilterSchemaFor(MyUserModel);
24+
expect(MyUserModel.definition.name).to.eql('my-user-model');
25+
expect(schema).to.eql({
26+
properties: {
27+
where: {type: 'object'},
28+
fields: {type: 'object'},
29+
offset: {type: 'integer', minimum: 0},
30+
limit: {type: 'integer', minimum: 0},
31+
skip: {type: 'integer', minimum: 0},
32+
order: {type: 'array', items: {type: 'string'}},
33+
},
34+
});
35+
});
36+
});

packages/repository/src/decorators/model.decorator.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,15 @@ export function buildModelDefinition(
6868
target: Function & {definition?: ModelDefinition | undefined},
6969
def?: ModelDefinitionSyntax,
7070
) {
71-
// Check if the definition for this class has been built
72-
if (!def && target.definition && target.definition.name === target.name) {
71+
// Check if the definition for this class has been built (not from the super
72+
// class)
73+
const baseClass = Object.getPrototypeOf(target);
74+
if (
75+
!def &&
76+
target.definition &&
77+
baseClass &&
78+
target.definition !== baseClass.definition
79+
) {
7380
return target.definition;
7481
}
7582
const modelDef = new ModelDefinition(def || {name: target.name});

0 commit comments

Comments
 (0)