Skip to content

feat: add content negotiation with #[Produces] attribute#136

Merged
usernane merged 1 commit into
devfrom
feat/content-negotiation
Jun 1, 2026
Merged

feat: add content negotiation with #[Produces] attribute#136
usernane merged 1 commit into
devfrom
feat/content-negotiation

Conversation

@usernane

@usernane usernane commented Jun 1, 2026

Copy link
Copy Markdown
Member

Summary

Add HTTP content negotiation support via #[Produces] attribute and MediaType constants. Services can declare what content types they produce, and the framework matches against the client's Accept header.

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 types
  • MediaType class with constants for common MIME types
  • WebService::getNegotiatedContentType() returns the matched type for the method to use
  • ErrorResponse::notAcceptable() for 406 responses with supported types listed
  • Negotiation runs in processWithAutoHandling() after auth, before validation
  • Works with ResponseEntity — method sets content type based on negotiation result

UI Changes

N/A

How to Test / Verify

php vendor/bin/phpunit tests/WebFiori/Tests/Http/ContentNegotiationTest.php

14 new tests, 28 assertions. Full suite: 588 tests pass.

Breaking Changes and Migration Steps

None. No #[Produces] = no negotiation = current behavior unchanged.

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • The title of the pull request follows Conventional Commits
  • I added/updated tests (or explained why not)
  • I updated docs (if needed)
  • I ran lint/cs-fixer (if applicable)
  • I considered backward compatibility
  • I considered security

Related issues

N/A — new feature for HTTP spec compliance

…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
@sonarqubecloud

sonarqubecloud Bot commented Jun 1, 2026

Copy link
Copy Markdown

@usernane usernane merged commit bc33b35 into dev Jun 1, 2026
3 checks passed
@codecov

codecov Bot commented Jun 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.82609% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 93.84%. Comparing base (2793917) to head (0826ce1).
⚠️ Report is 4 commits behind head on dev.

Files with missing lines Patch % Lines
WebFiori/Http/WebService.php 97.82% 1 Missing ⚠️
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              
Flag Coverage Δ
php-8.3 93.84% <97.82%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant