From cd8b6e170fc26122c19861052b7e4e7b3d4685d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Kope=C4=87?= Date: Thu, 9 May 2024 17:49:45 +0200 Subject: [PATCH 1/2] Fix for issue #32 --- .../NSAttributeDescription+Data.swift | 4 ++-- .../CodablePropertiesTests.swift | 24 +++++++++++++++++++ .../Schemas/CodablePropertySchema.swift | 1 + 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Sources/ManagedModels/SchemaCompatibility/NSAttributeDescription+Data.swift b/Sources/ManagedModels/SchemaCompatibility/NSAttributeDescription+Data.swift index 6e7f240..b32f945 100644 --- a/Sources/ManagedModels/SchemaCompatibility/NSAttributeDescription+Data.swift +++ b/Sources/ManagedModels/SchemaCompatibility/NSAttributeDescription+Data.swift @@ -32,8 +32,8 @@ extension CoreData.NSAttributeDescription: SchemaProperty { if let baseType = attributeType.swiftBaseType(isOptional: isOptional) { return baseType } - guard let attributeValueClassName else { return Any.self } - return NSClassFromString(attributeValueClassName) ?? Any.self + guard let attributeValueClassName else { return isOptional ? Any?.self : Any.self } + return NSClassFromString(attributeValueClassName) ?? (isOptional ? Any?.self : Any.self) } set { // Note: This needs to match up w/ PersistentModel+KVC. diff --git a/Tests/ManagedModelTests/CodablePropertiesTests.swift b/Tests/ManagedModelTests/CodablePropertiesTests.swift index a122880..add217b 100644 --- a/Tests/ManagedModelTests/CodablePropertiesTests.swift +++ b/Tests/ManagedModelTests/CodablePropertiesTests.swift @@ -69,4 +69,28 @@ final class CodablePropertiesTests: XCTestCase { XCTAssertNotNil(attribute.valueTransformerName) XCTAssertEqual(attribute.valueTransformerName, transformerName.rawValue) } + + func testOptionalCodablePropertyEntity() throws { + let entity = try XCTUnwrap( + container?.managedObjectModel.entitiesByName["StoredAccess"] + ) + + // Creating the entity should have registered the transformer for the + // CodableBox. + let transformerName = try XCTUnwrap( + ValueTransformer.valueTransformerNames().first(where: { + $0.rawValue.range(of: "CodableTransformerGSqVOO17ManagedModelTests8") + != nil + }) + ) + let transformer = try XCTUnwrap(ValueTransformer(forName: transformerName)) + _ = transformer // to clear unused-wraning + + let attribute = try XCTUnwrap(entity.attributesByName["optionalSip"]) + XCTAssertEqual(attribute.name, "optionalSip") + XCTAssertTrue(attribute.valueType == Any?.self) + // Fixtures.CodablePropertiesSchema.AccessSIP?.self) + XCTAssertNotNil(attribute.valueTransformerName) + XCTAssertEqual(attribute.valueTransformerName, transformerName.rawValue) + } } diff --git a/Tests/ManagedModelTests/Schemas/CodablePropertySchema.swift b/Tests/ManagedModelTests/Schemas/CodablePropertySchema.swift index 2482789..813e27c 100644 --- a/Tests/ManagedModelTests/Schemas/CodablePropertySchema.swift +++ b/Tests/ManagedModelTests/Schemas/CodablePropertySchema.swift @@ -20,6 +20,7 @@ extension Fixtures { var token : String var expires : Date var sip : AccessSIP + var optionalSip : AccessSIP? } struct AccessSIP: Codable { From 8f65cbb6044dca2de005dc3ff2dea284cb50c31a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Kope=C4=87?= Date: Thu, 9 May 2024 17:54:31 +0200 Subject: [PATCH 2/2] Whitespaces aligned --- .../SchemaCompatibility/NSAttributeDescription+Data.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/ManagedModels/SchemaCompatibility/NSAttributeDescription+Data.swift b/Sources/ManagedModels/SchemaCompatibility/NSAttributeDescription+Data.swift index b32f945..b579b7c 100644 --- a/Sources/ManagedModels/SchemaCompatibility/NSAttributeDescription+Data.swift +++ b/Sources/ManagedModels/SchemaCompatibility/NSAttributeDescription+Data.swift @@ -32,8 +32,8 @@ extension CoreData.NSAttributeDescription: SchemaProperty { if let baseType = attributeType.swiftBaseType(isOptional: isOptional) { return baseType } - guard let attributeValueClassName else { return isOptional ? Any?.self : Any.self } - return NSClassFromString(attributeValueClassName) ?? (isOptional ? Any?.self : Any.self) + guard let attributeValueClassName else { return isOptional ? Any?.self : Any.self } + return NSClassFromString(attributeValueClassName) ?? (isOptional ? Any?.self : Any.self) } set { // Note: This needs to match up w/ PersistentModel+KVC.