feat: add content negotiation with #[Produces] attribute#136
Merged
Conversation
…e constants - Add #[Produces] attribute to declare supported response content types - Add MediaType class with constants (JSON, XML, HTML, PLAIN, etc.) - Add WebService::getNegotiatedContentType() for methods to read the result - Add ErrorResponse::notAcceptable() for 406 responses - Negotiation runs after auth, before validation in processWithAutoHandling() - No #[Produces] = no negotiation (100% backward compatible) - Accept: */* or missing = server's first preference - No match = 406 Not Acceptable with supported types listed - Update README with content negotiation documentation - 14 new tests covering all negotiation paths
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #136 +/- ##
============================================
+ Coverage 93.75% 93.84% +0.08%
- Complexity 1317 1334 +17
============================================
Files 39 39
Lines 3218 3264 +46
============================================
+ Hits 3017 3063 +46
Misses 201 201
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Add HTTP content negotiation support via
#[Produces]attribute andMediaTypeconstants. Services can declare what content types they produce, and the framework matches against the client'sAcceptheader.Motivation
Enterprise APIs behind gateways need proper 406 responses when clients request unsupported formats. The library always returned JSON regardless of the Accept header. This adds spec-compliant content negotiation (RFC 7231 §5.3.2).
Changes
#[Produces(MediaType::JSON, MediaType::XML)]attribute declares supported typesMediaTypeclass with constants for common MIME typesWebService::getNegotiatedContentType()returns the matched type for the method to useErrorResponse::notAcceptable()for 406 responses with supported types listedprocessWithAutoHandling()after auth, before validationResponseEntity— method sets content type based on negotiation resultUI Changes
N/A
How to Test / Verify
14 new tests, 28 assertions. Full suite: 588 tests pass.
Breaking Changes and Migration Steps
None. No
#[Produces]= no negotiation = current behavior unchanged.Checklist
Related issues
N/A — new feature for HTTP spec compliance