Skip to content

Commit 6ade96f

Browse files
committed
back to regular OK,BadREquest plain responses and more using AddJsonOptions/options.JsonSerializerOptions.TypeInfoResolverChain combo
1 parent 01df57f commit 6ade96f

5 files changed

Lines changed: 53 additions & 27 deletions

File tree

src/IdentityManager2/Api/Controllers/MetaController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public async Task<IActionResult> Get()
5555
links["createRole"] = new CreateRoleLink(Url, meta.RoleMetadata);
5656
}
5757

58-
return Json(new MetaResult
58+
return Ok(new MetaResult
5959
{
6060
Data = data,
6161
Links = links
62-
}, MetaResult_Context.Default.Options);
62+
});
6363
}
6464
}
6565
}

src/IdentityManager2/Api/Controllers/RolesController.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace IdentityManager2.Api.Controllers
1616
{
1717
[Route(IdentityManagerConstants.RoleRoutePrefix)]
1818
[Authorize(IdentityManagerConstants.IdMgrAuthPolicy)]
19-
[ResponseCache(NoStore=true, Location=ResponseCacheLocation.None)]
19+
[ResponseCache(NoStore = true, Location = ResponseCacheLocation.None)]
2020
public class RolesController : Controller
2121
{
2222
private readonly IIdentityManagerService service;
@@ -55,7 +55,7 @@ public async Task<IActionResult> GetRolesAsync(string filter = null, int start =
5555
{
5656
try
5757
{
58-
return Json(new RoleQueryResultResource(result.Result, Url, meta.RoleMetadata), RoleQueryResultResource_Context.Default.Options);
58+
return Ok(new RoleQueryResultResource(result.Result, Url, meta.RoleMetadata));
5959
}
6060
catch (Exception exp)
6161
{
@@ -68,7 +68,7 @@ public async Task<IActionResult> GetRolesAsync(string filter = null, int start =
6868

6969
// POST
7070
[HttpPost, Route("", Name = IdentityManagerConstants.RouteNames.CreateRole)]
71-
public async Task<IActionResult> CreateRoleAsync([FromBody]PropertyValue[] properties)
71+
public async Task<IActionResult> CreateRoleAsync([FromBody] PropertyValue[] properties)
7272
{
7373
var meta = await GetMetadataAsync();
7474
if (!meta.RoleMetadata.SupportsCreate)
@@ -95,7 +95,7 @@ public async Task<IActionResult> CreateRoleAsync([FromBody]PropertyValue[] prope
9595
Data = new AnonymousSubject { subject = result.Result.Subject },
9696
Links = new AnonymousDetail { detail = url }
9797
};
98-
return Created(url, JsonSerializer.Serialize(resource, AnonymousCreatedRole_Context.Default.AnonymousCreatedRole));
98+
return Created(url, resource);
9999
}
100100

101101
ModelState.AddModelError("", errors.ToString());
@@ -109,7 +109,7 @@ public async Task<IActionResult> GetRoleAsync(string subject)
109109
{
110110
if (IsNullOrWhiteSpace(subject))
111111
{
112-
ModelState["subject.String"].Errors.Clear();
112+
ModelState["subject.String"]?.Errors.Clear();
113113
ModelState.AddModelError("", Messages.SubjectRequired);
114114
}
115115

@@ -133,9 +133,7 @@ public async Task<IActionResult> GetRoleAsync(string subject)
133133
return NotFound();
134134
}
135135

136-
var response = Json(new RoleDetailResource(result.Result, Url, meta.RoleMetadata), RoleDetailResource_Context.Default.Options);
137-
138-
136+
var response = Ok(new RoleDetailResource(result.Result, Url, meta.RoleMetadata));
139137
return response;
140138
}
141139
return BadRequest(result.ToError());
@@ -169,7 +167,7 @@ public async Task<IActionResult> SetPropertyAsync(string subject, string type)
169167
{
170168
if (IsNullOrWhiteSpace(subject))
171169
{
172-
ModelState["subject.String"].Errors.Clear();
170+
ModelState["subject.String"]?.Errors.Clear();
173171
ModelState.AddModelError("", Messages.SubjectRequired);
174172
}
175173

src/IdentityManager2/Api/Controllers/UsersController.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using IdentityManager2.Resources;
1111
using Microsoft.AspNetCore.Authorization;
1212
using Microsoft.AspNetCore.Mvc;
13-
using Microsoft.AspNetCore.Mvc.ModelBinding;
1413
using static System.String;
1514

1615
namespace IdentityManager2.Api.Controllers
@@ -27,7 +26,7 @@ public UsersController(IIdentityManagerService service)
2726
{
2827
this.service = service ?? throw new ArgumentNullException(nameof(service));
2928
}
30-
29+
3130
public async Task<IdentityManagerMetadata> GetMetadataAsync()
3231
{
3332
if (metadata == null)
@@ -49,7 +48,7 @@ public async Task<IActionResult> GetUsersAsync(string filter = null, int start =
4948
var meta = await GetMetadataAsync();
5049

5150
var resource = new UserQueryResultResource(result.Result, Url, meta.UserMetadata);
52-
return Json(resource, UserQueryResultResource_Context.Default.Options);
51+
return Ok(resource);
5352
}
5453

5554
return BadRequest(result.ToError());
@@ -76,19 +75,19 @@ public async Task<IActionResult> CreateUserAsync([FromBody] PropertyValue[] prop
7675
var result = await service.CreateUserAsync(properties);
7776
if (result.IsSuccess)
7877
{
79-
var url = Url.Link(IdentityManagerConstants.RouteNames.GetUser, new AnonymousSubject {subject = result.Result.Subject});
78+
var url = Url.Link(IdentityManagerConstants.RouteNames.GetUser, new AnonymousSubject { subject = result.Result.Subject });
8079
var resource = new AnonymousCreatedUser
8180
{
82-
Data = new AnonymousSubject {subject = result.Result.Subject},
83-
Links = new AnonymousDetail {detail = url}
81+
Data = new AnonymousSubject { subject = result.Result.Subject },
82+
Links = new AnonymousDetail { detail = url }
8483
};
8584

86-
return Created(url, JsonSerializer.Serialize(resource, AnonymousCreatedUser_Context.Default.AnonymousCreatedUser));
85+
return Created(url, resource);
8786
}
8887

8988
ModelState.AddModelError("errors", result.Errors.Aggregate((workingSentence, next) => workingSentence + " " + next));
90-
if(result.Errors.Count > 0)
91-
return BadRequest(JsonSerializer.Serialize(ModelState, ModelStateDictionary_Context.Default.ModelStateDictionary));
89+
if (result.Errors.Count > 0)
90+
return BadRequest(ModelState);
9291
}
9392

9493
return BadRequest(400);
@@ -99,7 +98,7 @@ public async Task<IActionResult> GetUserAsync(string subject)
9998
{
10099
if (IsNullOrWhiteSpace(subject))
101100
{
102-
ModelState["subject.String"].Errors.Clear();
101+
ModelState["subject.String"]?.Errors.Clear();
103102
ModelState.AddModelError("", Messages.SubjectRequired);
104103
}
105104

@@ -129,7 +128,7 @@ public async Task<IActionResult> GetUserAsync(string subject)
129128
roles = roleResult.Result.Items.ToArray();
130129
}
131130

132-
return Json(new UserDetailResource(result.Result, Url, meta, roles), UserDetailResource_Context.Default.Options);
131+
return Ok(new UserDetailResource(result.Result, Url, meta, roles));
133132
}
134133

135134
return BadRequest(result.ToError());
@@ -146,7 +145,7 @@ public async Task<IActionResult> DeleteUserAsync(string subject)
146145

147146
if (IsNullOrWhiteSpace(subject))
148147
{
149-
ModelState["subject.String"].Errors.Clear();
148+
ModelState["subject.String"]?.Errors.Clear();
150149
ModelState.AddModelError("", Messages.SubjectRequired);
151150
}
152151

@@ -169,7 +168,7 @@ public async Task<IActionResult> SetPropertyAsync(string subject, string type)
169168
{
170169
if (IsNullOrWhiteSpace(subject))
171170
{
172-
ModelState["subject.String"].Errors.Clear();
171+
ModelState["subject.String"]?.Errors.Clear();
173172
ModelState.AddModelError("", Messages.SubjectRequired);
174173
}
175174

@@ -205,7 +204,7 @@ public async Task<IActionResult> AddClaimAsync(string subject, [FromBody] ClaimV
205204

206205
if (IsNullOrWhiteSpace(subject))
207206
{
208-
ModelState["subject.String"].Errors.Clear();
207+
ModelState["subject.String"]?.Errors.Clear();
209208
ModelState.AddModelError("", Messages.SubjectRequired);
210209
}
211210

@@ -225,7 +224,7 @@ public async Task<IActionResult> AddClaimAsync(string subject, [FromBody] ClaimV
225224

226225
ModelState.AddErrors(result);
227226
}
228-
227+
229228
return BadRequest(ModelState.ToError());
230229
}
231230

src/IdentityManager2/Api/Models/JsonSerializers.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,20 @@ public partial class AnonymousCreatedUser_Context : JsonSerializerContext { }
7878

7979
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
8080
[JsonSerializable(typeof(ModelStateDictionary))]
81-
public partial class ModelStateDictionary_Context : JsonSerializerContext { }
81+
public partial class ModelStateDictionary_Context : JsonSerializerContext { }
82+
83+
84+
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
85+
[JsonSerializable(typeof(ErrorModel))]
86+
public partial class ErrorModel_Context : JsonSerializerContext { }
87+
88+
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
89+
[JsonSerializable(typeof(List<string>))]
90+
public partial class ListStringErrors_Context : JsonSerializerContext { }
91+
92+
93+
94+
95+
[JsonSourceGenerationOptions(PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase)]
96+
[JsonSerializable(typeof(Microsoft.AspNetCore.Mvc.SerializableError))]
97+
public partial class SerializableError_Context : JsonSerializerContext { }

src/IdentityManager2/Configuration/DependencyInjection/IdentityManagerServiceCollectionExtensions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
3+
using System.Linq;
34
using System.Threading.Tasks;
45
using IdentityManager2;
56
using IdentityManager2.Configuration;
@@ -23,6 +24,18 @@ public static IIdentityManagerBuilder AddIdentityManager(this IServiceCollection
2324
{
2425
options.JsonSerializerOptions.TypeInfoResolverChain.Add(ArrayPropertyValue_Context.Default);
2526
options.JsonSerializerOptions.TypeInfoResolverChain.Add(ClaimValue_Context.Default);
27+
options.JsonSerializerOptions.TypeInfoResolverChain.Add(UserQueryResultResource_Context.Default);
28+
options.JsonSerializerOptions.TypeInfoResolverChain.Add(ErrorModel_Context.Default);
29+
options.JsonSerializerOptions.TypeInfoResolverChain.Add(MetaResult_Context.Default);
30+
options.JsonSerializerOptions.TypeInfoResolverChain.Add(UserDetailResource_Context.Default);
31+
options.JsonSerializerOptions.TypeInfoResolverChain.Add(ListStringErrors_Context.Default);
32+
options.JsonSerializerOptions.TypeInfoResolverChain.Add(ModelStateDictionary_Context.Default);
33+
options.JsonSerializerOptions.TypeInfoResolverChain.Add(AnonymousCreatedUser_Context.Default);
34+
options.JsonSerializerOptions.TypeInfoResolverChain.Add(MetaResult_Context.Default);
35+
options.JsonSerializerOptions.TypeInfoResolverChain.Add(RoleQueryResultResource_Context.Default);
36+
options.JsonSerializerOptions.TypeInfoResolverChain.Add(AnonymousCreatedRole_Context.Default);
37+
options.JsonSerializerOptions.TypeInfoResolverChain.Add(RoleDetailResource_Context.Default);
38+
options.JsonSerializerOptions.TypeInfoResolverChain.Add(SerializableError_Context.Default);
2639
});
2740

2841
if (!string.IsNullOrEmpty(identityManagerOptions.SecurityConfiguration.AuthenticationScheme))

0 commit comments

Comments
 (0)