Skip to content

[OpenApi] Generate schema for JSON Patch endpoints #63052

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 7 commits into from
Aug 4, 2025

Conversation

martincostello
Copy link
Member

Generate schema for JSON Patch endpoints

Generate an OpenAPI schema for JSON Patch endpoints.

Description

Follow-up for #62988 (comment).

I did a bit Googling and came up with a schema that's a synthesis of different examples I found. It could potentially be simplified to not use OneOf.

I couldn't see any obvious reason to generate a different schema for JsonPatchDocument<T> compared to JsonPatchDocument as the patch schema is the same for both as it isn't strongly-typed to the document being patched, so both use the same schema.

Tested locally by adding SwaggerUI to the Sample App and viewing the operation generated, then using the sample request as-is and submitting it and inspecting the in-memory document in the debugger to check that it was deserialized correctly and all seems to work as expected.

image

@Copilot Copilot AI review requested due to automatic review settings August 2, 2025 13:46
@martincostello martincostello requested review from captainsafia and a team as code owners August 2, 2025 13:46
@github-actions github-actions bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Aug 2, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Aug 2, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds OpenAPI schema generation support for JSON Patch endpoints by implementing a dedicated schema for JsonPatchDocument and JsonPatchDocument<T> types. The implementation follows the JSON Patch RFC 6902 specification to generate appropriate OpenAPI documentation.

Key changes:

  • Adds specialized schema generation for JSON Patch documents with proper operation structures (add, replace, test, move, copy, remove)
  • Uses the same schema for both generic and non-generic JsonPatchDocument types since the patch format is identical
  • Sets default media type to "application/json-patch+json" for JSON Patch parameters

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs Implements the core JSON Patch schema generation logic with operation-specific structures
src/OpenApi/src/Services/OpenApiDocumentService.cs Adds default media type handling for JSON Patch parameters
src/OpenApi/src/Services/OpenApiGenerator.cs Updates parameter detection to recognize JSON Patch types as body parameters
src/OpenApi/src/Extensions/JsonTypeInfoExtensions.cs Maps both JsonPatchDocument types to the same schema name
src/OpenApi/src/Microsoft.AspNetCore.OpenApi.csproj Adds reference to JsonPatch.SystemTextJson package
Test files Comprehensive test coverage for various JSON Patch scenarios and edge cases

@martincostello martincostello added feature-json-patch feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Aug 2, 2025
@martincostello
Copy link
Member Author

Looks like adding Microsoft.AspNetCore.JsonPatch.SystemTextJson as a dependency of Microsoft.AspNetCore.OpenApi causes trim warnings as the former is not AoT compatible but the latter is.

Having a quick look at JsonPatch there's a tonne of object in there and I don't think it can be made trivially AoT compatible (it would probably need a source generator?).

A workaround might be to remove the dependency and instead check on the type name, even though that's ick.

@captainsafia
Copy link
Member

Looks like adding Microsoft.AspNetCore.JsonPatch.SystemTextJson as a dependency of Microsoft.AspNetCore.OpenApi causes trim warnings as the former is not AoT compatible but the latter is.

Yep, I figured this problem would come up. 😅 I think the workaround of a type name + assembly name check is probably sufficient for now. In an ideal world, the M.A.JP.STJ package would've been trim-friendly to start but that didn't end up being the chase. :/

Copy link
Member

@captainsafia captainsafia 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 overall! Left some small comments about a tweak to the schema. Would also like @mikekistler to chime in on the proposed schema here.

@martincostello
Copy link
Member Author

Addressed feedback. Also explains why I didn't spot #63073 in this PR, as it updates the snapshots which inadvertently fixes the test (you can see in the diff for that file it only adds, while the other two also have deletes).

Update the sample to include a non-generic JSON patch endpoint.
Generate an appropriate OpenAPI schema for JSON Patch endpoints.
Leftovers from before tests were added.
- Handle custom types derived from `JsonPatchDocument` or `JsonPatchDocument<T>`.
- Add extension method to remove duplicated type checks.
Remove the dependency on `Microsoft.AspNetCore.JsonPatch.SystemTextJson` as it creates type warnings, and instead check the types by name.
- Check for a generic type before type name check.
- Replace `StartsWith()` with `==`.
Add `required` members to the OpenAPI schema for a JSON Patch remove operation.
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.

This looks great! Thanks @martincostello !

@captainsafia captainsafia enabled auto-merge (squash) August 4, 2025 15:22
@captainsafia captainsafia merged commit d84aec3 into dotnet:main Aug 4, 2025
30 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-rc1 milestone Aug 4, 2025
@martincostello martincostello deleted the jsonpath-openapi-schema branch August 4, 2025 16:26
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 community-contribution Indicates that the PR has been added by a community member feature-json-patch feature-openapi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants