Skip to content

Commit

Permalink
Use latest ProtoBufJsonConverter to support WellKnownTypes (#1161)
Browse files Browse the repository at this point in the history
* Use latest ProtoBufJsonConverter to support WellKnownTypes

* Fix

* 02

* WireMockServer_WithBodyAsProtoBuf_WithWellKnownTypes

* .

* extra test

* 0.4.0-preview-06

* 7

* <PackageReference Include="ProtoBufJsonConverter" Version="0.4.0-preview-08" />

* Update README.md

* <PackageReference Include="ProtoBufJsonConverter" Version="0.4.0-preview-09" />

* <PackageReference Include="ProtoBufJsonConverter" Version="0.4.0" />

* Update README.md
  • Loading branch information
StefH authored Oct 16, 2024
1 parent ac693e0 commit 1682c61
Show file tree
Hide file tree
Showing 34 changed files with 530 additions and 153 deletions.
7 changes: 6 additions & 1 deletion src/WireMock.Net.Abstractions/Admin/Mappings/MappingModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class MappingModel

/// <summary>
/// Data Object which can be used when WithTransformer is used.
/// e.g. lookup an path in this object using
/// e.g. lookup a path in this object using
/// <example>
/// lookup data "1"
/// </example>
Expand All @@ -105,4 +105,9 @@ public class MappingModel
/// The Grpc ProtoDefinition which is used for this mapping (request and response). [Optional]
/// </summary>
public string? ProtoDefinition { get; set; }

/// <summary>
/// The Grpc ProtoDefinitions which are used for this mapping (request and response). [Optional]
/// </summary>
public string[]? ProtoDefinitions { get; set; }
}
5 changes: 5 additions & 0 deletions src/WireMock.Net.Abstractions/Admin/Mappings/ResponseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public class ResponseModel
/// </summary>
public string? ProtoDefinition { get; set; }

/// <summary>
/// Gets or sets the proto definitions.
/// </summary>
public string[]? ProtoDefinitions { get; set; }

/// <summary>
/// Gets or sets the full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public class SettingsModel
/// <summary>
/// A list of Grpc ProtoDefinitions which can be used.
/// </summary>
public Dictionary<string, string>? ProtoDefinitions { get; set; }
public Dictionary<string, string[]>? ProtoDefinitions { get; set; }

#if NETSTANDARD1_3_OR_GREATER || NET461
/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net.Abstractions/Models/IBodyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public interface IBodyData
/// <summary>
/// Gets or sets the proto definition.
/// </summary>
public Func<IdOrText>? ProtoDefinition { get; set; }
public Func<IdOrTexts>? ProtoDefinition { get; set; }

/// <summary>
/// Gets or sets the full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".
Expand Down
35 changes: 0 additions & 35 deletions src/WireMock.Net.Abstractions/Models/IdOrText.cs

This file was deleted.

59 changes: 59 additions & 0 deletions src/WireMock.Net.Abstractions/Models/IdOrTexts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright © WireMock.Net

using System;
using System.Collections.Generic;

namespace WireMock.Models;

/// <summary>
/// A structure defining an (optional) Id and a Text.
/// </summary>
public readonly struct IdOrTexts
{
/// <summary>
/// The Id [optional].
/// </summary>
public string? Id { get; }

/// <summary>
/// The Text.
/// </summary>
public IReadOnlyList<string> Texts { get; }

/// <summary>
/// Create a IdOrText
/// </summary>
/// <param name="id">The Id [optional]</param>
/// <param name="text">The Text.</param>
public IdOrTexts(string? id, string text) : this(id, [text])
{
}

/// <summary>
/// Create a IdOrText
/// </summary>
/// <param name="id">The Id [optional]</param>
/// <param name="texts">The Texts.</param>
public IdOrTexts(string? id, IReadOnlyList<string> texts)
{
Id = id;
Texts = texts;
}

/// <summary>
/// When Id is defined, return process the Id, else process the Texts.
/// </summary>
/// <param name="id">Callback to process the id.</param>
/// <param name="texts">Callback to process the texts.</param>
public void Value(Action<string> id, Action<IReadOnlyList<string>> texts)
{
if (Id != null)
{
id(Id);
}
else
{
texts(Texts);
}
}
}
10 changes: 10 additions & 0 deletions src/WireMock.Net/Extensions/AnyOfExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public static string GetPattern(this AnyOf<string, StringPattern> value)
return value.IsFirst ? value.First : value.Second.Pattern;
}

