Skip to content

Commit

Permalink
Fix google protobuf WellKnownTypes: Empty, Duration and Timestamp (#1231
Browse files Browse the repository at this point in the history
)

* Fix google protobuf WellKnownTypes: Timestamp and Duration

* Fix protobuf Empty

* .

* small refactor

* 006

* fix

* policy

* ---

* <PackageReference Include="ProtoBufJsonConverter" Version="0.7.0" />
  • Loading branch information
StefH authored Jan 9, 2025
1 parent 9c94324 commit 44c1c7a
Show file tree
Hide file tree
Showing 15 changed files with 413 additions and 79 deletions.
33 changes: 16 additions & 17 deletions src/WireMock.Net.Abstractions/Handlers/IFileSystemHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright © WireMock.Net

using JetBrains.Annotations;
using System.Collections.Generic;

namespace WireMock.Handlers;
Expand All @@ -21,91 +20,91 @@ public interface IFileSystemHandler
/// </summary>
/// <param name="path">The path.</param>
/// <returns>true if path refers to an existing directory; false if the directory does not exist or an error occurs when trying to determine if the specified directory exists.</returns>
bool FolderExists([NotNull] string path);
bool FolderExists(string path);

/// <summary>
/// Creates all directories and subdirectories in the specified path unless they already exist.
/// </summary>
/// <param name="path">The path.</param>
void CreateFolder([NotNull] string path);
void CreateFolder(string path);

/// <summary>
/// Returns an enumerable collection of file names in a specified path.
/// </summary>
/// <param name="path">The path.</param>
/// <param name="includeSubdirectories">A value indicating whether subdirectories should also included when enumerating files.</param>
/// <param name="includeSubdirectories">A value indicating whether subdirectories should also be included when enumerating files.</param>
/// <returns>An enumerable collection of the full names (including paths) for the files in the directory (and optionally subdirectories) specified by path.</returns>
IEnumerable<string> EnumerateFiles([NotNull] string path, bool includeSubdirectories);
IEnumerable<string> EnumerateFiles(string path, bool includeSubdirectories);

/// <summary>
/// Read a static mapping file as text.
/// </summary>
/// <param name="path">The path (folder + filename with .json extension).</param>
/// <returns>The file content as text.</returns>
string ReadMappingFile([NotNull] string path);
string ReadMappingFile(string path);

/// <summary>
/// Write the static mapping file.
/// </summary>
/// <param name="path">The path (folder + filename with .json extension).</param>
/// <param name="text">The text.</param>
void WriteMappingFile([NotNull] string path, [NotNull] string text);
void WriteMappingFile(string path, string text);

/// <summary>
/// Read a response body file as byte[].
/// </summary>
/// <param name="path">The path or filename from the file to read.</param>
/// <returns>The file content as bytes.</returns>
byte[] ReadResponseBodyAsFile([NotNull] string path);
byte[] ReadResponseBodyAsFile(string path);

/// <summary>
/// Read a response body file as text.
/// </summary>
/// <param name="path">The path or filename from the file to read.</param>
/// <returns>The file content as text.</returns>
string ReadResponseBodyAsString([NotNull] string path);
string ReadResponseBodyAsString(string path);

/// <summary>
/// Delete a file.
/// </summary>
/// <param name="filename">The filename.</param>
void DeleteFile([NotNull] string filename);
void DeleteFile(string filename);

/// <summary>
/// Determines whether the given path refers to an existing file on disk.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>true if path refers to an existing file; false if the file does not exist.</returns>
bool FileExists([NotNull] string filename);
bool FileExists(string filename);

/// <summary>
/// Write a file.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="bytes">The bytes.</param>
void WriteFile([NotNull] string filename, [NotNull] byte[] bytes);
void WriteFile(string filename, byte[] bytes);

/// <summary>
/// Write a file.
/// </summary>
/// <param name="folder">The folder.</param>
/// <param name="filename">The filename.</param>
/// <param name="bytes">The bytes.</param>
void WriteFile([NotNull] string folder, [NotNull] string filename, [NotNull] byte[] bytes);
void WriteFile(string folder, string filename, byte[] bytes);

/// <summary>
/// Read a file as bytes.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>The file content as bytes.</returns>
byte[] ReadFile([NotNull] string filename);
byte[] ReadFile(string filename);

/// <summary>
/// Read a file as string.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>The file content as a string.</returns>
string ReadFileAsString([NotNull] string filename);
string ReadFileAsString(string filename);

/// <summary>
/// Gets the folder where the unmatched requests should be stored. For local file system, this would be `{CurrentFolder}/requests/unmatched`.
Expand All @@ -114,9 +113,9 @@ public interface IFileSystemHandler
string GetUnmatchedRequestsFolder();

/// <summary>
/// Write a unmatched request to the Unmatched RequestsFolder.
/// Write an unmatched request to the Unmatched RequestsFolder.
/// </summary>
/// <param name="filename">The filename.</param>
/// <param name="text">The text.</param>
void WriteUnmatchedRequest([NotNull] string filename, [NotNull] string text);
void WriteUnmatchedRequest(string filename, string text);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright © WireMock.Net

#pragma warning disable CS1591
using WireMock.Extensions;
using WireMock.Matchers;

// ReSharper disable once CheckNamespace
namespace WireMock.FluentAssertions;

#pragma warning disable CS1591
public partial class WireMockAssertions
{
[CustomAssertion]
Expand Down
10 changes: 4 additions & 6 deletions src/WireMock.Net/Json/JObjectExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright © WireMock.Net

// Copied from https://github.com/Handlebars-Net/Handlebars.Net.Helpers/blob/master/src/Handlebars.Net.Helpers.DynamicLinq
// Copied from https://github.com/Handlebars-Net/Handlebars.Net.Helpers/blob/master/src/Handlebars.Net.Helpers.DynamicLinq which is copied from https://github.com/StefH/JsonConverter

using System;
using System.Collections;
Expand All @@ -14,9 +14,7 @@ namespace WireMock.Json;

internal static class JObjectExtensions
{
private class JTokenResolvers : Dictionary<JTokenType, Func<JToken, DynamicJsonClassOptions?, object?>>
{
}
private class JTokenResolvers : Dictionary<JTokenType, Func<JToken, DynamicJsonClassOptions?, object?>>;

private static readonly JTokenResolvers Resolvers = new()
{
Expand Down Expand Up @@ -180,7 +178,7 @@ private static IEnumerable ConvertJTokenArray(JToken arg, DynamicJsonClassOption
private static IEnumerable ConvertToTypedArray(IEnumerable<object?> src, Type newType)
{
var method = ConvertToTypedArrayGenericMethod.MakeGenericMethod(newType);
return (IEnumerable)method.Invoke(null, new object[] { src })!;
return (IEnumerable)method.Invoke(null, [src])!;
}

private static readonly MethodInfo ConvertToTypedArrayGenericMethod = typeof(JObjectExtensions).GetMethod(nameof(ConvertToTypedArrayGeneric), BindingFlags.NonPublic | BindingFlags.Static)!;
Expand All @@ -193,7 +191,7 @@ private static T[] ConvertToTypedArrayGeneric<T>(IEnumerable<object> src)
public static DynamicClass CreateInstance(IList<DynamicPropertyWithValue> dynamicPropertiesWithValue, bool createParameterCtor = true)
{
var type = DynamicClassFactory.CreateType(dynamicPropertiesWithValue.Cast<DynamicProperty>().ToArray(), createParameterCtor);
var dynamicClass = (DynamicClass)Activator.CreateInstance(type);
var dynamicClass = (DynamicClass)Activator.CreateInstance(type)!;
foreach (var dynamicPropertyWithValue in dynamicPropertiesWithValue.Where(p => p.Value != null))
{
dynamicClass.SetDynamicPropertyValue(dynamicPropertyWithValue.Name, dynamicPropertyWithValue.Value!);
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private bool IsFault(IResponseMessage responseMessage)
#if PROTOBUF
case BodyType.ProtoBuf:
var protoDefinitions = bodyData.ProtoDefinition?.Invoke().Texts;
return await ProtoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinitions, responseMessage.BodyData.ProtoBufMessageType, responseMessage.BodyData.BodyAsJson).ConfigureAwait(false);
return await ProtoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinitions, bodyData.ProtoBufMessageType, bodyData.BodyAsJson).ConfigureAwait(false);
#endif

case BodyType.Bytes:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright © WireMock.Net

using System.Collections.Generic;
using System.Linq;
using WireMock.Matchers;
using WireMock.Matchers.Request;
using WireMock.Models;
Expand Down
10 changes: 6 additions & 4 deletions src/WireMock.Net/ResponseBuilders/Response.WithBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public IResponseBuilder WithBodyAsProtoBuf(
Guard.NotNull(value);

#if !PROTOBUF
throw new System.NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
throw new NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
#else
ResponseMessage.BodyDestination = null;
ResponseMessage.BodyData = new BodyData
Expand All @@ -252,8 +252,9 @@ public IResponseBuilder WithBodyAsProtoBuf(
ProtoDefinition = () => new IdOrTexts(null, protoDefinitions),
ProtoBufMessageType = messageType
};
#endif

return this;
#endif
}

/// <inheritdoc />
Expand All @@ -268,7 +269,7 @@ public IResponseBuilder WithBodyAsProtoBuf(
Guard.NotNull(value);

#if !PROTOBUF
throw new System.NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
throw new NotSupportedException("The WithBodyAsProtoBuf method can not be used for .NETStandard1.3 or .NET Framework 4.6.1 or lower.");
#else
ResponseMessage.BodyDestination = null;
ResponseMessage.BodyData = new BodyData
Expand All @@ -278,7 +279,8 @@ public IResponseBuilder WithBodyAsProtoBuf(
ProtoDefinition = () => Mapping.ProtoDefinition ?? throw new WireMockException("ProtoDefinition cannot be resolved. You probably forgot to call .WithProtoDefinition(...) on the mapping."),
ProtoBufMessageType = messageType
};
#endif

return this;
#endif
}
}
2 changes: 1 addition & 1 deletion src/WireMock.Net/Util/ProtoBufUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal static async Task<byte[]> GetProtoBufMessageWithHeaderAsync(
}

var resolver = new WireMockProtoFileResolver(protoDefinitions);
var request = new ConvertToProtoBufRequest(protoDefinitions[0], messageType, value, true)
var request = new ConvertToProtoBufRequest(protoDefinitions[0], messageType!, value, true)
.WithProtoFileResolver(resolver);

return await SingletonFactory<Converter>
Expand Down
3 changes: 2 additions & 1 deletion src/WireMock.Net/WireMock.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,15 @@
<ItemGroup Condition="'$(TargetFramework)' != 'netstandard1.3' and '$(TargetFramework)' != 'net451' and '$(TargetFramework)' != 'net452' and '$(TargetFramework)' != 'net46' and '$(TargetFramework)' != 'net461'">
<PackageReference Include="GraphQL.NewtonsoftJson" Version="8.2.1" />
<PackageReference Include="MimeKitLite" Version="4.1.0.1" />
<PackageReference Include="ProtoBufJsonConverter" Version="0.5.0" />
<PackageReference Include="ProtoBufJsonConverter" Version="0.7.0" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netcoreapp3.1' ">
<PackageReference Include="Nullable" Version="1.3.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="System.ComponentModel" Version="4.3.0" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright © WireMock.Net

using System;
using System.Diagnostics.CodeAnalysis;
using Xunit;

namespace WireMock.Net.Tests.Facts;

[ExcludeFromCodeCoverage]
public sealed class IgnoreOnContinuousIntegrationFact : FactAttribute
{
private const string SkipReason = "Ignore when run via CI/CD";
Expand Down
2 changes: 2 additions & 0 deletions test/WireMock.Net.Tests/Facts/RunOnDockerPlatformFact.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Copyright © WireMock.Net
#if NET6_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using WireMock.Net.Testcontainers.Utils;
using Xunit;

namespace WireMock.Net.Tests.Facts;

[ExcludeFromCodeCoverage]
public sealed class RunOnDockerPlatformFact : FactAttribute
{
public RunOnDockerPlatformFact(string platform)
Expand Down
Loading

0 comments on commit 44c1c7a

Please sign in to comment.