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

Migration breaks when using PreserveReferencesHandling #20

Open
Awsmolak opened this issue Nov 29, 2022 · 2 comments
Open

Migration breaks when using PreserveReferencesHandling #20

Awsmolak opened this issue Nov 29, 2022 · 2 comments

Comments

@Awsmolak
Copy link

Awsmolak commented Nov 29, 2022

I can't seem to figure out how to resolve object references in a migration method if PreserveReferencesHandling was turned on during serialization. In that case the migrating object's JObject data may have a reference to the object (ex. "$ref": "123") instead of the actual JSON data. Is there a good solution for passing along all the object references from the top of the object graph?

Update: I'm pretty sure this is due to a reference loop. I noticed that MigrationConverterSpec.ShouldMigrateTypesWithReferenceLoops() unit test is skipped because "Not a requirement by now".

Any chance you might have some hints to get me started with a PR to add that?

@Awsmolak
Copy link
Author

Awsmolak commented Dec 6, 2022

Basically, migrations are not getting called for some serialized objects that happen to be referenced by "$ref" later on. I attempted to fix MigrationConverter.ReadJson to check for ref, and to return the referenced object:

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
    var data = JToken.Load(reader);

    //don't try and repeat migration for objects serialized as refs to previous
    if (data["$ref"] != null)
    {
        return serializer.ReferenceResolver.ResolveReference(serializer, (string)data["$ref"]);
    }

    var migratedData = DataMigrator.TryMigrate(data, objectType, serializer);
...

This fix does work to prevent the migration methods from getting called on encountered "$ref" JTokens. However, the migrations DO NOT get called on the initial deserialization of those objects.

Just to confirm that there isn't anything wonky with my test data or a fundamental misunderstanding of anything, I implemented a JsonConverter<> to perform the same migrations as I was attempting to do with this lib. For the very same dataset, all JTokens for my class are migrated correctly in order. So there is something about your particular implementation that breaks when using references, besides not detecting/resolving them in ReadJson.

Update:
I punted and wrote my own migration code. I'm somewhat confident that this library could be updated to suit my purposes but I just don't have time to work on a PR at the moment. I may revisit, or in case someone else digs into this, the following SO links may prove helpful:
Trying to implement a JSON version migrator by implementing JsonConverter<T>
Generic method of modifying JSON before being returned to client
JSON.Net throws StackOverflowException when using [JsonConvert()]

@Awsmolak Awsmolak changed the title Migration when using PreserveReferencesHandling Migration breaks when using PreserveReferencesHandling Dec 6, 2022
@vangogih
Copy link

vangogih commented Mar 28, 2024

@Awsmolak hey mate!

I rewrote this plugin and made it from 5 to 10 times faster
Link to plugin: https://github.com/vangogih/FastMigrations.Json.Net

And also added the feature with PreserveReferencesHandling support.
Link to test case

It has been a while since your comment but I hope you will find the time to check this test case to be sure that I have wrote it correctly ;)

Looking forward to getting feedback from you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants