Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -1640,11 +1640,6 @@
"resolved": "7.20.0",
"contentHash": "nJLDCOv3qemN0XI5kmUN9h38Qw+ZQBoYtWwSGhPkDEiiZSDcAwLMgqbGNKmK3myhVWASCqXtOqrj5jUnGZrH/Q=="
},
"Microsoft.SqlServer.Dacpacs.Master": {
"type": "Transitive",
"resolved": "160.0.0",
"contentHash": "bgiN77Hfpev7+LON0WOZbgz1VvL6abuUkw14ulLwAnAiuYCcav8HCAoGlgSXVhsLd8U5+jSgeX/ZrNBuPF24qA=="
},
"Microsoft.SqlServer.Server": {
"type": "Transitive",
"resolved": "1.0.0",
Expand Down Expand Up @@ -3357,19 +3352,12 @@
"Ark.Reference.Common": "[0.9.1, )"
}
},
"ark.reference.core.database": {
"type": "Project",
"dependencies": {
"Microsoft.SqlServer.Dacpacs.Master": "[160.0.0, )"
}
},
"ark.reference.core.webinterface": {
"type": "Project",
"dependencies": {
"Ark.Reference.Core.Application": "[0.9.1, )",
"Ark.Tools.AspNetCore": "[1.0.0, )",
"Azure.Extensions.AspNetCore.Configuration.Secrets": "[1.3.2, )",
"Microsoft.AspNetCore.Authentication.JwtBearer": "[8.0.11, )",
"Microsoft.Identity.Web": "[3.5.0, )"
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

<ItemGroup>
<PackageReference Include="Azure.Extensions.AspNetCore.Configuration.Secrets" Version="1.3.2" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="8.0.11" />
<PackageReference Include="Microsoft.Identity.Web" Version="3.5.0" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ public override void ConfigureServices(IServiceCollection services)

// add custom model binders to beginning of collection
opt.ModelBinderProviders.Insert(0, new FormDataJsonBinderProvider(opt.InputFormatters));
})
.AddXmlSerializerFormatters();
});

services.Configure<SnapshotCollectorConfiguration>(o =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@
"Microsoft.Extensions.Configuration": "2.1.0"
}
},
"Microsoft.AspNetCore.Authentication.JwtBearer": {
"type": "Direct",
"requested": "[8.0.11, )",
"resolved": "8.0.11",
"contentHash": "9KhRuywosM24BPf1R5erwsvIkpRUu1+btVyOPlM3JgrhFVP4pq5Fuzi3vjP01OHXfbCtNhWa+HGkZeqaWdcO5w==",
"dependencies": {
"Microsoft.IdentityModel.Protocols.OpenIdConnect": "7.1.2"
}
},
"Microsoft.CodeAnalysis.NetAnalyzers": {
"type": "Direct",
"requested": "[8.0.0, )",
Expand Down Expand Up @@ -555,6 +546,14 @@
"Newtonsoft.Json.Bson": "1.0.1"
}
},
"Microsoft.AspNetCore.Authentication.JwtBearer": {
"type": "Transitive",
"resolved": "8.0.11",
"contentHash": "9KhRuywosM24BPf1R5erwsvIkpRUu1+btVyOPlM3JgrhFVP4pq5Fuzi3vjP01OHXfbCtNhWa+HGkZeqaWdcO5w==",
"dependencies": {
"Microsoft.IdentityModel.Protocols.OpenIdConnect": "7.1.2"
}
},
"Microsoft.AspNetCore.Authentication.OpenIdConnect": {
"type": "Transitive",
"resolved": "8.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Copyright (C) 2024 Ark Energy S.r.l. All rights reserved.
// Licensed under the MIT License. See LICENSE file for license information.
using EnsureThat;

using MessagePack;
using Microsoft.AspNetCore.Http;

using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.WebUtilities;

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Ark.Tools.AspNetCore.MessagePackFormatter
Expand All @@ -33,6 +32,7 @@ public LZ4MessagePackInputFormatter(IFormatterResolver? resolver)
_options = MessagePackSerializer.DefaultOptions.WithResolver(resolver);

_options = _options.WithCompression(MessagePackCompression.Lz4Block);
_options = _options.WithSecurity(MessagePackSecurity.UntrustedData);
}

protected override bool CanReadType(Type type)
Expand All @@ -45,18 +45,10 @@ public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputForma
EnsureArg.IsNotNull(context);

var request = context.HttpContext.Request;
var ctk = context.HttpContext.RequestAborted;

if (!request.Body.CanSeek)
{
request.EnableBuffering();

await request.Body.DrainAsync(CancellationToken.None);
request.Body.Seek(0L, SeekOrigin.Begin);
}

var result = await MessagePackSerializer.DeserializeAsync(context.ModelType, request.Body, _options, context.HttpContext.RequestAborted);

return await InputFormatterResult.SuccessAsync(result);
var result = await MessagePackSerializer.DeserializeAsync(context.ModelType, request.Body, _options, ctk).ConfigureAwait(false);
return await InputFormatterResult.SuccessAsync(result).ConfigureAwait(false);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,32 @@ protected override bool CanWriteType(Type? type)

public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
// 'object' want to use anonymous type serialize, etc...
if (context.ObjectType == typeof(object))
if (context.Object == null)
{
if (context.Object == null)
var writer = context.HttpContext.Response.BodyWriter;
if (writer == null)
{
context.HttpContext.Response.Body.WriteByte(MessagePackCode.Nil);
return Task.CompletedTask;
}
else
{
return MessagePackSerializer.SerializeAsync(context.Object.GetType(), context.HttpContext.Response.Body, context.Object, _options, context.HttpContext.RequestAborted);
}
}

var span = writer.GetSpan(1);
span[0] = MessagePackCode.Nil;
writer.Advance(1);
return writer.FlushAsync().AsTask();
}
else
{
return MessagePackSerializer.SerializeAsync(context.ObjectType!, context.HttpContext.Response.Body, context.Object, _options, context.HttpContext.RequestAborted);
var objectType = context.ObjectType == null || context.ObjectType == typeof(object) ? context.Object.GetType() : context.ObjectType;

var writer = context.HttpContext.Response.BodyWriter;
if (writer == null)
{
return MessagePackSerializer.SerializeAsync(objectType, context.HttpContext.Response.Body, context.Object, _options, context.HttpContext.RequestAborted);
}

MessagePackSerializer.Serialize(objectType, writer, context.Object, _options, context.HttpContext.RequestAborted);
return writer.FlushAsync().AsTask();
}
}
}
Expand Down
23 changes: 8 additions & 15 deletions Ark.Tools.AspNetCore.MessagePack/MessagePackInputFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Copyright (C) 2024 Ark Energy S.r.l. All rights reserved.
// Licensed under the MIT License. See LICENSE file for license information.
using EnsureThat;

using MessagePack;
using Microsoft.AspNetCore.Http;

using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.WebUtilities;

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace Ark.Tools.AspNetCore.MessagePackFormatter
Expand All @@ -31,6 +30,8 @@ public MessagePackInputFormatter(IFormatterResolver? resolver)
_options = MessagePackSerializer.DefaultOptions;
else
_options = MessagePackSerializer.DefaultOptions.WithResolver(resolver);

_options = _options.WithSecurity(MessagePackSecurity.UntrustedData);
}

protected override bool CanReadType(Type type)
Expand All @@ -43,18 +44,10 @@ public override async Task<InputFormatterResult> ReadRequestBodyAsync(InputForma
EnsureArg.IsNotNull(context);

var request = context.HttpContext.Request;
var ctk = context.HttpContext.RequestAborted;

if (!request.Body.CanSeek)
{
request.EnableBuffering();

await request.Body.DrainAsync(CancellationToken.None);
request.Body.Seek(0L, SeekOrigin.Begin);
}

var result = MessagePackSerializer.Deserialize(context.ModelType, request.Body, _options);

return await InputFormatterResult.SuccessAsync(result);
var result = await MessagePackSerializer.DeserializeAsync(context.ModelType, request.Body, _options, ctk).ConfigureAwait(false);
return await InputFormatterResult.SuccessAsync(result).ConfigureAwait(false);
}
}
}
29 changes: 19 additions & 10 deletions Ark.Tools.AspNetCore.MessagePack/MessagePackOutputFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,33 @@ protected override bool CanWriteType(Type? type)

