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

Circular dependencies detection problem 2 #593

Closed
UladimirLameyko opened this issue Jan 9, 2023 · 8 comments
Closed

Circular dependencies detection problem 2 #593

UladimirLameyko opened this issue Jan 9, 2023 · 8 comments
Assignees
Labels
bug (cc: fix) Something isn't working

Comments

@UladimirLameyko
Copy link

UladimirLameyko commented Jan 9, 2023

Continuation of #583

I have new case of same problem
Looks like this problem have more shades.
Problem found unexpectedly, previously we use .EnableReferences() but soon as we switched to .AllowMultipleReferences()
new problem is occured.
If I EnableReferences for same object it will serialize successfully, but will never use exs:reference="1"exs:identity="1"
I suppose this object should serialize with .AllowMultipleReferences only
Even Array.Empty will be detected as MultipleReferences

using ExtendedXmlSerializer;
using ExtendedXmlSerializer.Configuration;

var sameInnerObject = new InnerObject { Id = 100, Items = new string[] { } };
var testRootObject = new RootObject
{
    Item1 = sameInnerObject,
    Item2 = new InnerObject2 { Items = sameInnerObject.Items },
    List = new List<InnerObject>()
};

IExtendedXmlSerializer serializer = new ConfigurationContainer()
    .AllowMultipleReferences()
//.EnableReferences() 
    .Create();
var serialized = serializer.Serialize(testRootObject);
Console.WriteLine("Success");

public class RootObject
{
    public InnerObject Item1 { get; set; }
    public InnerObject2 Item2 { get; set; }
    public List<InnerObject> List { get; set; } = new();
}

public class InnerObject
{
    public int Id { get; set; }
    public string[] Items { get; set; }
}

public class InnerObject2
{
    public string[] Items { get; set; }
}
@Mike-E-angelo
Copy link
Member

Wow @UladimirLameyko I am a little shocked here. It appears the reference is not detected at all. Thank you for reporting this I will look into it for you. 👍

@Mike-E-angelo
Copy link
Member

Mike-E-angelo commented Jan 9, 2023

OK @UladimirLameyko I did look into this and we're running into a bit of a limitation here with ExtendedXmlSerializer. Basically, it stems from the classic serializer and empty collections are not emitted. This is what you are running into here with the above code. In this case. if you are using references with collections it is recommended/suggested you initialize with an empty instance, as such:

public class InnerObject
{
    public int Id { get; set; }
    public string[] Items { get; set; } = Array.Empty<string>();
}

public class InnerObject2
{
    public string[] Items { get; set; } = Array.Empty<string>();
}

I concede this is not perfect but poking around any further would introduce breaking changes and/or additional functionality I would not be comfortable introducing at this point with my limitations at the moment.

That stated, I am definitely open to further discussion around this if you have further ideas/considerations/suggestions and even a PR if we can land on one. :)

@UladimirLameyko
Copy link
Author

UladimirLameyko commented Jan 9, 2023

@Mike-E-angelo Problem not with empty arrays. Even if I put value into collection
var sameInnerObject = new InnerObject { Id = 100, Items = new string[] { "Some" } };
Exception still appear
Even if I initialize arrays with
public string[] Items { get; set; } = Array.Empty<string>();

@Mike-E-angelo
Copy link
Member

Mike-E-angelo commented Jan 9, 2023

Oh I see... are you saying this is throwing a CircularReferencesDetectedException instead of a MultipleReferencesDetectedException? I can look into that if so. 👍

@Mike-E-angelo Mike-E-angelo added the bug (cc: fix) Something isn't working label Jan 9, 2023
@Mike-E-angelo Mike-E-angelo self-assigned this Jan 9, 2023
@create-issue-branch
Copy link

Branch issues/fix/i593 created!

@Mike-E-angelo
Copy link
Member

Alright @UladimirLameyko please try out the build as listed here:
#594 (comment)

And let me know if that works for you. If so I will push this to NuGet. 👍

@UladimirLameyko
Copy link
Author

@Mike-E-angelo Look like it helps on my side.
Currently it is not generating Exceptions. Thanks.
Please push it into nuget😎

@Mike-E-angelo
Copy link
Member

Mike-E-angelo commented Jan 9, 2023

Great, sounds good @UladimirLameyko it has been deployed here:
https://www.nuget.org/packages/ExtendedXmlSerializer

I appreciate your continued feedback and cooperation towards improving ExtendedXmlSerializer 👍

An extensible Xml Serializer for .NET that builds on the functionality of the classic XmlSerializer with a powerful and robust extension model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug (cc: fix) Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants