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

Harden schema reference transformer for relative references #59763

Merged
merged 5 commits into from
Jan 8, 2025

Conversation

captainsafia
Copy link
Member

Addresses #59677, #58968, and #59427.

This PR resolves a set of issues that have been reported with schema reference resolution in the OpenAPI implementation. The crux of the issue deals with the way the underlying System.Text.Json generates relative references for schemas that include recursion or duplication.

For example, given the following type:

private class Root
{
    public Item Item1 { get; set; } = null!;
    public Item Item2 { get; set; } = null!;
}

private class Item
{
    public string[] Name { get; set; } = null!;
    public int value { get; set; }
}

The JsonSchemaExporter will produce the following schema:

{
  "type": "object",
  "properties": {
    "item1": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "name": {
          "type": [
            "array",
            "null"
          ],
          "items": {
            "type": "string",
            "nullable": false,
            "format": null,
            "x-schema-id": null,
            "pattern": null
          },
          "nullable": true
        },
        "value": {
          "type": "integer",
          "pattern": null,
          "nullable": false,
          "format": "int32",
          "x-schema-id": null
        }
      },
      "x-schema-id": "Item",
      "nullable": true
    },
    "item2": {
      "type": [
        "object",
        "null"
      ],
      "properties": {
        "name": {
          "$ref": "#/properties/item1/properties/name",
          "nullable": true
        },
        "value": {
          "type": "integer",
          "pattern": null,
          "nullable": false,
          "format": "int32",
          "x-schema-id": null
        }
      },
      "x-schema-id": "Item",
      "nullable": true
    }
  },
  "x-schema-id": "Root"
}

Where a relative reference to the first name schema in the Item1 property is used in the Item2 property. STJ anchors these references relative to the base type that was provided to the exporter, the Root type in this case. These relative references don't map to the OpenAPI document where the root for all schemas is the #/components/schemas entry.

To resolve this issue, we update the OpenAPI schema comparer to treat schemas where one schema uses a relative reference and the other does not as equivalent. In this case, the schemas for the item1 and item2 properties would be considered as equivalent since on contains a reference mapped to the other.

The current implementation is optimized to address the issue for when nested types are included directly in the referenced property. This PR expands the fix to address relative references that occur anywhere in the schema.

@captainsafia captainsafia added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc labels Jan 7, 2025
@captainsafia captainsafia requested a review from a team as a code owner January 7, 2025 22:10
Copy link
Contributor

@mikekistler mikekistler left a comment

Choose a reason for hiding this comment

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

Looks good! 👍

@shkarface
Copy link

We also have a similar issue, when can we expect a fix to be released for this issue?

@captainsafia
Copy link
Member Author

We also have a similar issue, when can we expect a fix to be released for this issue?

This PR will need to be approved and merged into main first before being back ported and going through the servicing process (ref). Release timelines vary depending on when the PRs to main and release/9.0 get merged so we'll know then.

@captainsafia captainsafia enabled auto-merge (squash) January 8, 2025 18:25
@captainsafia captainsafia merged commit 77b91e1 into main Jan 8, 2025
27 checks passed
@captainsafia captainsafia deleted the safia/openapi-dupes branch January 8, 2025 19:31
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview1 milestone Jan 8, 2025
@captainsafia
Copy link
Member Author

/backport to release/9.0

Copy link
Contributor

github-actions bot commented Jan 8, 2025

Started backporting to release/9.0: https://github.com/dotnet/aspnetcore/actions/runs/12677731301

Copy link
Contributor

github-actions bot commented Jan 8, 2025

@captainsafia backporting to release/9.0 failed, the patch most likely resulted in conflicts:

$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch

Applying: Harden schema reference transformer for relative references
Using index info to reconstruct a base tree...
M	src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs
M	src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Transformers/Implementations/OpenApiSchemaReferenceTransformerTests.cs
Falling back to patching base and 3-way merge...
Auto-merging src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Transformers/Implementations/OpenApiSchemaReferenceTransformerTests.cs
Auto-merging src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs
CONFLICT (content): Merge conflict in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config advice.mergeConflict false"
Patch failed at 0001 Harden schema reference transformer for relative references
Error: The process '/usr/bin/git' failed with exit code 128

Please backport manually!

captainsafia added a commit that referenced this pull request Jan 8, 2025
* Harden schema reference transformer for relative references

* Apply suggestions from code review

Co-authored-by: Brennan <[email protected]>

* Remove uneeded setting

* One more cleanup

* Check for top-level schemas in tests

---------

Co-authored-by: Brennan <[email protected]>
wtgodbe pushed a commit that referenced this pull request Jan 10, 2025
…nces (#59779)

* Harden schema reference transformer for relative references (#59763)

* Harden schema reference transformer for relative references

* Apply suggestions from code review

Co-authored-by: Brennan <[email protected]>

* Remove uneeded setting

* One more cleanup

* Check for top-level schemas in tests

---------

Co-authored-by: Brennan <[email protected]>

* Update src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Transformers/Implementations/OpenApiSchemaReferenceTransformerTests.cs

---------

Co-authored-by: Brennan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants