Skip to content

Commit

Permalink
Update dependencies and fix what broke
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriyDurov committed Feb 27, 2024
1 parent ef77682 commit d5383ed
Show file tree
Hide file tree
Showing 43 changed files with 359 additions and 303 deletions.
2 changes: 1 addition & 1 deletion sample/OCPI.Net.Sample/OCPI.Net.Sample.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>OCPI.Sample</RootNamespace>
Expand Down
2 changes: 1 addition & 1 deletion src/OCPI.Net.Contracts/OCPI.Net.Contracts.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>OCPI.Contracts</RootNamespace>
Expand Down
4 changes: 2 additions & 2 deletions src/OCPI.Net.Controllers/Models/OcpiControllerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void OcpiValidate<T>(T value, OcpiVersion forOcpiVersion)
var validationContext = GetRequiredService<OcpiValidationContext>();
validationContext.OcpiVersion = forOcpiVersion;

var validator = GetRequiredService<OcpiValidator<T>>();
var validator = GetRequiredService<IOcpiValidator<T>>();
var validationResult = validator.Validate(value);

if (!validationResult.IsValid)
Expand All @@ -49,7 +49,7 @@ public void OcpiValidate<T>(T value, OcpiVersion forOcpiVersion)
public void OcpiValidate<T>(T value)
{
if (value is null) return;
var validator = GetRequiredService<OcpiValidator<T>>();
var validator = GetRequiredService<IOcpiValidator<T>>();
var validationResult = validator.Validate(value);

if (!validationResult.IsValid)
Expand Down
2 changes: 1 addition & 1 deletion src/OCPI.Net.Controllers/OCPI.Net.Controllers.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>OCPI</RootNamespace>
Expand Down
25 changes: 8 additions & 17 deletions src/OCPI.Net.Controllers/Services/PageResponseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,19 @@

namespace OCPI.Services;

internal class PageResponseService
internal class PageResponseService(IHttpContextAccessor httpContextAccessor, OcpiOptions options)
{
private readonly HttpContext _httpContext;
private readonly OcpiOptions _options;
private readonly IOcpiVersionService _versionService;

public PageResponseService(IHttpContextAccessor httpContextAccessor, OcpiOptions options, IOcpiVersionService versionService)
{
_httpContext = httpContextAccessor.HttpContext!;
_options = options;
_versionService = versionService;
}
private readonly HttpContext _httpContext = httpContextAccessor.HttpContext!;

public void ConfigureResponse(PageResult pageResult)
{
if (_httpContext is null) return;

_httpContext.Response.Headers.Add("X-Total-Count", pageResult.Total.ToString());
_httpContext.Response.Headers.Append("X-Total-Count", pageResult.Total.ToString());

var maxLimit = _httpContext.Items["OcpiRequestMaxLimitValue"];
if (maxLimit is null) throw new Exception("Unable to find MaxLimit value for this request. Make sure SetMaxLimit is used in a controller method.");
_httpContext.Response.Headers.Add("X-Limit", maxLimit.ToString());
var maxLimit = _httpContext.Items["OcpiRequestMaxLimitValue"]
?? throw new Exception("Unable to find MaxLimit value for this request. Make sure SetMaxLimit is used in a controller method.");
_httpContext.Response.Headers.Append("X-Limit", maxLimit.ToString());

AddNextPageLink(pageResult);
}
Expand All @@ -45,9 +36,9 @@ private void AddNextPageLink(PageResult pageResult)
var nextLimit = entriesLeft > limit ? limit : entriesLeft;
query["limit"] = nextLimit.ToString();

var nextPageLink = $"{_options.BaseServiceUrl}{_httpContext.Request.Path}?{query.ToString()}";
var nextPageLink = $"{options.BaseServiceUrl}{_httpContext.Request.Path}?{query}";
var linkHeader = $"<{nextPageLink}>; rel=\"next\"";

_httpContext.Response.Headers.Add("Link", linkHeader);
_httpContext.Response.Headers.Append("Link", linkHeader);
}
}
4 changes: 2 additions & 2 deletions src/OCPI.Net.Core/OCPI.Net.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>OCPI</RootNamespace>
Expand All @@ -21,7 +21,7 @@

<ItemGroup>
<PackageReference Include="BitzArt.EnumToMemberValue" Version="1.0.0" />
<PackageReference Include="BitzArt.Pagination" Version="1.13.0" />
<PackageReference Include="BitzArt.Pagination" Version="1.14.0" />
</ItemGroup>

<ItemGroup>
Expand Down
29 changes: 11 additions & 18 deletions src/OCPI.Net.Exceptions/Models/Base/OcpiExceptionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@

namespace OCPI.Exceptions;

public abstract class OcpiExceptionBase : ApiExceptionBase
/// <summary>
/// Creates a new <see cref="OcpiExceptionBase"/>
/// </summary>
/// <param name="message">Message to be displayed</param>
/// <param name="ocpiStatusCode">OCPI Status Code</param>
/// <param name="ocpiLayerHttpStatusCode">HttpStatusCode for responses within OCPI Layer</param>
/// <param name="apiStatusCode">HttpStatusCode for responses outside of OCPI Layer</param>
/// <param name="innerException">Inner exception</param>
public abstract class OcpiExceptionBase(string message, int ocpiStatusCode, int ocpiLayerHttpStatusCode, int apiStatusCode, Exception? innerException = null) : ApiExceptionBase(message, apiStatusCode, innerException: innerException)
{
/// <summary>
/// OCPI Status Code. Used when displaying error responses within OCPI Layer.
/// </summary>
public int OcpiStatus { get; set; }
public int OcpiStatus { get; set; } = ocpiStatusCode;

/// <summary>
/// Used as an HttpStatusCode in responses within OCPI Layer.
/// <br>While <see cref="ApiExceptionBase.StatusCode"/> is used as an HttpStatusCode outside of OCPI layer.</br>
/// </summary>
public int OcpiLayerHttpStatusCode { get; set; }
public int OcpiLayerHttpStatusCode { get; set; } = ocpiLayerHttpStatusCode;

/// <summary>
/// Creates a new <see cref="OcpiExceptionBase"/>
Expand All @@ -36,19 +44,4 @@ public OcpiExceptionBase(string message, OcpiStatusCode ocpiStatusCode, ApiStatu
/// <param name="innerException">Inner exception</param>
public OcpiExceptionBase(string message, int ocpiStatusCode, ApiStatusCode ocpiLayerHttpStatusCode = ApiStatusCode.OK, ApiStatusCode apiStatusCode = ApiStatusCode.Error, Exception? innerException = null)
: this(message, ocpiStatusCode, (int)ocpiLayerHttpStatusCode, (int)apiStatusCode, innerException) { }

/// <summary>
/// Creates a new <see cref="OcpiExceptionBase"/>
/// </summary>
/// <param name="message">Message to be displayed</param>
/// <param name="ocpiStatusCode">OCPI Status Code</param>
/// <param name="ocpiLayerHttpStatusCode">HttpStatusCode for responses within OCPI Layer</param>
/// <param name="apiStatusCode">HttpStatusCode for responses outside of OCPI Layer</param>
/// <param name="innerException">Inner exception</param>
public OcpiExceptionBase(string message, int ocpiStatusCode, int ocpiLayerHttpStatusCode, int apiStatusCode, Exception? innerException = null)
: base(message, apiStatusCode, innerException: innerException)
{
OcpiStatus = ocpiStatusCode;
OcpiLayerHttpStatusCode = ocpiLayerHttpStatusCode;
}
}
15 changes: 6 additions & 9 deletions src/OCPI.Net.Exceptions/Models/ClientApiErrorOcpiException.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
namespace OCPI.Exceptions;

public class ClientApiErrorOcpiException : OcpiExceptionBase
/// <summary>
/// Creates a new <see cref="ClientApiErrorOcpiException"/>
/// </summary>
/// <param name="message">Message to be displayed</param>
/// <param name="innerException">Inner exception</param>
public class ClientApiErrorOcpiException(string message = ClientApiErrorOcpiException.DefaultMessage, Exception? innerException = null) : OcpiExceptionBase(message, OcpiStatusCode.ClientApiError, innerException: innerException)
{
public const string DefaultMessage = "Unable to use the client’s API.";

/// <summary>
/// Creates a new <see cref="ClientApiErrorOcpiException"/>
/// </summary>
/// <param name="message">Message to be displayed</param>
/// <param name="innerException">Inner exception</param>
public ClientApiErrorOcpiException(string message = DefaultMessage, Exception? innerException = null)
: base(message, OcpiStatusCode.ClientApiError, innerException: innerException) { }
}
4 changes: 2 additions & 2 deletions src/OCPI.Net.Exceptions/OCPI.Net.Exceptions.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>OCPI</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
Expand All @@ -17,7 +17,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BitzArt.ApiExceptions.AspNetCore" Version="1.18.2" />
<PackageReference Include="BitzArt.ApiExceptions.AspNetCore" Version="1.20.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using OCPI.Validation;

namespace OCPI;

public static class AddOcpiValidationContextExtension
{
public static IServiceCollection AddOcpiValidationContext(this IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddTransient(x => x.GetRequiredService<IHttpContextAccessor>()!.HttpContext!);

services.AddScoped(x =>
{
var httpContext = x.GetRequiredService<HttpContext>();
var request = httpContext.Request;

return new OcpiValidationContext(request);
});

return services;
}
}
31 changes: 13 additions & 18 deletions src/OCPI.Net.Validation/Extensions/AddOcpiValidationExtension.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
using FluentValidation;
using FluentValidation.AspNetCore;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using OCPI.Contracts;
using OCPI.Validation;
using System.Reflection;
using System.Text.Json.Serialization;

namespace OCPI;

public static class AddOcpiValidationExtension
{
private interface IOcpiValidatorsAssemblyPointer { }

public static WebApplicationBuilder AddOcpiValidation(this WebApplicationBuilder builder, Action<FluentValidationAutoValidationConfiguration>? validationConfigurationExpression = null)
public static IServiceCollection AddOcpiValidation(this IServiceCollection services)
{
builder.Services.AddHttpContextAccessor();
builder.Services.AddTransient(x => x.GetRequiredService<IHttpContextAccessor>()!.HttpContext!);

builder.Services.AddScoped(x =>
{
var httpContext = x.GetRequiredService<HttpContext>();
var request = httpContext.Request;

return new OcpiValidationContext(request);
});
ValidatorOptions.Global.PropertyNameResolver =
(_, member, _) =>
member?.GetCustomAttribute<JsonPropertyNameAttribute>()?.Name ?? member?.Name;

var validatorTypes = typeof(IOcpiValidatorsAssemblyPointer)
.Assembly.DefinedTypes
Expand All @@ -34,16 +26,19 @@ public static WebApplicationBuilder AddOcpiValidation(this WebApplicationBuilder
foreach (var type in validatorTypes)
{
var modelType = type!.BaseType!.GenericTypeArguments.First();
var resultingType = typeof(OcpiValidator<>).MakeGenericType(modelType);
builder.Services.AddScoped(resultingType, x =>
var resultingType = typeof(IOcpiValidator<>).MakeGenericType(modelType);
services.AddScoped(type);
services.AddScoped(resultingType, x =>
{
var instance = x.GetRequiredService(type);
var validationContext = x.GetRequiredService<OcpiValidationContext>();
var instance = Activator.CreateInstance(type, validationContext.ActionType, validationContext.OcpiVersion);
(instance as IActionValidator)!.ActionType = validationContext.ActionType;
(instance as IOcpiValidator)!.OcpiVersion = validationContext.OcpiVersion!.Value;
return instance!;
});
}

return builder;
return services;
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/OCPI.Net.Validation/Interfaces/IOcpiValidator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using FluentValidation;

namespace OCPI.Contracts;

public interface IOcpiValidator
{
public OcpiVersion OcpiVersion { get; internal set; }
}

public interface IOcpiValidator<T> : IOcpiValidator, IActionValidator, IValidator<T> { }
12 changes: 3 additions & 9 deletions src/OCPI.Net.Validation/Models/OcpiValidationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@

namespace OCPI.Validation;

public class OcpiValidationContext
public class OcpiValidationContext(ActionType actionType, OcpiVersion? ocpiVersion)
{
public ActionType ActionType { get; set; }
public OcpiVersion? OcpiVersion { get; set; }

public OcpiValidationContext(ActionType actionType, OcpiVersion? ocpiVersion)
{
ActionType = actionType;
OcpiVersion = ocpiVersion;
}
public ActionType ActionType { get; set; } = actionType;
public OcpiVersion? OcpiVersion { get; set; } = ocpiVersion;

public OcpiValidationContext(HttpRequest request)
: this(request.Method.ToActionType(), request.GetCurrentOcpiVersion())
Expand Down
4 changes: 2 additions & 2 deletions src/OCPI.Net.Validation/OCPI.Net.Validation.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<RootNamespace>OCPI.Validation</RootNamespace>
Expand All @@ -16,7 +16,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BitzArt.FluentValidation.Extensions" Version="0.0.3" />
<PackageReference Include="BitzArt.FluentValidation.Extensions" Version="1.1.0" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ namespace OCPI.Contracts;

internal partial class OcpiAdditionalGeolocationValidator : OcpiValidator<OcpiAdditionalGeolocation>
{
public OcpiAdditionalGeolocationValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
public OcpiAdditionalGeolocationValidator(
IOcpiValidator<OcpiDisplayText> displayTextValidator)
{
JsonRuleFor(x => x.Latitude)
RuleFor(x => x.Latitude)
.NotEmpty()
.ValidLatitude();

JsonRuleFor(x => x.Longitude)
RuleFor(x => x.Longitude)
.NotEmpty()
.ValidLongitude();

JsonRuleFor(x => x.Name!)
.SetValidator(new OcpiDisplayTextValidator(actionType, ocpiVersion))
RuleFor(x => x.Name!)
.SetValidator(displayTextValidator)
.When(x => x.Name is not null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ namespace OCPI.Contracts;

internal class OcpiBusinessDetailsValidator : OcpiValidator<OcpiBusinessDetails>
{
public OcpiBusinessDetailsValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
public OcpiBusinessDetailsValidator(
IOcpiValidator<OcpiImage> imageValidator)
{
JsonRuleFor(x => x.Name)
RuleFor(x => x.Name)
.NotEmpty()
.MaximumLength(100);

JsonRuleFor(x => x.Website)
RuleFor(x => x.Website)
.ValidUrl();

JsonRuleFor(x => x.Logo!)
.SetValidator(new OcpiImageValidator(actionType, ocpiVersion));
RuleFor(x => x.Logo!)
.SetValidator(imageValidator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ namespace OCPI.Contracts;

internal class OcpiDisplayTextValidator : OcpiValidator<OcpiDisplayText>
{
public OcpiDisplayTextValidator(ActionType actionType, OcpiVersion ocpiVersion) : base(actionType, ocpiVersion)
public OcpiDisplayTextValidator()
{
JsonRuleFor(x => x.Language)
RuleFor(x => x.Language)
.NotEmpty()
.MaximumLength(2);

JsonRuleFor(x => x.Text)
RuleFor(x => x.Text)
.NotEmpty()
.MaximumLength(512);
}
Expand Down
Loading

0 comments on commit d5383ed

Please sign in to comment.