Skip to content

Map Symfony Validator constraint violations to HTTP 422 (RFC 7807)#20

Merged
stixx merged 1 commit into
mainfrom
unprocessable-entity
May 8, 2026
Merged

Map Symfony Validator constraint violations to HTTP 422 (RFC 7807)#20
stixx merged 1 commit into
mainfrom
unprocessable-entity

Conversation

@stixx
Copy link
Copy Markdown
Owner

@stixx stixx commented May 8, 2026

Summary by CodeRabbit

Release Notes

  • Bug Fixes
    • Command validation errors now return HTTP 422 (Unprocessable Entity) status code instead of HTTP 400, providing more semantically accurate error responses for failed validation.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 8, 2026

Review Change Stack
No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: ea7323e8-2b1e-4f15-b206-112a607713c6

📥 Commits

Reviewing files that changed from the base of the PR and between 457c0dd and b601667.

📒 Files selected for processing (6)
  • src/Controller/CommandController.php
  • src/Exception/ApiProblemException.php
  • src/Exception/DefaultExceptionToApiProblemTransformer.php
  • tests/Functional/ScenarioTest.php
  • tests/Unit/Controller/CommandControllerTest.php
  • tests/Unit/Exception/ApiProblemExceptionTest.php

Walkthrough

This PR updates validation error responses from HTTP 400 (Bad Request) to HTTP 422 (Unprocessable Entity). A new ApiProblemException::unprocessableEntity() factory method is introduced to construct 422 responses, the title mapping is added to the transformer, the controller integrates the new method, and all tests are updated to verify the corrected status code.

Changes

HTTP 422 Validation Error Status Codes

Layer / File(s) Summary
Exception API and Title Mapping
src/Exception/ApiProblemException.php, src/Exception/DefaultExceptionToApiProblemTransformer.php
New unprocessableEntity() static factory accepts optional title (defaults to "The request body could not be processed"), optional detail, and violations (array or ConstraintViolationListInterface). Title transformer maps HTTP 422 to "Unprocessable Entity".
Command Validation Implementation
src/Controller/CommandController.php
validateCommand() now throws ApiProblemException::unprocessableEntity(...) instead of badRequest(...) when validation violations are detected.
Test Coverage
tests/Unit/Exception/ApiProblemExceptionTest.php, tests/Unit/Controller/CommandControllerTest.php, tests/Functional/ScenarioTest.php
New unit tests for unprocessableEntity() verify violations storage and 422 status; CommandControllerTest refactored to use try/catch and assert HTTP_UNPROCESSABLE_ENTITY; functional tests updated to expect 422 in both creation and update validation error scenarios.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 A hop, a skip, validation's trick,
Now errors land with status quite slick,
No longer four-oh-oh's gray haze,
Four-twenty-two blazes through the malformed maze,
Proper codes make API dreams come true!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: mapping Symfony Validator constraint violations to HTTP 422 status code with RFC 7807 compliance. It aligns perfectly with the core modifications across all changed files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch unprocessable-entity

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@stixx stixx merged commit caf6d8c into main May 8, 2026
3 checks passed
@stixx stixx deleted the unprocessable-entity branch May 8, 2026 15:31
@stixx stixx added the bug Something isn't working label May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant