Skip to content

Adopt 422 Unprocessable Entity for payload validation errors #319

@nanotaboada

Description

@nanotaboada

Problem

Field validation failures currently return 400 Bad Request, which conflates two distinct error categories:

  • 400 Bad Request — the request is malformed (unparseable JSON, wrong Content-Type)
  • 422 Unprocessable Entity — the request is syntactically valid but fails business/field validation rules

Proposed Solution

Return 422 Unprocessable Entity for field validation failures and reserve 400 Bad Request for genuinely malformed requests.

Replace HttpStatus.BAD_REQUEST with HttpStatus.UNPROCESSABLE_ENTITY in controller branches that handle validation failures (e.g. @Valid constraint violations, squad number mismatch). Spring's default HttpMessageNotReadableException handler (unparseable JSON) should remain 400.

Acceptance Criteria

  • Field validation failures return 422 Unprocessable Entity
  • 400 Bad Request is reserved for malformed requests (unparseable JSON, wrong Content-Type)
  • OpenAPI @ApiResponse annotations updated to declare 422 instead of 400 for validation responses
  • Tests updated to assert the new status code
  • All existing tests continue to pass
  • CHANGELOG.md updated

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestjavaPull requests that update Java codeplanningEnables automatic issue planning with CodeRabbitpriority lowNice-to-have improvement. Can be deferred without blocking other work.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions