Skip to content

Commit

Permalink
fix(repository): make the navigational property err msg more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin authored and agnes512 committed Jan 13, 2020
1 parent 39d47d1 commit 2d493bc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ export function hasManyRelationAcceptance(
],
}),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Customer" property "orders")',
'Navigational properties are not allowed in model data (model "Customer" property "orders"), please remove it.',
);
});

Expand All @@ -238,7 +238,7 @@ export function hasManyRelationAcceptance(
},
]),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Customer" property "orders")',
'Navigational properties are not allowed in model data (model "Customer" property "orders"), please remove it.',
);
});

Expand Down Expand Up @@ -296,7 +296,7 @@ export function hasManyRelationAcceptance(
});

await expect(customerRepo.delete(found)).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Customer" property "orders")',
'Navigational properties are not allowed in model data (model "Customer" property "orders"), please remove it.',
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -351,9 +351,20 @@ describe('DefaultCrudRepository', () => {
folderId: number;
}

@model()
class Account extends Entity {
@property({id: true})
id?: number;
@property()
name: string;
@belongsTo(() => Author, {name: 'author'})
author: number;
}

let folderRepo: DefaultCrudRepository<Folder, unknown, {}>;
let fileRepo: DefaultCrudRepository<File, unknown, {}>;
let authorRepo: DefaultCrudRepository<Author, unknown, {}>;
let accountRepo: DefaultCrudRepository<Account, unknown, {}>;

let folderFiles: HasManyRepositoryFactory<File, typeof Folder.prototype.id>;
let fileFolder: BelongsToAccessor<Folder, typeof File.prototype.id>;
Expand All @@ -370,6 +381,7 @@ describe('DefaultCrudRepository', () => {
folderRepo = new DefaultCrudRepository(Folder, ds);
fileRepo = new DefaultCrudRepository(File, ds);
authorRepo = new DefaultCrudRepository(Author, ds);
accountRepo = new DefaultCrudRepository(Account, ds);
});

before(() => {
Expand Down Expand Up @@ -461,18 +473,37 @@ describe('DefaultCrudRepository', () => {
});
});

it('throws if the target data passes to CRUD methods contains nav properties', async () => {
// a unit test for entityToData, which is invoked by create() method
// it would be the same of other CRUD methods.
await expect(
folderRepo.create({
name: 'f1',
files: [{title: 'nav property'}],
}),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Folder" property "files")',
);
});
context(
'throws if the target data passes to CRUD methods contains nav properties',
() => {
it('error message warns about the nav properties', async () => {
// a unit test for entityToData, which is invoked by create() method
// it would be the same of other CRUD methods.
await expect(
folderRepo.create({
name: 'f1',
files: [{title: 'nav property'}],
}),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Folder" property "files"), please remove it.',
);
});

it('error msg also warns about property and relation names in belongsTo relation', async () => {
// the belongsTo relation has the same property name as the relation name.
await expect(
accountRepo.create({
name: 'acoount 1',
author: 1, // same as the relation name
}),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Account" property "author"), please remove it.' +
' The error might be invoked by belongsTo relations, please make sure the relation name is not the same as' +
' the property name.',
);
});
},
);

// stub resolvers
const hasManyResolver: InclusionResolver<
Expand Down
14 changes: 10 additions & 4 deletions packages/repository/src/repositories/legacy-juggler-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,17 +573,23 @@ export class DefaultCrudRepository<
typeof entity.toJSON === 'function' ? entity.toJSON() : {...entity};
*/

// proposal solution from agnes: delete the id property in the
// target data when runs replaceById

const data: AnyObject = new this.entityClass(entity);

const def = this.entityClass.definition;
const props = def.properties;
for (const r in def.relations) {
const relName = def.relations[r].name;
if (relName in data) {
let invalidNameMsg = '';
if (relName in props) {
invalidNameMsg =
` The error might be invoked by belongsTo relations, please make sure the relation name is not the same as` +
` the property name.`;
}
throw new Error(
`Navigational properties are not allowed in model data (model "${this.entityClass.modelName}" property "${relName}")`,
`Navigational properties are not allowed in model data (model "${this.entityClass.modelName}"` +
` property "${relName}"), please remove it.` +
invalidNameMsg,
);
}
}
Expand Down

0 comments on commit 2d493bc

Please sign in to comment.