-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[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
[OpenApi] Generate schema for JSON Patch endpoints #63052
Conversation
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.
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 |
Looks like adding Having a quick look at JsonPatch there's a tonne of A workaround might be to remove the dependency and instead check on the type name, even though that's ick. |
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. :/ |
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.
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.
...ion/snapshots/OpenApiDocumentLocalizationTests.VerifyOpenApiDocumentIsInvariant.verified.txt
Show resolved
Hide resolved
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.
dceea9a
to
f91de94
Compare
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.
This looks great! Thanks @martincostello !
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 toJsonPatchDocument
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.