Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 524232e

Browse files
committed
feat: throw 422 status code when validation fails
1 parent b841433 commit 524232e

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

‎src/JsonApiDotNetCore/Controllers/BaseJsonApiController.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public virtual async Task<IActionResult> PostAsync([FromBody] T entity)
156156
return Forbidden();
157157

158158
if (_jsonApiContext.Options.ValidateModelState && !ModelState.IsValid)
159-
return BadRequest(ModelState.ConvertToErrorCollection<T>(_jsonApiContext.ContextGraph));
159+
return UnprocessableEntity(ModelState.ConvertToErrorCollection<T>(_jsonApiContext.ContextGraph));
160160

161161
entity = await _create.CreateAsync(entity);
162162

@@ -169,8 +169,9 @@ public virtual async Task<IActionResult> PatchAsync(TId id, [FromBody] T entity)
169169

170170
if (entity == null)
171171
return UnprocessableEntity();
172+
172173
if (_jsonApiContext.Options.ValidateModelState && !ModelState.IsValid)
173-
return BadRequest(ModelState.ConvertToErrorCollection<T>(_jsonApiContext.ContextGraph));
174+
return UnprocessableEntity(ModelState.ConvertToErrorCollection<T>(_jsonApiContext.ContextGraph));
174175

175176
var updatedEntity = await _update.UpdateAsync(id, entity);
176177

‎test/UnitTests/Controllers/BaseJsonApiController_Tests.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,8 @@ public async Task PatchAsync_ModelStateInvalid_ValidateModelStateEnabled()
200200

201201
// assert
202202
serviceMock.Verify(m => m.UpdateAsync(id, It.IsAny<Resource>()), Times.Never);
203-
Assert.IsType<BadRequestObjectResult>(response);
204-
Assert.IsType<ErrorCollection>(((BadRequestObjectResult) response).Value);
203+
Assert.IsType<UnprocessableEntityObjectResult>(response);
204+
Assert.IsType<ErrorCollection>(((UnprocessableEntityObjectResult) response).Value);
205205
}
206206

207207
[Fact]
@@ -278,8 +278,8 @@ public async Task PostAsync_ModelStateInvalid_ValidateModelStateEnabled()
278278

279279
// assert
280280
serviceMock.Verify(m => m.CreateAsync(It.IsAny<Resource>()), Times.Never);
281-
Assert.IsType<BadRequestObjectResult>(response);
282-
Assert.IsType<ErrorCollection>(((BadRequestObjectResult)response).Value);
281+
Assert.IsType<UnprocessableEntityObjectResult>(response);
282+
Assert.IsType<ErrorCollection>(((UnprocessableEntityObjectResult)response).Value);
283283
}
284284

285285
[Fact]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /