Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional Codable property fix #33

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 24 additions & 0 deletions Tests/ManagedModelTests/CodablePropertiesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extension Fixtures {
var token : String
var expires : Date
var sip : AccessSIP
var optionalSip : AccessSIP?
}

struct AccessSIP: Codable {
Expand Down
Loading