-
Notifications
You must be signed in to change notification settings - Fork 0
Fix compliance findings in Frends.JSON.Validate (net8, analyzers, error handling) #44
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
Draft
Copilot
wants to merge
7
commits into
main
Choose a base branch
from
copilot/fix-compliance-findings-again
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
933acac
Initial plan
Copilot 88ef94a
Fix compliance findings in Frends.JSON.Validate
Copilot a4d697e
Rename ThrowOnInvalidJson to FailOnInvalidJson, bump version to 2.0.0
Copilot eea1cee
Merge 1.1.0 and 2.0.0 CHANGELOG entries into a single 2.0.0 entry
Copilot 9603027
Update Frends.Newtonsoft.SchemaActivation to version 1.1.0
MichalFrends1 980fb5b
Fix Validate build by removing private package restore dependency
Copilot dbdccd2
Revert "Fix Validate build by removing private package restore depend…
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,13 @@ | ||
| # Changelog | ||
|
|
||
| ## [2.0.0] - 2026-08-11 | ||
| ### Changed | ||
| - The task now targets .NET 8. | ||
| - Added `ThrowErrorOnFailure` and `ErrorMessageOnFailure` options: you can now choose whether the task throws an exception or returns a failed result when an error occurs, and optionally provide a custom error message. | ||
| - Added a `CancellationToken` parameter to support task cancellation. | ||
| - The `Result` object now includes an `Error` property with details when the task fails. | ||
| - [Breaking Change] Renamed the `ThrowOnInvalidJson` option to `FailOnInvalidJson` to better reflect its purpose: it controls whether invalid JSON is treated as an error at all, independent of whether that error is thrown or returned (which is controlled by `ThrowErrorOnFailure`). Update any existing task configurations to use `FailOnInvalidJson`. | ||
|
|
||
| ## [1.0.0] - 2023-06-15 | ||
| ### Added | ||
| - Initial implementation | ||
52 changes: 52 additions & 0 deletions
52
...alidate/Frends.JSON.Validate.UnitTests/rends.JSON.Validate.UnitTests/ErrorHandlerTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using Frends.JSON.Validate.Definitions; | ||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System; | ||
| using System.Threading; | ||
|
|
||
| namespace Frends.JSON.Validate.UnitTests; | ||
|
|
||
| [TestClass] | ||
| public class ErrorHandlerTests | ||
| { | ||
| private const string CustomErrorMessage = "CustomErrorMessage"; | ||
|
|
||
| private static Input DefaultInput() => new() | ||
| { | ||
| Json = "not valid json {{{{", | ||
| JsonSchema = @"{'type': 'object'}" | ||
| }; | ||
|
|
||
| private static Options DefaultOptions() => new() | ||
| { | ||
| FailOnInvalidJson = true, | ||
| ThrowErrorOnFailure = true, | ||
| }; | ||
|
|
||
| [TestMethod] | ||
| public void Should_Throw_Error_When_ThrowErrorOnFailure_Is_True() | ||
| { | ||
| var ex = Assert.ThrowsException<Exception>(() => | ||
| JSON.Validate(DefaultInput(), DefaultOptions(), CancellationToken.None)); | ||
| Assert.IsNotNull(ex); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Should_Return_Failed_Result_When_ThrowErrorOnFailure_Is_False() | ||
| { | ||
| var options = DefaultOptions(); | ||
| options.ThrowErrorOnFailure = false; | ||
| var result = JSON.Validate(DefaultInput(), options, CancellationToken.None); | ||
| Assert.IsFalse(result.Success); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void Should_Use_Custom_ErrorMessageOnFailure() | ||
| { | ||
| var options = DefaultOptions(); | ||
| options.ErrorMessageOnFailure = CustomErrorMessage; | ||
| var ex = Assert.ThrowsException<Exception>(() => | ||
| JSON.Validate(DefaultInput(), options, CancellationToken.None)); | ||
| Assert.IsNotNull(ex); | ||
| Assert.IsTrue(ex.Message.Contains(CustomErrorMessage)); | ||
| } | ||
| } |
2 changes: 1 addition & 1 deletion
2
...ON.Validate.UnitTests/rends.JSON.Validate.UnitTests/Frends.JSON.Validate.UnitTests.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 24 additions & 3 deletions
27
Frends.JSON.Validate/Frends.JSON.Validate/Definitions/Options.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,34 @@ | ||
| namespace Frends.JSON.Validate.Definitions; | ||
| using System.ComponentModel; | ||
| using System.ComponentModel.DataAnnotations; | ||
|
|
||
| namespace Frends.JSON.Validate.Definitions; | ||
|
|
||
| /// <summary> | ||
| /// Options parameters. | ||
| /// </summary> | ||
| public class Options | ||
| { | ||
| /// <summary> | ||
| /// A flag to indicate whether an error should be thrown if JSON was invalid. | ||
| /// A flag to indicate whether invalid JSON should be treated as an error. | ||
| /// When true, validation failure or parse error is treated as an error (subject to ThrowErrorOnFailure). | ||
| /// When false, parse/validation errors are returned as a non-successful result with Success=false without going through error handling. | ||
| /// </summary> | ||
| /// <example>true</example> | ||
| public bool FailOnInvalidJson { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// If set to true, the task will throw an exception on failure. | ||
| /// If set to false, the task returns a result with Success = false. | ||
| /// </summary> | ||
| /// <example>true</example> | ||
| public bool ThrowOnInvalidJson { get; set; } | ||
| [DefaultValue(true)] | ||
| public bool ThrowErrorOnFailure { get; set; } = true; | ||
|
|
||
| /// <summary> | ||
| /// Optional custom error message used when ThrowErrorOnFailure is true or when returning a failed result. | ||
| /// </summary> | ||
| /// <example></example> | ||
| [DisplayFormat(DataFormatString = "Text")] | ||
| [DefaultValue("")] | ||
| public string ErrorMessageOnFailure { get; set; } = string.Empty; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
Frends.JSON.Validate/Frends.JSON.Validate/Helpers/ErrorHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| using System; | ||
| using Frends.JSON.Validate.Definitions; | ||
|
|
||
| namespace Frends.JSON.Validate.Helpers; | ||
|
|
||
| internal static class ErrorHandler | ||
| { | ||
| internal static Result Handle(this Exception exception, Options options, bool throwCanceled = true) | ||
| { | ||
| ThrowIfCanceled(exception, throwCanceled); | ||
| if (options.ThrowErrorOnFailure) ThrowBaseException(exception, options.ErrorMessageOnFailure); | ||
|
|
||
| return ReturnResult(exception, options.ErrorMessageOnFailure); | ||
| } | ||
|
|
||
| private static void ThrowIfCanceled(Exception exception, bool throwCanceled = true) | ||
| { | ||
| if (throwCanceled && exception is OperationCanceledException) throw exception; | ||
| } | ||
|
|
||
| private static void ThrowBaseException(Exception exception, string customMessage = null) | ||
| { | ||
| if (string.IsNullOrEmpty(customMessage)) | ||
| throw new Exception(exception.Message, exception); | ||
|
|
||
| throw new Exception(customMessage, exception); | ||
| } | ||
|
|
||
| private static Result ReturnResult(Exception exception, string customMessage = null) | ||
| { | ||
| var errorMessage = string.IsNullOrEmpty(customMessage) | ||
| ? exception.Message | ||
| : $"{customMessage}: {exception.Message}"; | ||
|
|
||
| return new Result | ||
| { | ||
| Success = false, | ||
| Error = new Error | ||
| { | ||
| Message = errorMessage, | ||
| AdditionalInfo = exception, | ||
| }, | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot we didn't release 1.1.0 we changed from 1.0.0 to 2.0.0, unify 11.0 and 2.0.0 under 2.0.0. and wire [Breaking Change] at the start of this sentence: [Breaking Change] Renamed the
ThrowOnInvalidJson...There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Merged the
1.1.0and2.0.0entries into a single[2.0.0]entry and prefixed the rename line with[Breaking Change].