Skip to content

Commit b3f761d

Browse files
committed
Add acceptance tests for microsoft#960
1 parent 7a6977f commit b3f761d

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<PropertyGroup>
55
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
66
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
7-
<MessagePackVersion>2.5.108</MessagePackVersion>
7+
<MessagePackVersion>2.5.172</MessagePackVersion>
88
<MicroBuildVersion>2.0.162</MicroBuildVersion>
99
<VisualStudioThreadingVersion>17.10.48</VisualStudioThreadingVersion>
1010
</PropertyGroup>
@@ -29,7 +29,7 @@
2929
<PackageVersion Include="System.IO.Pipelines" Version="8.0.0" />
3030
<PackageVersion Include="System.IO.Pipes" Version="4.3.0" />
3131
<PackageVersion Include="System.Net.Http" Version="4.3.4" />
32-
<PackageVersion Include="System.Text.Json" Version="8.0.3" />
32+
<PackageVersion Include="System.Text.Json" Version="8.0.4" />
3333
<PackageVersion Include="System.Threading.Tasks.Dataflow" Version="6.0.0" />
3434
<PackageVersion Include="System.ValueTuple" Version="4.5.0" />
3535
<PackageVersion Include="xunit.combinatorial" Version="1.6.24" />

test/StreamJsonRpc.Tests/JsonRpcMessagePackLengthTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ internal interface IMessagePackServer
2727
IAsyncEnumerable<UnionBaseClass> GetAsyncEnumerableOfUnionType(CancellationToken cancellationToken);
2828

2929
Task<bool> IsExtensionArgNonNull(CustomExtensionType extensionValue);
30+
31+
Task<string?> GetAPropertyFromStruct([MessagePackFormatter(typeof(ProprietaryStructFormatter))] ProprietaryStruct value, CancellationToken cancellationToken);
3032
}
3133

3234
protected override Type FormatterExceptionType => typeof(MessagePackSerializationException);
@@ -384,6 +386,23 @@ public async Task VerboseLoggingDoesNotFailWhenArgsDoNotDeserializePrimitively(b
384386
Assert.True(await clientProxy.IsExtensionArgNonNull(new CustomExtensionType()));
385387
}
386388

389+
[Fact]
390+
public async Task FormatterOnParameter_Interface()
391+
{
392+
IMessagePackServer clientProxy = this.clientRpc.Attach<IMessagePackServer>();
393+
const string expected = "Beehive";
394+
string? actual = await clientProxy.GetAPropertyFromStruct(new ProprietaryStruct { A = expected }, this.TimeoutToken);
395+
Assert.Equal(expected, actual);
396+
}
397+
398+
[Fact]
399+
public async Task FormatterOnParameter_NoInterface()
400+
{
401+
const string expected = "Beehive";
402+
string? actual = await this.clientRpc.InvokeWithCancellationAsync<string?>(nameof(MessagePackServer.GetAPropertyFromStructNoInterface), [expected], this.TimeoutToken);
403+
Assert.Equal(expected, actual);
404+
}
405+
387406
protected override void InitializeFormattersAndHandlers(
388407
Stream serverStream,
389408
Stream clientStream,
@@ -409,6 +428,14 @@ protected override void InitializeFormattersAndHandlers(
409428
: new LengthHeaderMessageHandler(clientStream, clientStream, clientMessageFormatter);
410429
}
411430

431+
/// <summary>
432+
/// A struct that intentionally uses no MessagePack attributes so that a custom formatter will have to be provided.
433+
/// </summary>
434+
internal struct ProprietaryStruct
435+
{
436+
public string? A { get; set; }
437+
}
438+
412439
[MessagePackObject]
413440
[Union(0, typeof(UnionDerivedClass))]
414441
public abstract class UnionBaseClass
@@ -512,6 +539,16 @@ public async IAsyncEnumerable<UnionBaseClass> GetAsyncEnumerableOfUnionType([Enu
512539
}
513540

514541
public Task<bool> IsExtensionArgNonNull(CustomExtensionType extensionValue) => Task.FromResult(extensionValue is not null);
542+
543+
public Task<string?> GetAPropertyFromStruct(ProprietaryStruct value, CancellationToken cancellationToken)
544+
{
545+
return Task.FromResult(value.A);
546+
}
547+
548+
public Task<string?> GetAPropertyFromStructNoInterface([MessagePackFormatter(typeof(ProprietaryStructFormatter))] ProprietaryStruct value, CancellationToken cancellationToken)
549+
{
550+
return Task.FromResult(value.A);
551+
}
515552
}
516553

517554
private class DelayedFlushingHandler : LengthHeaderMessageHandler, IControlledFlushHandler
@@ -532,4 +569,17 @@ protected override async ValueTask FlushAsync(CancellationToken cancellationToke
532569
await base.FlushAsync(cancellationToken);
533570
}
534571
}
572+
573+
private class ProprietaryStructFormatter : IMessagePackFormatter<ProprietaryStruct>
574+
{
575+
public ProprietaryStruct Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
576+
{
577+
return new ProprietaryStruct { A = reader.ReadString() };
578+
}
579+
580+
public void Serialize(ref MessagePackWriter writer, ProprietaryStruct value, MessagePackSerializerOptions options)
581+
{
582+
writer.Write(value.A);
583+
}
584+
}
535585
}

0 commit comments

Comments
 (0)