Skip to content

Commit

Permalink
fix(repository): remove property.array() call from hasMany decorator
Browse files Browse the repository at this point in the history
Fix for issue #1944 - Create related model in hasMany relation inconsistency
  • Loading branch information
SharonItz authored and b-admike committed Jan 15, 2019
1 parent e004d58 commit 56ab017
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 32 deletions.
Expand Up @@ -42,6 +42,7 @@ describe('build-schema', () => {
class NoPropertyMeta {
prop: string;
}

@model()
class OnePropertyDecorated {
@property()
Expand All @@ -64,6 +65,7 @@ describe('build-schema', () => {

it('does not convert models that have not been decorated with @model()', () => {
class Empty {}

class NoModelMeta {
@property()
foo: string;
Expand Down Expand Up @@ -106,6 +108,7 @@ describe('build-schema', () => {
const topMeta = {
description: 'Test description',
};

@model(topMeta)
class TestModel {
@property()
Expand Down Expand Up @@ -457,12 +460,14 @@ describe('build-schema', () => {
});
expect(customerSchema.properties).to.deepEqual({
id: {type: 'number'},
});
expect(customerSchema.properties).to.not.containDeep({
orders: {
type: 'array',
items: {$ref: '#/definitions/Order'},
},
});
expect(customerSchema.definitions).to.deepEqual({
expect(customerSchema.definitions).to.not.containEql({
Order: {
title: 'Order',
properties: {
Expand Down Expand Up @@ -548,6 +553,7 @@ describe('build-schema', () => {
@property()
foo: number;
}

const cachedSchema: JsonSchema = {
properties: {
cachedProperty: {
Expand All @@ -560,11 +566,9 @@ describe('build-schema', () => {
cachedSchema,
TestModel,
);

const jsonSchema = getJsonSchema(TestModel);
expect(jsonSchema).to.eql(cachedSchema);
});

it('creates JSON schema if one does not already exist', () => {
@model()
class NewModel {
Expand Down
Expand Up @@ -3,7 +3,6 @@
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {property} from '../../decorators/model.decorator';
import {Entity, EntityResolver} from '../../model';
import {relation} from '../relation.decorator';
import {HasManyDefinition, RelationType} from '../relation.types';
Expand All @@ -21,8 +20,6 @@ export function hasMany<T extends Entity>(
definition?: Partial<HasManyDefinition>,
) {
return function(decoratedTarget: Object, key: string) {
property.array(targetResolver)(decoratedTarget, key);

const meta: HasManyDefinition = Object.assign(
// default values, can be customized by the caller
{name: key},
Expand Down
Expand Up @@ -139,6 +139,19 @@ describe('HasMany relation', () => {
expect(_.pick(orders[0], ['customerId', 'description'])).to.eql(newOrder);
});

it('does not create an array of the related model', async () => {
await expect(
customerRepo.create({
name: 'a customer',
orders: [
{
description: 'order 1',
},
],
}),
).to.be.rejectedWith(/`orders` is not defined/);
});

// This should be enforced by the database to avoid race conditions
it.skip('reject create request when the customer does not exist');

Expand Down Expand Up @@ -170,6 +183,7 @@ describe('HasMany relation', () => {

function givenApplicationWithMemoryDB() {
class TestApp extends RepositoryMixin(Application) {}

app = new TestApp();
app.dataSource(new juggler.DataSource({name: 'db', connector: 'memory'}));
}
Expand Down
28 changes: 2 additions & 26 deletions packages/repository/test/unit/decorator/relation.decorator.unit.ts
Expand Up @@ -53,12 +53,10 @@ describe('relation decorator', () => {
source: AddressBook,
target: () => Address,
});

expect(jugglerMeta).to.eql({
expect(jugglerMeta).to.not.containEql({
type: Array,
itemType: () => Address,
});

expect(AddressBook.definition.relations).to.eql({
addresses: {
type: RelationType.hasMany,
Expand Down Expand Up @@ -100,33 +98,11 @@ describe('relation decorator', () => {
target: () => Address,
keyTo: 'someForeignKey',
});
expect(jugglerMeta).to.eql({
expect(jugglerMeta).to.not.containEql({
type: Array,
itemType: () => Address,
});
});

context('when interacting with @property.array', () => {
it('does not get its property metadata overwritten by @property.array', () => {
expect(() => {
class Address extends Entity {
addressId: number;
street: string;
province: string;
}

// tslint:disable-next-line:no-unused
class AddressBook extends Entity {
id: number;
@property.array(Entity)
@hasMany(() => Address, {
keyTo: 'someForeignKey',
})
addresses: Address[];
}
}).to.throw(/Decorator cannot be applied more than once/);
});
});
});

describe('belongsTo', () => {
Expand Down

0 comments on commit 56ab017

Please sign in to comment.