Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 22, 2025

Pull Request

Description

Extension parser exceptions were missing parent path segments in error pointers. For instance, an exception in #/definitions/demo/x-tag would report as #/definitions/x-tag, omitting demo.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Other (please describe):

Changes Made

  • Wrapped extension parser invocations in LoadExtension methods with try-catch blocks across all OpenAPI versions (V2, V3, V3.1, V3.2)
  • On OpenApiException, set pointer to current context location before logging to diagnostic
  • Return default JsonNodeExtension after logging to allow parsing to continue
  • Added test coverage for V2.0, V3.0, and V3.1 scenarios using raw string literals for consistency

Example:

var settings = new OpenApiReaderSettings
{
    ExtensionParsers = { { "x-tag", (any, version) => throw new OpenApiException("Testing") } }
};
var result = OpenApiDocument.Parse(json, "json", settings);

// Before: error.Pointer = "#/definitions/x-tag" (missing "demo")
// After:  error.Pointer = "#/definitions/demo/x-tag" (correct)

Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing performed
  • All existing tests pass

Checklist

  • My code follows the code style of this project
  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

Versions applicability

  • My change applies to the version 1.X of the library, if so PR link:
  • My change applies to the version 2.X of the library, if so PR link:
  • My change applies to the version 3.X of the library, if so PR link:
  • I have evaluated the applicability of my change against the other versions above.

Additional Notes

All LoadExtension implementations now follow a consistent exception handling pattern. The fix preserves parsing continuity by returning a default extension when custom parsers fail. Test code uses raw string literals ("""...""") for multi-line JSON data, consistent with the project's coding style.

Original prompt

This section details on the original issue you should resolve

<issue_title>OpenApiError.Pointer skips a level if extension parser throws OpenApiException</issue_title>
<issue_description># To reproduce

Microsoft.OpenApi.Readers 1.2.2

OpenAPI 2.0 document:

{
  "swagger": "2.0",
  "info": {
    "title": "Demo",
    "version": "1"
  },
  "paths": {},
  "definitions": {
    "demo": {
      "x-tag": null
    }
  }
}

Read with an extension parser that throws OpenApiException:

var settings = new OpenApiReaderSettings()
{
    ExtensionParsers =
    {
        { "x-tag", (any, version) => throw new OpenApiException("Testing") },
    },
};
var apiReader = new OpenApiStreamReader(settings);
OpenApiDocument document = apiReader.Read(stream, out OpenApiDiagnostic diagnostic);
Console.Error.WriteLine(diagnostic.Errors[0]);

Expected result

Testing [#/definitions/demo/x-tag]

Actual result

Testing [#/definitions/x-tag]

</issue_description>

<agent_instructions>start from support/v2 and target support/v2 for the pull request.
make sure you start by defining unit tests first.
make sure you consider the details in my response to this issue.
make sure you follow conventional commits conventions.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@baywet Hi @KalleOlaviNiemitalo Thank you for using the SDK and for reaching out. Thank you for your patience.

This is implemented here (3 other instances)

if (node.Context.ExtensionParsers is not null && node.Context.ExtensionParsers.TryGetValue(name, out var parser))

And that's happening because the call to the parser should be surrounded by a try catch, and when an exception is thrown, it should be captured, the pointer amended, and logged as an error in the context with the right pointer.

Additional tests should be added in this file.

I'll assign copilot to get a fix for this in v2 and v3, but we won't be addressing this in v1.

Let us know if you have any additional comments or questions.</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 2 commits December 22, 2025 19:07
… pointers

When extension parsers throw OpenApiException, the exceptions are now caught in LoadExtension methods across all OpenAPI versions (V2, V3, V3.1, V3.2). This ensures the error pointer correctly includes all path segments (e.g., #/definitions/demo/x-tag instead of #/definitions/x-tag).

Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
Moved the return statement for JsonNodeExtension outside the catch block to match the pattern used in V3, V31, and V32 deserializers.

Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix OpenApiError.Pointer skipping levels on exceptions fix: correct error pointer when extension parser throws OpenApiException Dec 22, 2025
Copilot AI requested a review from baywet December 22, 2025 19:12
Changed from verbatim string literals (@"...") to raw string literals ("""...""") for consistency with existing test code.

Co-authored-by: baywet <7905502+baywet@users.noreply.github.com>
Copilot AI requested a review from baywet December 22, 2025 19:22
@baywet baywet marked this pull request as ready for review December 22, 2025 19:32
@baywet baywet requested a review from a team as a code owner December 22, 2025 19:32
@baywet baywet enabled auto-merge December 22, 2025 19:32
@baywet baywet disabled auto-merge December 22, 2025 19:35
Signed-off-by: Vincent Biret <vibiret@microsoft.com>
@baywet baywet enabled auto-merge December 22, 2025 19:45
@sonarqubecloud
Copy link

Quality Gate Failed Quality Gate failed

Failed conditions
32.0% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@baywet baywet merged commit 43c75a9 into main Dec 22, 2025
16 of 17 checks passed
@baywet baywet deleted the copilot/fix-openapi-pointer-skip branch December 22, 2025 20:31
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.

OpenApiError.Pointer skips a level if extension parser throws OpenApiException

3 participants