Skip to content

Commit aa3a41a

Browse files
Pavel Yakimenkodianaafanador3
authored andcommitted
Check class on property value validation
1 parent e461cd3 commit aa3a41a

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

Realm/RLMObjectBase.mm

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ - (void)dealloc {
9393
}
9494

9595
static id coerceToObjectType(id obj, Class cls, RLMSchema *schema) {
96-
if (([obj isKindOfClass:cls] && ![(id)cls isEmbedded]) ||
97-
([(id)cls isEmbedded] && [obj respondsToSelector:@selector(realm)] && ![obj realm])) {
96+
if ([obj isKindOfClass:cls] && (![(id)cls isEmbedded] || ![obj realm])) {
9897
return obj;
9998
}
10099
obj = RLMBridgeSwiftValue(obj) ?: obj;
@@ -538,6 +537,9 @@ id RLMObjectThaw(RLMObjectBase *obj) {
538537
}
539538

540539
id RLMValidatedValueForProperty(id object, NSString *key, NSString *className) {
540+
if (![[[object objectSchema] className] isEqualToString:className]) {
541+
@throw RLMException(@"Invalid value: cannot initialize '%@' with value '%@'", className, object);
542+
}
541543
@try {
542544
return [object valueForKey:key];
543545
}

Realm/Tests/ObjectCreationTests.mm

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,14 @@ - (void)testInitValidatesNumberTypes {
529529
XCTAssertNoThrow(([[NumberObject alloc] initWithValue:@{@"doubleObj": @DBL_MAX}]));
530530
}
531531

532+
- (void)testInitEmbeddedProperty {
533+
NSArray *failVal = @[@{}, @{}, @{}, @{@"one": [[IntObject alloc] init]}];
534+
XCTAssertThrows([[DictionaryPropertyObject alloc] initWithValue:failVal]);
535+
536+
NSArray *passVal = @[@{}, @{}, @{}, @{@"one": [[EmbeddedIntObject alloc] init]}];
537+
XCTAssertNoThrow([[DictionaryPropertyObject alloc] initWithValue:passVal]);
538+
}
539+
532540
#pragma mark - Create
533541

534542
- (void)testCreateWithArray {

RealmSwift/Tests/ObjectCreationTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,7 @@ class ObjectCreationTests: TestCase {
906906
])
907907
// Do not copy unmanaged object
908908
let copyD = EmbeddedParentObject(value: parentUnmanaged)
909+
XCTAssertTrue(copyD.object === parentUnmanaged.object)
909910
realm.add(copyD)
910911
assertThrows(realm.add(parentUnmanaged), "Cannot set a link to an existing managed embedded object")
911912

@@ -937,6 +938,14 @@ class ObjectCreationTests: TestCase {
937938
realmB.cancelWrite()
938939
}
939940

941+
func testInitEmbeddedProperty() {
942+
let failVal: [Any] = [[], ["one": SwiftIntObject()]]
943+
assertThrows(SwiftDictionaryObject(value: failVal))
944+
945+
let passVal: [Any] = [[], ["one": EmbeddedSwiftIntObject()]]
946+
XCTAssertNoThrow(SwiftDictionaryObject(value: passVal))
947+
}
948+
940949
// test null object
941950
// test null list
942951

RealmSwift/Tests/SwiftTestObjects.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,3 +856,12 @@ class EmbeddedTreeObject3: EmbeddedObject, EmbeddedTreeObject {
856856
let parent3 = LinkingObjects(fromType: EmbeddedTreeObject2.self, property: "child")
857857
let parent4 = LinkingObjects(fromType: EmbeddedTreeObject2.self, property: "children")
858858
}
859+
860+
class EmbeddedSwiftIntObject: EmbeddedObject {
861+
@objc dynamic var intCol = 0
862+
}
863+
864+
class SwiftDictionaryObject: Object {
865+
let intDict = Map<String, SwiftIntObject?>()
866+
let embedIntDict = Map<String, EmbeddedSwiftIntObject?>()
867+
}

0 commit comments

Comments
 (0)