Skip to content

Commit

Permalink
Check class on property value validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Yakimenko authored and dianaafanador3 committed Oct 11, 2022
1 parent e358bc0 commit a61a5bd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions Realm/RLMObjectBase.mm
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ - (void)dealloc {
}

static id coerceToObjectType(id obj, Class cls, RLMSchema *schema) {
if (([obj isKindOfClass:cls] && ![(id)cls isEmbedded]) ||
([(id)cls isEmbedded] && [obj respondsToSelector:@selector(realm)] && ![obj realm])) {
if ([obj isKindOfClass:cls] && (![(id)cls isEmbedded] || ![obj realm])) {
return obj;
}
obj = RLMBridgeSwiftValue(obj) ?: obj;
Expand Down Expand Up @@ -533,6 +532,9 @@ id RLMObjectThaw(RLMObjectBase *obj) {
}

id RLMValidatedValueForProperty(id object, NSString *key, NSString *className) {
if (![[[object objectSchema] className] isEqualToString:className]) {
@throw RLMException(@"Invalid value: cannot initialize '%@' with value '%@'", className, object);
}
@try {
return [object valueForKey:key];
}
Expand Down
8 changes: 8 additions & 0 deletions Realm/Tests/ObjectCreationTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,14 @@ - (void)testInitValidatesNumberTypes {
XCTAssertNoThrow(([[NumberObject alloc] initWithValue:@{@"doubleObj": @DBL_MAX}]));
}

- (void)testInitEmbeddedProperty {
NSArray *failVal = @[@{}, @{}, @{}, @{@"one": [[IntObject alloc] init]}];
XCTAssertThrows([[DictionaryPropertyObject alloc] initWithValue:failVal]);

NSArray *passVal = @[@{}, @{}, @{}, @{@"one": [[EmbeddedIntObject alloc] init]}];
XCTAssertNoThrow([[DictionaryPropertyObject alloc] initWithValue:passVal]);
}

#pragma mark - Create

- (void)testCreateWithArray {
Expand Down
9 changes: 9 additions & 0 deletions RealmSwift/Tests/ObjectCreationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ class ObjectCreationTests: TestCase {
])
// Do not copy unmanaged object
let copyD = EmbeddedParentObject(value: parentUnmanaged)
XCTAssertTrue(copyD.object === parentUnmanaged.object)
realm.add(copyD)
assertThrows(realm.add(parentUnmanaged), "Cannot set a link to an existing managed embedded object")

Expand Down Expand Up @@ -937,6 +938,14 @@ class ObjectCreationTests: TestCase {
realmB.cancelWrite()
}

func testInitEmbeddedProperty() {
let failVal: [Any] = [[], ["one": SwiftIntObject()]]
assertThrows(SwiftDictionaryObject(value: failVal))

let passVal: [Any] = [[], ["one": EmbeddedSwiftIntObject()]]
XCTAssertNoThrow(SwiftDictionaryObject(value: passVal))
}

// test null object
// test null list

Expand Down
9 changes: 9 additions & 0 deletions RealmSwift/Tests/SwiftTestObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -852,3 +852,12 @@ class EmbeddedTreeObject3: EmbeddedObject, EmbeddedTreeObject {
let parent3 = LinkingObjects(fromType: EmbeddedTreeObject2.self, property: "child")
let parent4 = LinkingObjects(fromType: EmbeddedTreeObject2.self, property: "children")
}

class EmbeddedSwiftIntObject: EmbeddedObject {
@objc dynamic var intCol = 0
}

class SwiftDictionaryObject: Object {
let intDict = Map<String, SwiftIntObject?>()
let embedIntDict = Map<String, EmbeddedSwiftIntObject?>()
}

0 comments on commit a61a5bd

Please sign in to comment.