Code Monkey home page Code Monkey logo

Comments (5)

tacosontitan avatar tacosontitan commented on June 11, 2024 1

After further investigation, this is certainly not an issue with XML serialization, but rather how the assertion process handles equivalency:

image

As my screenshot shows, ignored members are not set during the deserialization process, and as a result the equivalency will fail. I am currently researching how involved of a change it would be to add support for this. I think the most surgical route would be to update EquivalencyOptions<TExpectation> to exclude ignored members and the BeXmlSerializable method to specify this as part of its options:

deserializedObject.Should().BeEquivalentTo(assertions.Subject, 
    static options => options
        .RespectingRuntimeTypes()
        .IncludingFields()
        .IncludingProperties()
        .ExcludeIgnoredMembers());

My biggest remaining question is if this new method should be specific to XML or not as ExcludeXmlIgnoredMembers for clarity that other serialization methods are not supported.

from fluentassertions.

dennisdoomen avatar dennisdoomen commented on June 11, 2024 1

I think a simpler approach could be to add a custom IMemberSelectionRule that is based on AllPropertiesSelectionRule and AllFieldsSelectionRule and filters out properties and fields marked with the XmlIgnore attribute.

from fluentassertions.

tacosontitan avatar tacosontitan commented on June 11, 2024

I'm not sure this is an issue with the process not ignoring members. Based on the code for the BeXmlSerializable method, it looks like this is working as expected and equivalency is failing:

public static AndConstraint<ObjectAssertions> BeXmlSerializable(this ObjectAssertions assertions, string because = "",
params object[] becauseArgs)
{
try
{
object deserializedObject = CreateCloneUsingXmlSerializer(assertions.Subject);
deserializedObject.Should().BeEquivalentTo(assertions.Subject,
options => options.RespectingRuntimeTypes().IncludingFields().IncludingProperties());
}
catch (Exception exc)
{
Execute.Assertion
.BecauseOf(because, becauseArgs)
.FailWith("Expected {0} to be serializable{reason}, but serialization failed with:"
+ Environment.NewLine + Environment.NewLine + "{1}.",
assertions.Subject,
exc.Message);
}
return new AndConstraint<ObjectAssertions>(assertions);
}

According to line 107 there, the deserialized object should be equivalent to the subject of the assertion. This, combined with my (albeit severely lacking) understanding of how the equivalency test works, should (currently) fail because the ignored members won't be set in the deserialized object.

@dennisdoomen, can this be assigned to me?

from fluentassertions.

tacosontitan avatar tacosontitan commented on June 11, 2024

I've defined a unit test to cover this scenario and opened a draft PR to begin working this in full. I simply need guidance on the following to prove as effective as possible on the issue:

  • Should this be added to the EquivalencyOptions model?
    • If so:
      • What should the name of the method be?
        • I recommend ExcludeXmlIgnoredMembers.
      • Are there any niche references of this that I need to be careful about?
    • If not:
      • How should this be accounted for?
  • Are there any ecosystem-wide scenarios or goals I need to keep in mind?
  • Are there any known gotchas with the BeXmlSerializable method?
  • Is there anything else I should be aware of prior to proceeding?

from fluentassertions.

sa-he avatar sa-he commented on June 11, 2024

Is it possible to choose an implementation that also enables BeEquivalentTo to optionally ignore properties with the XmlIgnoreAttribute?

from fluentassertions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.