Repository navigation
fix: say which value failed to parse and why references could not be resolved #54
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
199ade8
fix: say which value failed to parse and why references could not be …
bielu 1084c6f
Merge branch 'vnext' into fix/reader-error-details
VisualBean 0c63444
fix: accept only unsigned digits as pointer array indices and strip o…
bielu 9b361e9
fix: reject leading-zero pointer indices and report invalid schema bo…
bielu 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 |
|---|---|---|
|
|
@@ -197,6 +197,7 @@ private async Task ResolveReferencesAsync(AsyncApiDiagnostic diagnostic, IAsyncA | |
| continue; | ||
| } | ||
|
|
||
| var errorCount = diagnostic.Errors.Count; | ||
| IAsyncApiSerializable component = null; | ||
| if (reference.Reference.IsExternal) | ||
| { | ||
|
|
@@ -215,7 +216,12 @@ private async Task ResolveReferencesAsync(AsyncApiDiagnostic diagnostic, IAsyncA | |
|
|
||
| if (component == null) | ||
| { | ||
| diagnostic.Errors.Add(new AsyncApiError(string.Empty, $"Unable to deserialize reference '{reference.Reference.Reference}'")); | ||
| // Only add a generic error when resolving did not already explain what went wrong. | ||
| if (diagnostic.Errors.Count == errorCount) | ||
| { | ||
| diagnostic.Errors.Add(new AsyncApiError(string.Empty, $"Unable to deserialize reference '{reference.Reference.Reference}'")); | ||
| } | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -242,6 +248,7 @@ private void ResolveReferences(AsyncApiDiagnostic diagnostic, IAsyncApiSerializa | |
| continue; | ||
| } | ||
|
|
||
| var errorCount = diagnostic.Errors.Count; | ||
| IAsyncApiSerializable component = null; | ||
| if (reference.Reference.IsExternal) | ||
| { | ||
|
|
@@ -260,7 +267,12 @@ private void ResolveReferences(AsyncApiDiagnostic diagnostic, IAsyncApiSerializa | |
|
|
||
| if (component == null) | ||
| { | ||
| diagnostic.Errors.Add(new AsyncApiError(string.Empty, $"Unable to deserialize reference '{reference.Reference.Reference}'")); | ||
| // Only add a generic error when resolving did not already explain what went wrong. | ||
| if (diagnostic.Errors.Count == errorCount) | ||
| { | ||
| diagnostic.Errors.Add(new AsyncApiError(string.Empty, $"Unable to deserialize reference '{reference.Reference.Reference}'")); | ||
| } | ||
|
|
||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -349,9 +361,9 @@ private IAsyncApiSerializable ResolveStreamReference(Stream stream, IAsyncApiRef | |
| { | ||
| json = this.ReadToJson(stream); | ||
| } | ||
| catch | ||
| catch (Exception ex) | ||
| { | ||
| diagnostic.Errors.Add(new AsyncApiError(string.Empty, $"Unable to deserialize reference: '{reference.Reference.Reference}'")); | ||
| diagnostic.Errors.Add(new AsyncApiError(reference.Reference.Reference, $"Unable to deserialize reference '{reference.Reference.Reference}': {ex.Message}")); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ahh how I love the naming rules i made up for the reference heirachy. |
||
| return null; | ||
| } | ||
|
|
||
|
|
||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| namespace ByteBard.AsyncAPI.Tests | ||
| { | ||
| using System.Text.Json.Nodes; | ||
| using ByteBard.AsyncAPI.Models; | ||
| using ByteBard.AsyncAPI.Readers; | ||
| using ByteBard.AsyncAPI.Readers.ParseNodes; | ||
| using FluentAssertions; | ||
| using NUnit.Framework; | ||
|
|
||
| internal class ReaderErrorDetails_Should : TestBase | ||
| { | ||
| [TestCase("abc", "Value 'abc' could not parse to integer. Expected a whole number.")] | ||
| [TestCase("1.5", "Value '1.5' could not parse to integer. Expected a whole number.")] | ||
| [TestCase("+-1", "Value '+-1' could not parse to integer. Expected a whole number.")] | ||
| [TestCase("--1", "Value '--1' could not parse to integer. Expected a whole number.")] | ||
| [TestCase("99999999999","Value '99999999999' could not parse to integer. It must be between -2147483648 and 2147483647.")] | ||
| public void SchemaIntegerField_ReportsTheValueAndWhyItIsInvalid(string value, string expectedMessage) | ||
| { | ||
| var yaml = $"type: string\nmaxLength: '{value}'"; | ||
|
|
||
| new AsyncApiStringReader().ReadFragment<AsyncApiJsonSchema>(yaml, AsyncApiVersion.AsyncApi3_0, out var diagnostics); | ||
|
|
||
| var error = diagnostics.Errors.Should().ContainSingle().Subject; | ||
| error.Message.Should().Be(expectedMessage); | ||
| error.Pointer.Should().Be("#/maxLength"); | ||
| } | ||
|
|
||
| [TestCase("uniqueItems")] | ||
| [TestCase("readOnly")] | ||
| [TestCase("writeOnly")] | ||
| [TestCase("deprecated")] | ||
| public void SchemaBooleanField_ReportsTheValueAndWhyItIsInvalid(string field) | ||
| { | ||
| var yaml = $"type: string\n{field}: 'yes please'"; | ||
|
|
||
| new AsyncApiStringReader().ReadFragment<AsyncApiJsonSchema>(yaml, AsyncApiVersion.AsyncApi3_0, out var diagnostics); | ||
|
|
||
| var error = diagnostics.Errors.Should().ContainSingle().Subject; | ||
| error.Message.Should().Be("Value 'yes please' could not parse to bool. Expected true or false."); | ||
| error.Pointer.Should().Be($"#/{field}"); | ||
| } | ||
|
|
||
| [Test] | ||
| public void MissingLocalReference_IsReportedOnce() | ||
| { | ||
| var yaml = | ||
| """ | ||
| asyncapi: 3.0.0 | ||
| info: | ||
| title: test | ||
| version: 1.0.0 | ||
| channels: | ||
| users: | ||
| messages: | ||
| created: | ||
| payload: | ||
| $ref: '#/components/schemas/Missing' | ||
| """; | ||
|
|
||
| new AsyncApiStringReader().Read(yaml, out var diagnostics); | ||
|
|
||
| diagnostics.Errors.Should().ContainSingle() | ||
| .Which.Message.Should().Be("Could not resolve reference fragment."); | ||
| } | ||
|
|
||
| [TestCase("/info/title/nested")] | ||
| [TestCase("/tags/5")] | ||
| [TestCase("/tags/name")] | ||
| [TestCase("/tags/-0")] | ||
| [TestCase("/tags/+0")] | ||
| [TestCase("/tags/ 0")] | ||
| [TestCase("/tags/00")] | ||
| [TestCase("/tags/01")] | ||
| [TestCase("/missing")] | ||
| public void Find_ReturnsNull_WhenThePointerDoesNotLeadToANode(string pointer) | ||
| { | ||
| var json = JsonNode.Parse("""{ "info": { "title": "test" }, "tags": [ { "name": "a" } ] }"""); | ||
|
|
||
| new JsonPointer(pointer).Find(json).Should().BeNull(); | ||
| } | ||
|
|
||
| [Test] | ||
| public void Find_FollowsObjectsAndArrays() | ||
| { | ||
| var json = JsonNode.Parse("""{ "tags": [ { "name": "a" } ] }"""); | ||
|
|
||
| new JsonPointer("/tags/0/name").Find(json).GetValue<string>().Should().Be("a"); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
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.
a bit on the simple side, but lets run with it for now.