Skip to content

Commit

Permalink
Enhance ProblemDetails and update project files
Browse files Browse the repository at this point in the history
- Modified `ProblemDetailsActionResult` and `ProblemDetailsResult` to include HTTP method and request path in `Instance` property.
- Updated `Arcturus.Extensions.ResultObjects.AspNetCore.csproj` to generate documentation file and suppress warning 1591.
- Changed access modifiers of methods in `JsonSerializerExtensions` from `public` to `internal`.
- Added detailed XML documentation comments to `ToActionResult` methods in `ResultExtensions`.
- Removed redundant type parameter comment from `WithHelpLink` method in `ResultExtensions`.
- Added `PackageLicenseExpression` property with value `MIT` to `Arcturus.ResultObjects.csproj`.
  • Loading branch information
cloudfy committed Nov 11, 2024
1 parent 8475a54 commit 31bd6bb
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@ public Task ExecuteResultAsync(ActionContext context)
{
HttpStatusCode defaultStatusCode = _result.HttpStatusCode ?? HttpStatusCode.BadRequest;

//var cts = new ProblemDetailsContext()
//{
// HttpContext = context.HttpContext
//};
var factory = context.HttpContext.RequestServices.GetService<ProblemDetailsFactory>();
var problemDetails = factory!.CreateProblemDetails(context.HttpContext, (int)defaultStatusCode);
problemDetails.Type ??= "https://schemas/2022/fault/#" + defaultStatusCode.ToString().ToLower();
problemDetails.Title ??= _result.Fault?.Code;
problemDetails.Detail = _result.Fault?.Message;
problemDetails.Instance = $"{context.HttpContext.Request.Method} {context.HttpContext.Request.Path}";
//if (_properties?.Length > 0)
//{
// prbml.Extensions.Add("fields", _properties.ToKeyValueList(_ => _.Code, _ => _.Message));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
<PackageId>Arcturus.Extensions.ResultObjects.AspNetCore</PackageId>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageTags>result,resultobject,aspnetcore,arcturus</PackageTags>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>

<PropertyGroup>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ namespace Arcturus.Extensions.ResultObjects.AspNetCore.Internals;

internal static class JsonSerializerExtensions
{
public static bool HasKnownPolymorphism(this JsonTypeInfo jsonTypeInfo)
internal static bool HasKnownPolymorphism(this JsonTypeInfo jsonTypeInfo)
=> jsonTypeInfo.Type.IsSealed || jsonTypeInfo.Type.IsValueType || jsonTypeInfo.PolymorphismOptions is not null;

public static bool ShouldUseWith(this JsonTypeInfo jsonTypeInfo, [NotNullWhen(false)] Type? runtimeType)
internal static bool ShouldUseWith(this JsonTypeInfo jsonTypeInfo, [NotNullWhen(false)] Type? runtimeType)
=> runtimeType is null || jsonTypeInfo.Type == runtimeType || jsonTypeInfo.HasKnownPolymorphism();

public static JsonTypeInfo GetReadOnlyTypeInfo(this JsonSerializerOptions options, Type type)
internal static JsonTypeInfo GetReadOnlyTypeInfo(this JsonSerializerOptions options, Type type)
{
options.MakeReadOnly();
return options.GetTypeInfo(type);
}

public static JsonTypeInfo GetRequiredTypeInfo(this JsonSerializerContext context, Type type)
internal static JsonTypeInfo GetRequiredTypeInfo(this JsonSerializerContext context, Type type)
=> context.GetTypeInfo(type) ?? throw new InvalidOperationException($"Unable to obtain the JsonTypeInfo for type '{type.FullName}' from the context '{context.GetType().FullName}'.");
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ public static IActionResult ToActionResult<T>(this Result<T> result)
result.WithHttpStatusCode(_defaultStatusCode));
}
/// <summary>
///
/// Converts a <see cref="Result"/> object to an <see cref="IActionResult"/> object.
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
/// <param name="result">The <see cref="Result"/> object to convert.</param>
/// <returns>An <see cref="IActionResult"/> object representing the result.</returns>
public static IActionResult ToActionResult(this Result result)
{
if (result.IsSuccess && result.HttpStatusCode is null)
Expand All @@ -52,10 +52,10 @@ public static IActionResult ToActionResult(this Result result)

}
/// <summary>
///
/// Converts a Task of <see cref="Result"/> object to an <see cref="IActionResult"/> object.
/// </summary>
/// <param name="result"></param>
/// <returns></returns>
/// <param name="result">The <see cref="Result"/> object to convert.</param>
/// <returns>An <see cref="IActionResult"/> object representing the result.</returns>
public async static Task<IActionResult> ToActionResult(this Task<Result> result)
=> (await result).ToActionResult();
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ public Task ExecuteAsync(HttpContext httpContext)
{
HttpStatusCode defaultStatusCode = _result.HttpStatusCode ?? HttpStatusCode.BadRequest;

//var factory1 = httpContext.RequestServices.GetService<IProblemDetailsService>();
var factory = httpContext.RequestServices.GetService<ProblemDetailsFactory>();
var problemDetails = factory!.CreateProblemDetails(httpContext, (int)defaultStatusCode);

problemDetails.Title ??= _result.Fault?.Code;
problemDetails.Detail = _result.Fault?.Message;
problemDetails.Type ??= "https://schemas/2022/fault/#" + defaultStatusCode.ToString().ToLower();

problemDetails.Instance = $"{httpContext.Request.Method} {httpContext.Request.Path}";
//var cts = new ProblemDetailsContext()
//{
// HttpContext = httpContext
Expand Down
1 change: 1 addition & 0 deletions src/Arcturus.ResultObjects/Arcturus.ResultObjects.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<PackageId>Arcturus.ResultObjects</PackageId>
<PackageTags>result,resultobject,arcturus</PackageTags>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
Expand Down
1 change: 0 additions & 1 deletion src/Arcturus.ResultObjects/ResultExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public static Result<T> WithHelpLink<T>(this Result<T> result, string uri)
/// <summary>
/// Assigns a helplink to the result.
/// </summary>
/// <typeparam name="T">Type of result object.</typeparam>
/// <param name="result">Required.</param>
/// <param name="uri">Url of the help link.</param>
/// <returns><see cref="Result"/></returns>
Expand Down

0 comments on commit 31bd6bb

Please sign in to comment.