/// <summary>
/// Gets the patterns.
/// </summary>
/// <param name="values">AnyOf types</param>
/// <returns>string values</returns>
public static string[] GetPatterns(this AnyOf<string, StringPattern>[] values)
{
return values.Select(GetPattern).ToArray();
}

/// <summary>
/// Converts a string-patterns to AnyOf patterns.
/// </summary>
Expand Down
27 changes: 4 additions & 23 deletions src/WireMock.Net/IMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public interface IMapping
/// <summary>
/// The Grpc ProtoDefinition which is used for this mapping (request and response). [Optional]
/// </summary>
IdOrText? ProtoDefinition { get; }
IdOrTexts? ProtoDefinition { get; }

/// <summary>
/// ProvideResponseAsync
Expand Down Expand Up @@ -175,26 +175,7 @@ public interface IMapping
/// <summary>
/// Define a Grpc ProtoDefinition which is used for this mapping (request and response).
/// </summary>
/// <param name="protoDefinition">The proto definition as text.</param>
/// <param name="protoDefinition">The proto definitions as id or text.</param>
/// <returns>The <see cref="IMapping"/>.</returns>
IMapping WithProtoDefinition(IdOrText protoDefinition);
}

/*
executionConditionState">State in which the current mapping can occur. [Optional]
nextState">The next state which will occur after the current mapping execution. [Optional]
stateTimes">Only when the current state is executed this number, the next state which will occur. [Optional]
webhooks">The Webhooks. [Optional]
useWebhooksFireAndForget">Use Fire and Forget for the defined webhook(s). [Optional]
timeSettings">The TimeSettings. [Optional]
data">The data object. [Optional]
string? executionConditionState,
string? nextState,
int? stateTimes,
IWebhook[]? webhooks,
bool? useWebhooksFireAndForget,
ITimeSettings? timeSettings,
object? data,
*/
IMapping WithProtoDefinition(IdOrTexts protoDefinition);
}
4 changes: 2 additions & 2 deletions src/WireMock.Net/Mapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public class Mapping : IMapping
public double? Probability { get; private set; }

/// <inheritdoc />
public IdOrText? ProtoDefinition { get; private set; }
public IdOrTexts? ProtoDefinition { get; private set; }

/// <summary>
/// Initializes a new instance of the <see cref="Mapping"/> class.
Expand Down Expand Up @@ -189,7 +189,7 @@ public IMapping WithScenario(string scenario)
}

