Skip to content

Conversion Realm objects to dictionary: property mapping in nested arrays #314

@CallMeSnake

Description

@CallMeSnake

There is a problem when you convert Realm objects using toDictionary(). When given realm object has nested array it uses toCodableValue() method from RealmListEVCustomReflectable.swift. There're conversion of every item in nested array to dictionary in this method:

    public func toCodableValue() -> Any {
        var q:[Any] = []
        for case let e as Any in self {
            q.append((e as? EVReflectable)?.toDictionary([.PropertyConverter, .KeyCleanup, .PropertyMapping, .DefaultSerialize]) ?? e)
        }
        return q
 
        // Why do we need all this code? Should be the same as this. But this crashes.
        //return self.enumerated().map { ($0.element as? EVReflectable)?.toDictionary() ?? NSDictionary() }
    }

The problem here is in conversion options: [.PropertyConverter, .KeyCleanup, .PropertyMapping, .DefaultSerialize]. This converts properties of items in nested array when you use .KeyCleanup

I've got object like this in my case :

    {
        id: 1,
        relativeId: 4,
        ...
        programRoles: [
            {
                id: 1,
                personId: 1,
                programId: 42,
                programType: 2
            }
        ]
    }

And after conversion I've got this dictionary with unexpected properties in array items:

    [
        {
            key: id,
            value: 1
        },
        {
            key: relativeId,
            value: 4
        },
        ...
        {
            key: programRoles,
            value: [
                [
                    {
                        key: id,
                        value: 1
                    },
                    {
                        key: person_id,
                        value: 1
                    },
                    {
                        key: program_id,
                        value: 42
                    },
                    {
                        key: program_type,
                        value: 2
                    }
                ]
            ]
        }
    ]

Can you look up this issue somehow? E.g. use conversionOptions passed from above in method toCodableValue() or just remove this hard-coded options?
Thnx for attention.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions