Skip to content

Commit 1682c61

Browse files
authored
Use latest ProtoBufJsonConverter to support WellKnownTypes (#1161)
* 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
1 parent ac693e0 commit 1682c61

34 files changed

+530
-153
lines changed

src/WireMock.Net.Abstractions/Admin/Mappings/MappingModel.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public class MappingModel
8989

9090
/// <summary>
9191
/// Data Object which can be used when WithTransformer is used.
92-
/// e.g. lookup an path in this object using
92+
/// e.g. lookup a path in this object using
9393
/// <example>
9494
/// lookup data "1"
9595
/// </example>
@@ -105,4 +105,9 @@ public class MappingModel
105105
/// The Grpc ProtoDefinition which is used for this mapping (request and response). [Optional]
106106
/// </summary>
107107
public string? ProtoDefinition { get; set; }
108+
109+
/// <summary>
110+
/// The Grpc ProtoDefinitions which are used for this mapping (request and response). [Optional]
111+
/// </summary>
112+
public string[]? ProtoDefinitions { get; set; }
108113
}

src/WireMock.Net.Abstractions/Admin/Mappings/ResponseModel.cs

+5
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ public class ResponseModel
137137
/// </summary>
138138
public string? ProtoDefinition { get; set; }
139139

140+
/// <summary>
141+
/// Gets or sets the proto definitions.
142+
/// </summary>
143+
public string[]? ProtoDefinitions { get; set; }
144+
140145
/// <summary>
141146
/// Gets or sets the full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".
142147
/// </summary>

src/WireMock.Net.Abstractions/Admin/Settings/SettingsModel.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public class SettingsModel
121121
/// <summary>
122122
/// A list of Grpc ProtoDefinitions which can be used.
123123
/// </summary>
124-
public Dictionary<string, string>? ProtoDefinitions { get; set; }
124+
public Dictionary<string, string[]>? ProtoDefinitions { get; set; }
125125

126126
#if NETSTANDARD1_3_OR_GREATER || NET461
127127
/// <summary>

src/WireMock.Net.Abstractions/Models/IBodyData.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public interface IBodyData
7979
/// <summary>
8080
/// Gets or sets the proto definition.
8181
/// </summary>
82-
public Func<IdOrText>? ProtoDefinition { get; set; }
82+
public Func<IdOrTexts>? ProtoDefinition { get; set; }
8383

8484
/// <summary>
8585
/// Gets or sets the full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".

src/WireMock.Net.Abstractions/Models/IdOrText.cs

-35
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright © WireMock.Net
2+
3+
using System;
4+
using System.Collections.Generic;
5+
6+
namespace WireMock.Models;
7+
8+
/// <summary>
9+
/// A structure defining an (optional) Id and a Text.
10+
/// </summary>
11+
public readonly struct IdOrTexts
12+
{
13+
/// <summary>
14+
/// The Id [optional].
15+
/// </summary>
16+
public string? Id { get; }
17+
18+
/// <summary>
19+
/// The Text.
20+
/// </summary>
21+
public IReadOnlyList<string> Texts { get; }
22+
23+
/// <summary>
24+
/// Create a IdOrText
25+
/// </summary>
26+
/// <param name="id">The Id [optional]</param>
27+
/// <param name="text">The Text.</param>
28+
public IdOrTexts(string? id, string text) : this(id, [text])
29+
{
30+
}
31+
32+
/// <summary>
33+
/// Create a IdOrText
34+
/// </summary>
35+
/// <param name="id">The Id [optional]</param>
36+
/// <param name="texts">The Texts.</param>
37+
public IdOrTexts(string? id, IReadOnlyList<string> texts)
38+
{
39+
Id = id;
40+
Texts = texts;
41+
}
42+
43+
/// <summary>
44+
/// When Id is defined, return process the Id, else process the Texts.
45+
/// </summary>
46+
/// <param name="id">Callback to process the id.</param>
47+
/// <param name="texts">Callback to process the texts.</param>
48+
public void Value(Action<string> id, Action<IReadOnlyList<string>> texts)
49+
{
50+
if (Id != null)
51+
{
52+
id(Id);
53+
}
54+
else
55+
{
56+
texts(Texts);
57+
}
58+
}
59+
}

src/WireMock.Net/Extensions/AnyOfExtensions.cs

+10
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ public static string GetPattern(this AnyOf<string, StringPattern> value)
2222
return value.IsFirst ? value.First : value.Second.Pattern;
2323
}
2424

25+
/// <summary>
26+
/// Gets the patterns.
27+
/// </summary>
28+
/// <param name="values">AnyOf types</param>
29+
/// <returns>string values</returns>
30+
public static string[] GetPatterns(this AnyOf<string, StringPattern>[] values)
31+
{
32+
return values.Select(GetPattern).ToArray();
33+
}
34+
2535
/// <summary>
2636
/// Converts a string-patterns to AnyOf patterns.
2737
/// </summary>

src/WireMock.Net/IMapping.cs

+4-23
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public interface IMapping
141141
/// <summary>
142142
/// The Grpc ProtoDefinition which is used for this mapping (request and response). [Optional]
143143
/// </summary>
144-
IdOrText? ProtoDefinition { get; }
144+
IdOrTexts? ProtoDefinition { get; }
145145

146146
/// <summary>
147147
/// ProvideResponseAsync
@@ -175,26 +175,7 @@ public interface IMapping
175175
/// <summary>
176176
/// Define a Grpc ProtoDefinition which is used for this mapping (request and response).
177177
/// </summary>
178-
/// <param name="protoDefinition">The proto definition as text.</param>
178+
/// <param name="protoDefinition">The proto definitions as id or text.</param>
179179
/// <returns>The <see cref="IMapping"/>.</returns>
180-
IMapping WithProtoDefinition(IdOrText protoDefinition);
181-
}
182-
183-
/*
184-
executionConditionState">State in which the current mapping can occur. [Optional]
185-
nextState">The next state which will occur after the current mapping execution. [Optional]
186-
stateTimes">Only when the current state is executed this number, the next state which will occur. [Optional]
187-
webhooks">The Webhooks. [Optional]
188-
useWebhooksFireAndForget">Use Fire and Forget for the defined webhook(s). [Optional]
189-
timeSettings">The TimeSettings. [Optional]
190-
data">The data object. [Optional]
191-
192-
193-
string? executionConditionState,
194-
string? nextState,
195-
int? stateTimes,
196-
IWebhook[]? webhooks,
197-
bool? useWebhooksFireAndForget,
198-
ITimeSettings? timeSettings,
199-
object? data,
200-
*/
180+
IMapping WithProtoDefinition(IdOrTexts protoDefinition);
181+
}

src/WireMock.Net/Mapping.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public class Mapping : IMapping
8282
public double? Probability { get; private set; }
8383

8484
/// <inheritdoc />
85-
public IdOrText? ProtoDefinition { get; private set; }
85+
public IdOrTexts? ProtoDefinition { get; private set; }
8686

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

