ExtendedXmlSerializer / home

A configurable and eXtensible Xml serializer for .NET.

Home Page:https://extendedxmlserializer.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Any way to allow Array.Empty<xxx> to pass the reference check?

jhranac opened this issue · comments

using Array.Empty as a default value for collections uses in the background the same array instance.
This fails the references check and the serializer throws an exception that there are two(or more) identical objects you are trying to serialize.

Is there a workaround for that without having to avoid using Array.Empty or allowing references ?

configuration.AllowedReferenceTypes().Add(typeof(Array).GetTypeInfo());

works, but that allows references for all arrays, not just the empty one.
Also it produces unwanted XML (containing reference, not an empty array)

HI @jhranac thank you for writing in with this. At first glance it appears we need to add an AllowedReferences method that works like AllowedReferenceTypes but with actual references.

As this is an enhancement rather than a bug fix I would be more apt to suggest a PR. I may suggest a PR in any case going forward as my time for this project is considerably reduced these days. Additionally, since it's been over a year since the last version deployment there will be some brushing off some dust to get any updates out there. 😬

Are you able to create an extension on your side that mimics AllowedReferenceTypes but with instance references? That would be the most ideal. If not, a PR would be the next suggestion. And lastly, if those options cannot work I can try to carve some time here to see if I can address it and get a deployment out there.

I'll see what we have time for. No promises there.

For the meantime, is there a way to disable the references check and force the serializer to output the same object multiple times?

Alright @jhranac I took a look into this a bit and the following works for me/passes:

public sealed class Issue581Tests
	{
		[Fact]
		public void Verify()
		{
			var container = new ConfigurationContainer();
			//container.IgnoredReferenceTypes().Add(typeof(Array).GetTypeInfo());
			var instance = new Subject() {  };
			container.ForTesting().Cycle(instance).Should().BeEquivalentTo(instance);

		}

		sealed class Subject
		{
			public int[] First { get; set; } = Array.Empty<int>();

			public int[] Second { get; set; } = Array.Empty<int>();
		}
	}

Do you have code on what is failing so that I can further compare?

For the meantime, is there a way to disable the references check and force the serializer to output the same object multiple times?

I have the following up above but am unsure if it works as it passes without issue ATM for me:

// container.IgnoredReferenceTypes().Add(typeof(Array).GetTypeInfo());

Going through older questions and it appears this one has been answered. Please do let me know if you have any further comments/questions and I will further assist you. 👍