Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ csharp_style_expression_bodied_local_functions = true:refactoring
dotnet_style_readonly_field = true:suggestion
dotnet_code_quality_unused_parameters = non_public:suggestion

csharp_style_namespace_declarations = file_scoped:warning

# Naming: PascalCase for constants
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = warning
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
Expand Down
125 changes: 62 additions & 63 deletions JsonApiToolkit/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,83 +10,82 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace JsonApiToolkit.Extensions
namespace JsonApiToolkit.Extensions;

/// <summary>
/// Extension methods for registering JsonApiToolkit services.
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// Extension methods for registering JsonApiToolkit services.
/// Registers JsonApiToolkit services with default options.
/// </summary>
public static IServiceCollection AddJsonApiToolkit(this IServiceCollection services) =>
AddJsonApiToolkit(services, _ => { });

/// <summary>
/// Registers JsonApiToolkit services with custom options configuration.
/// </summary>
public static class ServiceCollectionExtensions
/// <param name="services">The service collection.</param>
/// <param name="configure">Action to configure JsonApiOptions.</param>
public static IServiceCollection AddJsonApiToolkit(
this IServiceCollection services,
Action<JsonApiOptions> configure
)
{
/// <summary>
/// Registers JsonApiToolkit services with default options.
/// </summary>
public static IServiceCollection AddJsonApiToolkit(this IServiceCollection services) =>
AddJsonApiToolkit(services, _ => { });
// Register options
services.Configure(configure);
// Configure JSON serialization options
services.Configure<JsonOptions>(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.DefaultIgnoreCondition =
JsonIgnoreCondition.WhenWritingNull;
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});

/// <summary>
/// Registers JsonApiToolkit services with custom options configuration.
/// </summary>
/// <param name="services">The service collection.</param>
/// <param name="configure">Action to configure JsonApiOptions.</param>
public static IServiceCollection AddJsonApiToolkit(
this IServiceCollection services,
Action<JsonApiOptions> configure
)
// Configure MVC formatters and filters
services.Configure<MvcOptions>(options =>
{
// Register options
services.Configure(configure);
// Configure JSON serialization options
services.Configure<JsonOptions>(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
options.JsonSerializerOptions.DefaultIgnoreCondition =
JsonIgnoreCondition.WhenWritingNull;
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
});
SystemTextJsonOutputFormatter? jsonOutputFormatter = options
.OutputFormatters.OfType<SystemTextJsonOutputFormatter>()
.FirstOrDefault();

// Configure MVC formatters and filters
services.Configure<MvcOptions>(options =>
if (
jsonOutputFormatter?.SupportedMediaTypes.Contains("application/vnd.api+json")
== false
)
{
SystemTextJsonOutputFormatter? jsonOutputFormatter = options
.OutputFormatters.OfType<SystemTextJsonOutputFormatter>()
.FirstOrDefault();
jsonOutputFormatter.SupportedMediaTypes.Add("application/vnd.api+json");
}

if (
jsonOutputFormatter?.SupportedMediaTypes.Contains("application/vnd.api+json")
== false
)
{
jsonOutputFormatter.SupportedMediaTypes.Add("application/vnd.api+json");
}
SystemTextJsonInputFormatter? jsonInputFormatter = options
.InputFormatters.OfType<SystemTextJsonInputFormatter>()
.FirstOrDefault();

SystemTextJsonInputFormatter? jsonInputFormatter = options
.InputFormatters.OfType<SystemTextJsonInputFormatter>()
.FirstOrDefault();

if (
jsonInputFormatter?.SupportedMediaTypes.Contains("application/vnd.api+json")
== false
)
{
jsonInputFormatter.SupportedMediaTypes.Add("application/vnd.api+json");
}
if (
jsonInputFormatter?.SupportedMediaTypes.Contains("application/vnd.api+json")
== false
)
{
jsonInputFormatter.SupportedMediaTypes.Add("application/vnd.api+json");
}

options.Filters.AddService<JsonApiContentTypeFilter>();
});
options.Filters.AddService<JsonApiContentTypeFilter>();
});

// Register filters
services.AddScoped<JsonApiExceptionFilter>();
services.AddScoped<JsonApiContentTypeFilter>();
// Register filters
services.AddScoped<JsonApiExceptionFilter>();
services.AddScoped<JsonApiContentTypeFilter>();

// Register query parser service
services.AddScoped<IJsonApiQueryParser, JsonApiQueryParserService>();
// Register query parser service
services.AddScoped<IJsonApiQueryParser, JsonApiQueryParserService>();

// Register include pattern validator for startup validation
services.TryAddEnumerable(
ServiceDescriptor.Transient<IApplicationModelProvider, IncludePatternValidator>()
);
// Register include pattern validator for startup validation
services.TryAddEnumerable(
ServiceDescriptor.Transient<IApplicationModelProvider, IncludePatternValidator>()
);

return services;
}
return services;
}
}
6 changes: 3 additions & 3 deletions JsonApiToolkit/Filters/JsonApiContentTypeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace JsonApiToolkit.Filters;
/// </summary>
public class JsonApiContentTypeFilter : IActionFilter
{
private const string s_jsonApiMediaType = "application/vnd.api+json";
private const string JsonApiMediaType = "application/vnd.api+json";

/// <summary>
/// Called before the action executes.
Expand All @@ -23,11 +23,11 @@ public void OnActionExecuted(ActionExecutedContext context)
if (context.Result is ObjectResult objectResult)
{
objectResult.ContentTypes.Clear();
objectResult.ContentTypes.Add(s_jsonApiMediaType);
objectResult.ContentTypes.Add(JsonApiMediaType);
}
else if (context.Result is StatusCodeResult)
{
context.HttpContext.Response.ContentType = s_jsonApiMediaType;
context.HttpContext.Response.ContentType = JsonApiMediaType;
}
}
}
4 changes: 2 additions & 2 deletions JsonApiToolkit/Parsing/JsonApiQueryParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace JsonApiToolkit.Parsing;
/// </summary>
public static class JsonApiQueryParser
{
private const int MIN_PAGE_SIZE = 1;
private const int MinPageSize = 1;

/// <summary>
/// Minimum length for a valid filter key: "filter[x]" = 9 characters.
Expand Down Expand Up @@ -97,7 +97,7 @@ public static QueryParameters Parse(
hasPageNumber && int.TryParse(pageNumber, out int num) ? Math.Max(1, num) : 1, // Default to page 1 if not specified or invalid
Size =
hasPageSize && int.TryParse(pageSize, out int size)
? Math.Clamp(size, MIN_PAGE_SIZE, options.MaxPageSize)
? Math.Clamp(size, MinPageSize, options.MaxPageSize)
: options.DefaultPageSize, // Use configured defaults
};
}
Expand Down