public override Task WriteResponseBodyAsync(OutputFormatterWriteContext context)
{
// 'object' want to use anonymous type serialize, etc...
if (context.ObjectType == typeof(object))

if (context.Object == null)
{
if (context.Object == null)
var writer = context.HttpContext.Response.BodyWriter;
if (writer == null)
{
context.HttpContext.Response.Body.WriteByte(MessagePackCode.Nil);
return Task.CompletedTask;
}
else
{
MessagePackSerializer.Serialize(context.Object.GetType(), context.HttpContext.Response.Body, context.Object, _options);
return Task.CompletedTask;
}

var span = writer.GetSpan(1);
span[0] = MessagePackCode.Nil;
writer.Advance(1);
return writer.FlushAsync().AsTask();
}
else
{
MessagePackSerializer.Serialize(context.ObjectType!, context.HttpContext.Response.Body, context.Object, _options);
return Task.CompletedTask;
var objectType = context.ObjectType == null || context.ObjectType == typeof(object) ? context.Object.GetType() : context.ObjectType;

var writer = context.HttpContext.Response.BodyWriter;
if (writer == null)
{
return MessagePackSerializer.SerializeAsync(objectType, context.HttpContext.Response.Body, context.Object, _options, context.HttpContext.RequestAborted);
}

MessagePackSerializer.Serialize(objectType, writer, context.Object, _options, context.HttpContext.RequestAborted);
return writer.FlushAsync().AsTask();
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions Ark.Tools.Http/Ex.MsgPack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ public static partial class Ex
var resp = await response;
if (resp == null) return default;

using (var stream = await resp.ResponseMessage.Content.ReadAsStreamAsync())
return await GetMsgPackAsync<T>(resp, formatterResolver);
}

public static async Task<T?> GetMsgPackAsync<T>(this IFlurlResponse response, IFormatterResolver formatterResolver)
{
using (var stream = await response.ResponseMessage.Content.ReadAsStreamAsync())
{
return await MessagePackSerializer.DeserializeAsync<T>(stream);
return await MessagePackSerializer.DeserializeAsync<T>(stream, MessagePackSerializerOptions.Standard.WithResolver(formatterResolver));
}
}

Expand Down
2 changes: 2 additions & 0 deletions Ark.Tools.Http/Ex.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
using System;
using Ark.Tools.NewtonsoftJson;
using System.Text.Json;
#if !NET5_0_OR_GREATER
using System.Net;
#endif

namespace Ark.Tools.Http
{
Expand Down
29 changes: 24 additions & 5 deletions Samples/TestProject/Initialization/TestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,39 @@
using System;
using System.Net.Http.Headers;
using System.Linq;
using Ark.Tools.Http;
using Ark.Tools.Core.EntityTag;
using Flurl.Http;
using Microsoft.AspNetCore.Mvc;
using FluentAssertions;
using MessagePack;

namespace TestProject
{
[Binding]
public sealed class TestClient : IDisposable
{
internal IFlurlClient _client;

private readonly string _version;
private readonly IFlurlClient _client;
private readonly IFormatterResolver _formatter;
private readonly string _version;

private IFlurlResponse? _backProperty;
private IFlurlResponse _lastResponse {
get => _backProperty ?? throw new InvalidOperationException("Make a request first");
set { _backProperty?.Dispose(); _backProperty = value; }
}

public IFlurlResponse LastResponse => _lastResponse;

public TestClient(FeatureContext fctx, ScenarioContext sctx, IFlurlClient client)
{
_client = client;
_client = client.WithHeader("Accept", "application/x-msgpack;q=1.0, application/json;q=0.9");

_formatter = MessagePack.Resolvers.CompositeResolver.Create(
MessagePack.NodaTime.NodatimeResolver.Instance,
MessagePack.Resolvers.DynamicEnumAsStringResolver.Instance,
MessagePack.Resolvers.StandardResolver.Instance
);

var tags = sctx.ScenarioInfo.Tags.Concat(fctx.FeatureInfo.Tags);

Expand Down Expand Up @@ -128,14 +138,22 @@ public void PatchAsJson<T>(string requestUri, T? body = null) where T : class
_lastResponse = req.PatchJsonAsync(body).GetAwaiter().GetResult();
}

public T ReadAs<T>()
public T ReadAsJson<T>()
{
if (_lastResponse == null)
throw new InvalidOperationException("I suggest to make a request first ...");

return _lastResponse.GetJsonAsync<T>().GetAwaiter().GetResult();
}

public T? ReadAsMsgPack<T>()
{
if (_lastResponse == null)
throw new InvalidOperationException("I suggest to make a request first ...");

return _lastResponse.GetMsgPackAsync<T>(_formatter).GetAwaiter().GetResult();
}

public string ReadAsString()
{
if (_lastResponse == null)
Expand Down Expand Up @@ -179,6 +197,7 @@ public void ThenTheRequestReturns(HttpStatusCode code)
public void Dispose()
{
_lastResponse?.Dispose();
_client?.Dispose();
}
}
}
17 changes: 17 additions & 0 deletions Samples/TestProject/TestProject.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<SpecFlowObsoleteCodeBehindFiles Remove="Tests\SpecFlowTest - Copy.feature.cs" />
</ItemGroup>


<ItemGroup>
<Content Include="appsettings.json">
Expand Down Expand Up @@ -38,5 +42,18 @@
<ProjectReference Include="..\WebApplicationDemo\WebApplicationDemo.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Update="Tests\MsgPack.feature.cs">
<DependentUpon>MsgPack.feature</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
<SpecFlowFeatureFiles Update="Tests\MsgPack.feature">
<Visible>$(UsingMicrosoftNETSdk)</Visible>
<CodeBehindFile>%(RelativeDir)%(Filename).feature$(DefaultLanguageSourceExtension)</CodeBehindFile>
</SpecFlowFeatureFiles>
</ItemGroup>


</Project>
Loading
Loading