Skip to content

Commit bde3409

Browse files
Generator: find backlink source also if target is processed first #125
1 parent 678be41 commit bde3409

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

generator/lib/src/code_builder.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,10 @@ class CodeBuilder extends Builder {
369369
}
370370
rel.targetId = targetEntity.id;
371371
}
372-
372+
}
373+
// Note: finding the backlink source requires that all ToMany relations have
374+
// a targetId set, so find backlink sources in a separate loop.
375+
for (var entity in model.entities) {
373376
for (var backlink in entity.backlinks) {
374377
backlink.source = _findBacklinkSource(model, entity, backlink);
375378
}

generator/test/code_builder_test.dart

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,59 @@ void main() {
271271
''');
272272
});
273273

274+
test('Finds backlink source if type is unique', () async {
275+
final source = r'''
276+
library example;
277+
import 'package:objectbox/objectbox.dart';
278+
279+
@Entity()
280+
class Example {
281+
@Id()
282+
int id = 0;
283+
284+
final relA = ToMany<A>();
285+
final relB = ToOne<B>();
286+
}
287+
288+
// Name related classes to be lexically before Example so they are
289+
// processed first.
290+
291+
@Entity()
292+
class A {
293+
@Id()
294+
int id = 0;
295+
296+
@Backlink()
297+
final backRel = ToMany<Example>();
298+
}
299+
300+
@Entity()
301+
class B {
302+
@Id()
303+
int id = 0;
304+
305+
@Backlink()
306+
final backRel = ToMany<Example>();
307+
}
308+
''';
309+
310+
final testEnv = GeneratorTestEnv();
311+
await testEnv.run(source);
312+
313+
final entityA = testEnv.model.entities.firstWhere((e) => e.name == 'A');
314+
var backlinkSourceA = entityA.backlinks.first.source;
315+
expect(backlinkSourceA, isA<BacklinkSourceRelation>());
316+
expect((backlinkSourceA as BacklinkSourceRelation).srcRel.name, 'relA');
317+
318+
final entityB = testEnv.model.entities.firstWhere((e) => e.name == 'B');
319+
var backlinkSourceB = entityB.backlinks.first.source;
320+
expect(backlinkSourceB, isA<BacklinkSourceProperty>());
321+
expect(
322+
(backlinkSourceB as BacklinkSourceProperty).srcProp.relationField,
323+
'relB',
324+
);
325+
});
326+
274327
test('@TargetIdProperty ToOne annotation', () async {
275328
final source = r'''
276329
library example;

objectbox/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
MongoDB to match the name used in the MongoDB database. [#713](https://github.com/objectbox/objectbox-dart/issues/713)
1616
* Provide a helpful error message if the name of a property conflicts with a target ID property
1717
created for a `ToOne` relation. [#713](https://github.com/objectbox/objectbox-dart/issues/713)
18+
* Generator: find `@Backlink()` source relation also in case target entity class is processed first.
19+
[#687](https://github.com/objectbox/objectbox-dart/issues/687)
1820

1921
### Sync
2022

0 commit comments

Comments
 (0)