ExtendedXmlSerializer / home

A configurable and eXtensible Xml serializer for .NET.
https://extendedxmlserializer.github.io/
MIT License
336 stars 47 forks source link

Composite property is being wrongly identified as circular (I think) #563

Closed flycast closed 2 years ago

flycast commented 3 years ago

I have two properties:

public List<byte[]> ParentKey { get; set; } = new ();
public byte[] Id { get; set; }

I have a third property that is read only that is made of a combination of ParentKey and Id:

        public List<byte[]> Key
        {
            get
            {
                List<byte[]> result = new(ParentKey);
                result.Add(Id);
                return result;
            }
        }

When I serialize I get the following exceptions:

Exception thrown: 'ExtendedXmlSerializer.ExtensionModel.References.CircularReferencesDetectedException' in ExtendedXmlSerializer.dll
Exception thrown: 'System.Reflection.TargetInvocationException' in mscorlib.dll

If I use the attribute [XmlIgnore] the object serializes although without the Key property.

        [XmlIgnore]
        public List<byte[]> Key
        {
            get
            {
                List<byte[]> result = new(ParentKey);
                result.Add(Id);
                return result;
            }
        }

Are these really circular? They are related - 'Key' is made with 'ParentKey' and 'Id' but neither 'ParentKey' or 'Id' is made with Key.

create-issue-branch[bot] commented 3 years ago

Branch issues/other/i563 created!

Mike-E-angelo commented 3 years ago

Ah indeed @flycast since you have a reference/class-based property and a list of said property (or two of them), they can have the same reference occur within the same graph. In order to proceed in this scenario, you can use EnableReferences. Passing test with your described model here:

https://github.com/ExtendedXmlSerializer/home/blob/f848604e741e90d5a598efff89cf493c661ea79e/test/ExtendedXmlSerializer.Tests.ReportedIssues/Issue563Tests.cs#L11-L34

However, since you are creating a new list with each and every call to the Key property I would definitely put that under an ignored call. In addition to the XmlIgnore attribute you can configure it on the container as such:

        var subject = new ConfigurationContainer().EnableReferences()
                                                  .Type<Subject>()
                                                  .Member(x => x.Key)
                                                  .Ignore()
                                                  .Create();

Please do let me know if that helps you out and/or if you have any additional questions and I'll see if I can further assist.

flycast commented 3 years ago

Yes, helps immensely. Thanks.

Mike-E-angelo commented 2 years ago

Going through items here in this repository and I see that this is an open ticket that appears to be answered/solved. If this is not the case, please do let me know by either commenting on this ticket or opening a new one, and I will do my best to assist. Closing for now.