/// <inheritdoc />
public IMapping WithProtoDefinition(IdOrText protoDefinition)
public IMapping WithProtoDefinition(IdOrTexts protoDefinition)
{
ProtoDefinition = protoDefinition;
return this;
Expand Down
15 changes: 10 additions & 5 deletions src/WireMock.Net/Matchers/ProtoBufMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#if PROTOBUF
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ProtoBufJsonConverter;
Expand All @@ -25,9 +26,9 @@ public class ProtoBufMatcher : IProtoBufMatcher
public MatchBehaviour MatchBehaviour { get; }

/// <summary>
/// The Func to define The proto definition as text.
/// The Func to define the proto definition as id or texts.
/// </summary>
public Func<IdOrText> ProtoDefinition { get; }
public Func<IdOrTexts> ProtoDefinition { get; }

/// <summary>
/// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".
Expand All @@ -44,12 +45,12 @@ public class ProtoBufMatcher : IProtoBufMatcher
/// <summary>
/// Initializes a new instance of the <see cref="ProtoBufMatcher"/> class.
/// </summary>
/// <param name="protoDefinition">The proto definition.</param>
/// <param name="protoDefinition">The proto definition as id or text.</param>
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
/// <param name="matcher">The optional jsonMatcher to use to match the ProtoBuf as (json) object.</param>
public ProtoBufMatcher(
Func<IdOrText> protoDefinition,
Func<IdOrTexts> protoDefinition,
string messageType,
MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch,
IObjectMatcher? matcher = null
Expand Down Expand Up @@ -102,7 +103,11 @@ public string GetCSharpCodeArguments()
return null;
}

var request = new ConvertToObjectRequest(ProtoDefinition().Text, MessageType, input);
var protoDefinitions = ProtoDefinition().Texts;

var resolver = new WireMockProtoFileResolver(protoDefinitions);
var request = new ConvertToObjectRequest(protoDefinitions[0], MessageType, input)
.WithProtoFileResolver(resolver);

try
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class RequestMessageProtoBufMatcher : IRequestMatcher
/// Initializes a new instance of the <see cref="RequestMessageProtoBufMatcher"/> class.
/// </summary>
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
/// <param name="protoDefinition">The Func to define The proto definition as text.</param>
/// <param name="protoDefinition">The Func to define the proto definitions as id or text.</param>
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
/// <param name="matcher">The optional matcher to use to match the ProtoBuf as (json) object.</param>
public RequestMessageProtoBufMatcher(MatchBehaviour matchBehaviour, Func<IdOrText> protoDefinition, string messageType, IObjectMatcher? matcher = null)
public RequestMessageProtoBufMatcher(MatchBehaviour matchBehaviour, Func<IdOrTexts> protoDefinition, string messageType, IObjectMatcher? matcher = null)
{
#if PROTOBUF
Matcher = new ProtoBufMatcher(protoDefinition, messageType, matchBehaviour, matcher);
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net/Models/BodyData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class BodyData : IBodyData

#region ProtoBuf
/// <inheritdoc />
public Func<IdOrText>? ProtoDefinition { get; set; }
public Func<IdOrTexts>? ProtoDefinition { get; set; }

/// <inheritdoc />
public string? ProtoBufMessageType { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,8 @@ private bool IsFault(IResponseMessage responseMessage)

#if PROTOBUF
case BodyType.ProtoBuf:
var protoDefinition = bodyData.ProtoDefinition?.Invoke().Text;
return await ProtoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinition, bodyData.ProtoBufMessageType, bodyData.BodyAsJson).ConfigureAwait(false);
var protoDefinitions = bodyData.ProtoDefinition?.Invoke().Texts;
return await ProtoBufUtils.GetProtoBufMessageWithHeaderAsync(protoDefinitions, responseMessage.BodyData.ProtoBufMessageType, responseMessage.BodyData.BodyAsJson).ConfigureAwait(false);
#endif

case BodyType.Bytes:
Expand Down
28 changes: 24 additions & 4 deletions src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright © WireMock.Net

using System.Collections.Generic;
using WireMock.Matchers;

namespace WireMock.RequestBuilders;
Expand All @@ -10,7 +11,7 @@ namespace WireMock.RequestBuilders;
public interface IProtoBufRequestBuilder : IGraphQLRequestBuilder
{
/// <summary>
/// WithGrpcProto
/// WithBodyAsProtoBuf
/// </summary>
/// <param name="protoDefinition">The proto definition as text.</param>
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
Expand All @@ -19,7 +20,7 @@ public interface IProtoBufRequestBuilder : IGraphQLRequestBuilder
IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);

/// <summary>
/// WithGrpcProto
/// WithBodyAsProtoBuf
/// </summary>
/// <param name="protoDefinition">The proto definition as text.</param>
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
Expand All @@ -29,15 +30,34 @@ public interface IProtoBufRequestBuilder : IGraphQLRequestBuilder
IRequestBuilder WithBodyAsProtoBuf(string protoDefinition, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);

/// <summary>
/// WithGrpcProto
/// WithBodyAsProtoBuf
/// </summary>
/// <param name="protoDefinitions">The proto definitions as text.</param>
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBodyAsProtoBuf(IReadOnlyList<string> protoDefinitions, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);

/// <summary>
/// WithBodyAsProtoBuf
/// </summary>
/// <param name="protoDefinitions">The proto definitions as text.</param>
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
/// <param name="matcher">The matcher to use to match the ProtoBuf as (json) object.</param>
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBodyAsProtoBuf(IReadOnlyList<string> protoDefinitions, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);

/// <summary>
/// WithBodyAsProtoBuf
/// </summary>
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
IRequestBuilder WithBodyAsProtoBuf(string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);

/// <summary>
/// WithGrpcProto
/// WithBodyAsProtoBuf
/// </summary>
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
/// <param name="matcher">The matcher to use to match the ProtoBuf as (json) object.</param>
Expand Down
Loading

0 comments on commit 1682c61

Please sign in to comment.