Skip to content

Commit

Permalink
fix(repository): allow model classes with recursive type references
Browse files Browse the repository at this point in the history
Fixes #3671 (comment)


Co-authored-by: Miroslav Bajtoš <[email protected]>
  • Loading branch information
raymondfeng and bajtos committed Sep 20, 2019
1 parent 4e9611d commit ed6f243
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,35 @@ describe('DefaultCrudRepository', () => {
expect(User.definition.properties.roles.itemType).to.equal(Role);
expect(User.definition.properties.address.type).to.equal(Address);
});

it('handles recursive model references', () => {
@model()
class ReportState extends Entity {
@property({id: true})
id: string;

@property.array(ReportState, {})
states: ReportState[];

@property({
type: 'string',
})
benchmarkId?: string;

@property({
type: 'string',
})
color?: string;

constructor(data?: Partial<ReportState>) {
super(data);
}
}
const repo = new DefaultCrudRepository(ReportState, ds);
const definition = repo.modelClass.definition;
const typeOfStates = definition.properties.states.type;
expect(typeOfStates).to.eql([repo.modelClass]);
});
});

it('shares the backing PersistedModel across repo instances', () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/repository/src/repositories/legacy-juggler-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ export class DefaultCrudRepository<
return model as typeof juggler.PersistedModel;
}

// To handle circular reference back to the same model,
// we create a placeholder model that will be replaced by real one later
dataSource.getModel(definition.name, true /* forceCreate */);

// We need to convert property definitions from PropertyDefinition
// to plain data object because of a juggler limitation
const properties: {[name: string]: object} = {};
Expand Down

0 comments on commit ed6f243

Please sign in to comment.