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

Add BeValidJson extensions for strings #2558

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

vbreuss
Copy link
Contributor

@vbreuss vbreuss commented Jan 14, 2024

Draft for #2556

Add the following methods to the StringAssertions (similar to StringAssertionsExtensions in FluentAssertions.Json):

public AndWhichConstraint<TAssertions, JsonDocument> BeValidJson(string because = "", params object[] becauseArgs);
public AndWhichConstraint<TAssertions, JsonDocument> BeValidJson(JsonDocumentOptions options, string because = "", params object[] becauseArgs);

These methods should assert that the string is a valid JSON document.

IMPORTANT

  • If the PR touches the public API, the changes have been approved in a separate issue with the "api-approved" label.
  • The code complies with the Coding Guidelines for C#.
  • The changes are covered by unit tests which follow the Arrange-Act-Assert syntax and the naming conventions such as is used in these tests.
  • If the PR adds a feature or fixes a bug, please update the release notes with a functional description that explains what the change means to consumers of this library, which are published on the website.
  • If the PR changes the public API the changes needs to be included by running AcceptApiChanges.ps1 or AcceptApiChanges.sh.
  • If the PR affects the documentation, please include your changes in this pull request so the documentation will appear on the website.
    • Please also run ./build.sh --target spellcheck or .\build.ps1 --target spellcheck before pushing and check the good outcome

Copy link

github-actions bot commented Jan 14, 2024

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

@coveralls
Copy link

coveralls commented Jan 14, 2024

Pull Request Test Coverage Report for Build 7519123647

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.004%) to 97.476%

Totals Coverage Status
Change from base Build 7517844575: -0.004%
Covered Lines: 11775
Relevant Lines: 11958

💛 - Coveralls

@vbreuss vbreuss force-pushed the topic/json-support/be-valid-json branch from d46683f to c82b18e Compare January 14, 2024 12:22
@vbreuss vbreuss force-pushed the topic/json-support/be-valid-json branch from c82b18e to fc41d92 Compare January 14, 2024 19:20
.FailWith("Expected {context:string} to be valid JSON{reason}, but parsing failed with {0}.", jsonException.Message);
}

return new AndWhichConstraint<TAssertions, JsonDocument>((TAssertions)this, json);
Copy link

Choose a reason for hiding this comment

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

This way of using JsonDocument seems to be against the documentation that states that it is important to dispose (or clone) JsonDocument instances. I'm not sure I fully understand the consequences they warn about here, but my suggestion would be to use the raw Utf8JsonReader for BeValidJson and not provide the document for further usage.

https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/use-dom#jsondocument-is-idisposable

JsonDocument builds an in-memory view of the data into a pooled buffer. Therefore the JsonDocument type implements IDisposable and needs to be used inside a using block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants