Skip to content

Conversation

@dennisdoomen
Copy link
Member

@dennisdoomen dennisdoomen commented Sep 14, 2025

This PR adds comprehensive support for JSON assertion APIs in FluentAssertions along with BeEquivalentTo functionality for JSON nodes.

The PR introduces new assertion methods for System.Text.Json.Nodes.JsonNode and JsonArray types, including type checking methods like BeNumeric(), BeString(), BeBool(), BeLocalDate(), and BeUtcDate(). It also adds property verification methods such as HaveProperty() and array checking with BeAnArray().

A key feature is the BeEquivalentTo() method that allows comparing JSON nodes directly to C# objects, with automatic conversion of JSON values to appropriate .NET types (including ISO 8601 date parsing). This comparison supports case-insensitive property matching through the IgnoringJsonPropertyCasing() option.

The implementation includes new matching rules specifically for JSON properties that work alongside existing member matching, and a JSON conversion step that automatically handles type conversions during equivalency comparisons. The system seamlessly integrates with the existing equivalency framework, allowing JSON nodes to be compared against complex object graphs with nested properties and collections.

Closes #2205

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds comprehensive JSON assertion capabilities to FluentAssertions by introducing specialized assertion methods for System.Text.Json.Nodes.JsonNode and enabling equivalency comparisons between JSON nodes and C# objects.

Key changes include:

  • New JSON-specific assertion methods for type checking (BeNumeric(), BeString(), BeBool(), BeLocalDate(), BeUtcDate())
  • Property verification methods (HaveProperty(), NotHaveProperty()) and array checking (BeAnArray(), NotBeAnArray())
  • BeEquivalentTo() functionality that allows direct comparison of JSON nodes to C# objects with automatic type conversion
  • Case-insensitive property matching option through IgnoringJsonPropertyCasing()

Reviewed Changes

Copilot reviewed 37 out of 37 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
Tests/FluentAssertions.Specs/Specialized/JsonNodeSpecs.cs Comprehensive test suite covering all new JSON assertion methods
Tests/FluentAssertions.Equivalency.Specs/JsonNodeSpecs.cs Test cases for JSON equivalency comparisons with various scenarios
Tests/FluentAssertions.Extensibility.Specs/JsonWithInlineAssertionsSpecs.cs Tests demonstrating JSON assertions with inline predicates and assertions
Tests/FluentAssertions.Extensibility.Specs/HttpResponseMessageAssertions.cs Extension methods for HTTP response JSON assertion support
Src/FluentAssertions/Specialized/JsonNodeAssertions.cs Main assertion class providing JSON-specific assertion methods
Src/FluentAssertions/Formatting/JsonNodeFormatter.cs Custom formatter for rendering JSON nodes in assertion messages
Src/FluentAssertions/Equivalency/Steps/JsonConversionStep.cs Equivalency step for converting JSON values to appropriate .NET types
Src/FluentAssertions/Equivalency/JsonProperty.cs JSON property representation for equivalency matching
Src/FluentAssertions/Equivalency/Matching/*.cs JSON-aware matching rules for property comparison

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@dennisdoomen dennisdoomen force-pushed the expose-inline-assertion-type branch from 3ca68d4 to a534045 Compare September 14, 2025 13:39
@github-actions
Copy link

github-actions bot commented Sep 14, 2025

Qodana for .NET

It seems all right 👌

No new problems were found according to the checks applied

💡 Qodana analysis was run in the pull request mode: only the changed files were checked
☁️ View the detailed Qodana report

Contact Qodana team

Contact us at qodana-support@jetbrains.com

@dennisdoomen dennisdoomen force-pushed the expose-inline-assertion-type branch 8 times, most recently from 90ce2b6 to 5995d90 Compare September 14, 2025 14:53
@dennisdoomen dennisdoomen marked this pull request as ready for review September 14, 2025 14:53
@dennisdoomen dennisdoomen requested a review from jnyrup September 14, 2025 14:53
@dennisdoomen dennisdoomen force-pushed the expose-inline-assertion-type branch from 5995d90 to 6ed1e49 Compare September 16, 2025 18:14
@dennisdoomen dennisdoomen requested a review from jnyrup September 16, 2025 18:16
@dennisdoomen dennisdoomen force-pushed the expose-inline-assertion-type branch 2 times, most recently from 6a0cc0c to 3f5a206 Compare September 16, 2025 18:45
@coveralls
Copy link

coveralls commented Sep 16, 2025

Pull Request Test Coverage Report for Build 17881824721

Details

  • 220 of 236 (93.22%) changed or added relevant lines in 17 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.1%) to 97.157%

Changes Missing Coverage Covered Lines Changed/Added Lines %
Src/FluentAssertions/Specialized/JsonNodeAssertions.cs 114 115 99.13%
Src/FluentAssertions/Equivalency/JsonProperty.cs 16 31 51.61%
Totals Coverage Status
Change from base Build 17775691023: -0.1%
Covered Lines: 12757
Relevant Lines: 12975

💛 - Coveralls

@dennisdoomen dennisdoomen force-pushed the expose-inline-assertion-type branch from 3f5a206 to 3b050a8 Compare September 16, 2025 18:54
@dennisdoomen dennisdoomen requested a review from jnyrup September 16, 2025 18:55
@dennisdoomen dennisdoomen force-pushed the expose-inline-assertion-type branch from 3b050a8 to e8d5da8 Compare September 18, 2025 16:26
@dennisdoomen dennisdoomen requested a review from jnyrup September 18, 2025 17:06
Copy link
Member

@jnyrup jnyrup left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coveralls seems down for me, but have you checked the missing lines if they warrant extra tests?

@dennisdoomen dennisdoomen force-pushed the expose-inline-assertion-type branch from e8d5da8 to a5f0796 Compare September 20, 2025 15:48
This was referenced Jan 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Assertions on System.Text.Json

3 participants