191191
/// <inheritdoc />
192-
public IMapping WithProtoDefinition(IdOrText protoDefinition)
192+
public IMapping WithProtoDefinition(IdOrTexts protoDefinition)
193193
{
194194
ProtoDefinition = protoDefinition;
195195
return this;

src/WireMock.Net/Matchers/ProtoBufMatcher.cs

+10-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#if PROTOBUF
44
using System;
5+
using System.Linq;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using ProtoBufJsonConverter;
@@ -25,9 +26,9 @@ public class ProtoBufMatcher : IProtoBufMatcher
2526
public MatchBehaviour MatchBehaviour { get; }
2627

2728
/// <summary>
28-
/// The Func to define The proto definition as text.
29+
/// The Func to define the proto definition as id or texts.
2930
/// </summary>
30-
public Func<IdOrText> ProtoDefinition { get; }
31+
public Func<IdOrTexts> ProtoDefinition { get; }
3132

3233
/// <summary>
3334
/// The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".
@@ -44,12 +45,12 @@ public class ProtoBufMatcher : IProtoBufMatcher
4445
/// <summary>
4546
/// Initializes a new instance of the <see cref="ProtoBufMatcher"/> class.
4647
/// </summary>
47-
/// <param name="protoDefinition">The proto definition.</param>
48+
/// <param name="protoDefinition">The proto definition as id or text.</param>
4849
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
4950
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
5051
/// <param name="matcher">The optional jsonMatcher to use to match the ProtoBuf as (json) object.</param>
5152
public ProtoBufMatcher(
52-
Func<IdOrText> protoDefinition,
53+
Func<IdOrTexts> protoDefinition,
5354
string messageType,
5455
MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch,
5556
IObjectMatcher? matcher = null
@@ -102,7 +103,11 @@ public string GetCSharpCodeArguments()
102103
return null;
103104
}
104105

105-
var request = new ConvertToObjectRequest(ProtoDefinition().Text, MessageType, input);
106+
var protoDefinitions = ProtoDefinition().Texts;
107+
108+
var resolver = new WireMockProtoFileResolver(protoDefinitions);
109+
var request = new ConvertToObjectRequest(protoDefinitions[0], MessageType, input)
110+
.WithProtoFileResolver(resolver);
106111

107112
try
108113
{

src/WireMock.Net/Matchers/Request/RequestMessageProtoBufMatcher.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ public class RequestMessageProtoBufMatcher : IRequestMatcher
1919
/// Initializes a new instance of the <see cref="RequestMessageProtoBufMatcher"/> class.
2020
/// </summary>
2121
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
22-
/// <param name="protoDefinition">The Func to define The proto definition as text.</param>
22+
/// <param name="protoDefinition">The Func to define the proto definitions as id or text.</param>
2323
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
2424
/// <param name="matcher">The optional matcher to use to match the ProtoBuf as (json) object.</param>
25-
public RequestMessageProtoBufMatcher(MatchBehaviour matchBehaviour, Func<IdOrText> protoDefinition, string messageType, IObjectMatcher? matcher = null)
25+
public RequestMessageProtoBufMatcher(MatchBehaviour matchBehaviour, Func<IdOrTexts> protoDefinition, string messageType, IObjectMatcher? matcher = null)
2626
{
2727
#if PROTOBUF
2828
Matcher = new ProtoBufMatcher(protoDefinition, messageType, matchBehaviour, matcher);

src/WireMock.Net/Models/BodyData.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class BodyData : IBodyData
5252

5353
#region ProtoBuf
5454
/// <inheritdoc />
55-
public Func<IdOrText>? ProtoDefinition { get; set; }
55+
public Func<IdOrTexts>? ProtoDefinition { get; set; }
5656

5757
/// <inheritdoc />
5858
public string? ProtoBufMessageType { get; set; }

src/WireMock.Net/Owin/Mappers/OwinResponseMapper.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ private bool IsFault(IResponseMessage responseMessage)
151151

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

158158
case BodyType.Bytes:

src/WireMock.Net/RequestBuilders/IProtoBufRequestBuilder.cs

+24-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright © WireMock.Net
22

3+
using System.Collections.Generic;
34
using WireMock.Matchers;
45

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

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

3132
/// <summary>
32-
/// WithGrpcProto
33+
/// WithBodyAsProtoBuf
34+
/// </summary>
35+
/// <param name="protoDefinitions">The proto definitions as text.</param>
36+
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
37+
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
38+
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
39+
IRequestBuilder WithBodyAsProtoBuf(IReadOnlyList<string> protoDefinitions, string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
40+
41+
/// <summary>
42+
/// WithBodyAsProtoBuf
43+
/// </summary>
44+
/// <param name="protoDefinitions">The proto definitions as text.</param>
45+
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
46+
/// <param name="matcher">The matcher to use to match the ProtoBuf as (json) object.</param>
47+
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
48+
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
49+
IRequestBuilder WithBodyAsProtoBuf(IReadOnlyList<string> protoDefinitions, string messageType, IObjectMatcher matcher, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
50+
51+
/// <summary>
52+
/// WithBodyAsProtoBuf
3353
/// </summary>
3454
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
3555
/// <param name="matchBehaviour">The match behaviour. (default = "AcceptOnMatch")</param>
3656
/// <returns>The <see cref="IRequestBuilder"/>.</returns>
3757
IRequestBuilder WithBodyAsProtoBuf(string messageType, MatchBehaviour matchBehaviour = MatchBehaviour.AcceptOnMatch);
3858

3959
/// <summary>
40-
/// WithGrpcProto
60+
/// WithBodyAsProtoBuf
4161
/// </summary>
4262
/// <param name="messageType">The full type of the protobuf (request/response) message object. Format is "{package-name}.{type-name}".</param>
4363
/// <param name="matcher">The matcher to use to match the ProtoBuf as (json) object.</param>

0 commit comments

Comments
 (0)