fix: aligns exception and diagnostics behaviour across the API surface - #3078
Draft
Vincent Biret (baywet) with Copilot wants to merge 17 commits into
Draft
fix: aligns exception and diagnostics behaviour across the API surface#3078Vincent Biret (baywet) with Copilot wants to merge 17 commits into
Vincent Biret (baywet) with Copilot wants to merge 17 commits into
Conversation
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix inconsistent behavior in OpenApiYamlReader.Read for error handling
Document reader parser-level exceptions
Sep 10, 2026
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
Vincent Biret (baywet)
approved these changes
Sep 11, 2026
Vincent Biret (baywet)
marked this pull request as ready for review
September 11, 2026 11:58
Vincent Biret (baywet)
enabled auto-merge (squash)
September 11, 2026 11:58
Vincent Biret (baywet)
disabled auto-merge
September 11, 2026 11:58
Vincent Biret (baywet)
enabled auto-merge (squash)
September 11, 2026 11:58
This comment was marked as outdated.
This comment was marked as outdated.
Vincent Biret (baywet)
disabled auto-merge
September 11, 2026 12:09
Vincent Biret (baywet)
marked this pull request as draft
September 11, 2026 12:10
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
This comment was marked as outdated.
This comment was marked as outdated.
Vincent Biret (baywet)
approved these changes
Sep 11, 2026
There was a problem hiding this comment.
🔵 Needs a closer look
Async empty-stream handling and inconsistent parser-failure contracts remain unresolved.
Review details
Suppressed comments (6)
src/Microsoft.OpenApi/Models/OpenApiDocument.cs:786
- This wrapper delegates directly to
OpenApiModelFactory.Parse, whose remarks explicitly state that parser failures occurring before a result can be created may throw. Omitting that caveat here givesOpenApiDocument.Parsea stronger and misleading contract than the implementation it exposes; mirror the factory wording in this public convenience API.
/// OpenAPI semantic and parser errors are returned in the <see cref="ReadResult.Diagnostic"/>; a null or empty input string throws <see cref="ArgumentException"/>.
/// </remarks>
src/Microsoft.OpenApi/Models/OpenApiDocument.cs:770
LoadAsyncdoes not follow this documented empty-input contract whenformatis omitted.PrepareStreamForReadingAsyncclassifies an empty stream as YAML, but the default settings register only JSON, soInternalLoadAsynccallsGetReader("yaml")and throwsNotSupportedExceptionbefore the reader can create a diagnostic. Add the empty-stream handling to the async path (and coverOpenApiDocument.LoadAsync(new MemoryStream())) or narrow this remark.
/// <remarks>
/// OpenAPI semantic and parser errors are returned in the <see cref="ReadResult.Diagnostic"/>.
/// </remarks>
src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs:47
- This is a runtime contract change, not a documentation-only update: the widened catch makes
Read,ReadAsync, andReadFragmentreturn diagnostics for anullJSON root instead of propagatingInvalidOperationException(andInternalLoadalso changes empty-streamLoadbehavior). The new tests explicitly assert these changes, so please update the PR type/description and release notes to acknowledge the behavioral change, or revert the implementation if the change must remain documentation-only.
catch (Exception ex) when (ex is JsonException or InvalidOperationException)
{
diagnostic.Errors.Add(CreateOpenApiError(ex, true));
src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs:125
- This contract is still violated by the async convenience path for an empty stream. With the default settings,
PrepareStreamForReadingAsyncinfers YAML for an emptyMemoryStream, andInternalLoadAsynccallssettings.GetReader(format)before any empty-input diagnostic; because default settings register only JSON,OpenApiDocument.LoadAsync(new MemoryStream())throwsNotSupportedExceptioninstead of returning aReadResult. Handle the empty stream before reader lookup (and add an async regression test) so this matches the synchronous path documented here.
/// OpenAPI semantic and parser errors are returned in the <see cref="ReadResult.Diagnostic"/>.
src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs:242
- The generic overload has the same pre-result failure boundary as the non-generic
Parseabove, but its new remarks omit the caveat that parser failures may throw before a diagnostic can be created. This leaves the two public parsing APIs with inconsistent contracts; mirror the non-generic wording here.
/// OpenAPI semantic and parser errors are returned in the <paramref name="diagnostic"/>.
/// <see cref="ArgumentException"/> is thrown when <paramref name="input"/> is null or empty before parsing starts.
src/Microsoft.OpenApi/Reader/OpenApiModelFactory.cs:311
- This is a runtime contract change, not documentation-only: the previous
ArgumentExceptionfor an empty stream is replaced with a successfulReadResultcontaining a diagnostic. The new JSON reader catches also change null-root stream calls from throwing to returning diagnostics. Please update the PR type/notes and compatibility assessment to reflect this behavior change rather than describing it as docs-only.
var diagnostic = new OpenApiDiagnostic
{
Format = format,
};
diagnostic.Errors.Add(new OpenApiError(null, $"Cannot parse the stream: {nameof(input)} is empty or contains no elements."));
- Files reviewed: 9/9 changed files
- Comments generated: 0 new
- Review effort level: Lite
|
Peter Ombwa (peombwa)
approved these changes
Sep 11, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



Pull Request
Description
Clarifies reader behavior when input is invalid before OpenAPI parsing begins.
ReadResult.Diagnosticcovers OpenAPI semantic errors; JSON/YAML syntax-level failures may throw before a result can be created.Type of Change
Related Issue(s)
Linked by automation.
Changes Made
IOpenApiReaderread and fragment APIs.OpenApiModelFactoryandOpenApiDocumentconvenience APIs.Testing
Checklist
Versions applicability
See the contributing guidelines for more information about how patches are applied across multiple versions.
Additional Notes
Documentation-only change; no runtime behavior changes.