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

Basic support for third-party JSON:API extensions #1623

Merged
merged 1 commit into from
Oct 21, 2024
Merged

Conversation

bkoelman
Copy link
Member

@bkoelman bkoelman commented Oct 21, 2024

This PR adds basic support for developing your own JSON:API extensions.

  • IJsonApiOptions has a new Extensions property, and JsonApiOptions has a new IncludeExtensions method, to indicate which extensions are enabled API-wide.
  • New interface IJsonApiContentNegotiator with implementation JsonApiContentNegotiator, which provides a virtual GetPossibleMediaTypes method to indicate which combinations of extensions are allowed for the current request.
  • The new property IJsonApiRequest.Extensions contains the active extensions for the current request.

While JSON:API doesn't specify it as a requirement, JsonApiDotNetCore assumes that an extension always applies to both the request body and the response body, for simplicity. An error is returned when the Content-Type header is incompatible with the Accept header.

The properties on HeaderConstants have been obsoleted, in favor of the new JsonApiMediaType and JsonApiExtension types.

Now that the basic building blocks are in place (which we need for supporting OpenAPI with resource inheritance), the next step (no promises) would be to enable extending the JSON:API request/response body data structure (Document class) with extension-prefixed members. I suspect we'd need that at the top level, in resource objects, and relationship objects. Please open an issue to express your ideas and/or requirements. Right now, obtaining the request body requires some service container magic (see tests in CustomExtensions subdirectory).

Bugfix: Always require clients to send an Accept HTTP header when an extension is used; this is required by JSON:API. This currently affects the usage of atomic:operations.

This PR optimizes test execution by reducing redundant parsing of the JSON:API media type.

Aside from the text above, no documentation page is provided yet, because we don't have a solid end-to-end use case for developing a custom JSON:API extension at the moment. Feedback welcome!

QUALITY CHECKLIST

Copy link

codecov bot commented Oct 21, 2024

Codecov Report

Attention: Patch coverage is 91.62996% with 19 lines in your changes missing coverage. Please review.

Project coverage is 90.72%. Comparing base (eb2ef95) to head (fe4b81a).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...c/JsonApiDotNetCore/Middleware/JsonApiMediaType.cs 92.95% 5 Missing ⚠️
...iDotNetCore/Middleware/JsonApiContentNegotiator.cs 95.29% 2 Missing and 2 partials ⚠️
...c/JsonApiDotNetCore/Middleware/JsonApiExtension.cs 73.33% 3 Missing and 1 partial ⚠️
.../JsonApiDotNetCore/Middleware/JsonApiMiddleware.cs 88.23% 2 Missing and 2 partials ⚠️
src/JsonApiDotNetCore/CollectionExtensions.cs 60.00% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1623      +/-   ##
==========================================
- Coverage   90.79%   90.72%   -0.07%     
==========================================
  Files         349      352       +3     
  Lines       11314    11438     +124     
  Branches     1849     1879      +30     
==========================================
+ Hits        10272    10377     +105     
- Misses        682      695      +13     
- Partials      360      366       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bkoelman bkoelman marked this pull request as ready for review October 21, 2024 03:08
… content negotation and exposure of the active extensions

Bugfix: always require Accept header in atomic:operations requests
@bkoelman bkoelman merged commit 8ff2900 into master Oct 21, 2024
16 checks passed
@bkoelman bkoelman deleted the custom-extensions branch October 21, 2024 21:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant