Skip to content

Commit 875a990

Browse files
committed
Fixed an issue with serializer not respecting JSON property attributes and auto validation
1 parent cd8c1f4 commit 875a990

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
using System.ComponentModel.DataAnnotations;
22
using Newtonsoft.Json;
3+
using Newtonsoft.Json.Serialization;
34

45
namespace Exceptionless.Web.Models;
56

7+
// NOTE: This will bypass our LowerCaseUnderscorePropertyNamesContractResolver and provide the correct casing.
8+
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
69
public record ExternalAuthInfo
710
{
811
[Required]
9-
[JsonProperty("clientId")]
1012
public required string ClientId { get; init; }
1113

1214
[Required]
13-
[JsonProperty("code")]
1415
public required string Code { get; init; }
1516

1617
[Required]
17-
[JsonProperty("redirectUri")]
1818
public required string RedirectUri { get; init; }
1919

20-
[JsonProperty("inviteToken")]
2120
public string? InviteToken { get; init; }
2221
}

src/Exceptionless.Web/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using Exceptionless.Web.Security;
1111
using Exceptionless.Web.Utility;
1212
using Exceptionless.Web.Utility.Handlers;
13-
using Exceptionless.Web.Utility.Results;
1413
using FluentValidation;
1514
using Foundatio.Extensions.Hosting.Startup;
1615
using Foundatio.Repositories.Exceptions;
@@ -196,6 +195,7 @@ private void CustomizeProblemDetails(ProblemDetailsContext ctx)
196195
if (ctx.ProblemDetails is ValidationProblemDetails validationProblem)
197196
{
198197
// This might be possible to accomplish via serializer.
198+
// NOTE: the key could be wrong for things like ExternalAuthInfo where the keys are camel case.
199199
validationProblem.Errors = validationProblem.Errors
200200
.ToDictionary(
201201
error => error.Key.ToLowerUnderscoredWords(),
@@ -204,7 +204,6 @@ private void CustomizeProblemDetails(ProblemDetailsContext ctx)
204204
}
205205

206206
// errors
207-
208207
// TODO: Check casing of property names of model state validation errors.
209208
}
210209

src/Exceptionless.Web/Utility/AutoValidationActionFilter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionE
4343
foreach (var error in errors)
4444
{
4545
// TODO: Verify nested object keys
46-
var modelStateEntry = context.ModelState[error.Key];
46+
// NOTE: Fallback to finding model state errors where the serializer already changed the key, but differs from ModelState like ExternalAuthInfo (without NamingStrategyType)
47+
var modelStateEntry = context.ModelState[error.Key] ?? context.ModelState[error.Key.ToLowerUnderscoredWords()];
4748
foreach (string errorMessage in error.Value)
4849
{
4950
hasErrors = true;
50-
if (modelStateEntry is null || !modelStateEntry.Errors.Contains(e => String.Equals(e.ErrorMessage, errorMessage)))
51+
if (modelStateEntry is null || !modelStateEntry.Errors.Contains(e => String.Equals(e.ErrorMessage, errorMessage, StringComparison.OrdinalIgnoreCase)))
5152
context.ModelState.AddModelError(error.Key, errorMessage);
5253
}
5354
}

tests/http/auth.http

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Content-Type: application/json
1818
{
1919
"email": "{{email}}",
2020
"password": "{{password}}",
21-
"name":"test"
21+
"name": "test"
2222
}
2323

2424
### Signup with token
@@ -28,5 +28,15 @@ Content-Type: application/json
2828
{
2929
"email": "{{email}}",
3030
"password": "{{password}}",
31-
"name":"test"
31+
"name": "test"
32+
}
33+
34+
### OAuth Login
35+
POST {{apiUrl}}/auth/google
36+
Content-Type: application/json
37+
38+
{
39+
"code": "code",
40+
"clientId": "clientId",
41+
"redirectUri": "http://localhost",
3242
}

0 commit comments

Comments
 (0)