diff --git a/examples/WireMock.Net.OpenApiParser.ConsoleApp/Program.cs b/examples/WireMock.Net.OpenApiParser.ConsoleApp/Program.cs index e0e55e1d3..e9b5fcd8f 100644 --- a/examples/WireMock.Net.OpenApiParser.ConsoleApp/Program.cs +++ b/examples/WireMock.Net.OpenApiParser.ConsoleApp/Program.cs @@ -8,11 +8,11 @@ class Program private const string Folder = "OpenApiFiles"; static void Main(string[] args) { - var serverOpenAPIExamples = Run.RunServer(Path.Combine(Folder, "openAPIExamples.yaml"), "http://localhost:9091/"); - var serverPetstore_V2_json = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V2.0.json"), "http://localhost:9092/"); - var serverPetstore_V2_yaml = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V2.0.yaml"), "http://localhost:9093/"); - var serverPetstore_V300_yaml = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V3.0.0.yaml"), "http://localhost:9094/"); - var serverPetstore_V302_json = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V3.0.2.json"), "http://localhost:9095/"); + var serverOpenAPIExamples = Run.RunServer(Path.Combine(Folder, "openAPIExamples.yaml"), "https://localhost:9091/"); + var serverPetstore_V2_json = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V2.0.json"), "https://localhost:9092/"); + var serverPetstore_V2_yaml = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V2.0.yaml"), "https://localhost:9093/"); + var serverPetstore_V300_yaml = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V3.0.0.yaml"), "https://localhost:9094/"); + var serverPetstore_V302_json = Run.RunServer(Path.Combine(Folder, "Swagger_Petstore_V3.0.2.json"), "https://localhost:9095/"); Console.WriteLine("Press any key to stop the servers"); Console.ReadKey(); diff --git a/examples/WireMock.Net.OpenApiParser.ConsoleApp/Run.cs b/examples/WireMock.Net.OpenApiParser.ConsoleApp/Run.cs index f791b8f00..855ddff3e 100644 --- a/examples/WireMock.Net.OpenApiParser.ConsoleApp/Run.cs +++ b/examples/WireMock.Net.OpenApiParser.ConsoleApp/Run.cs @@ -13,7 +13,7 @@ namespace WireMock.Net.OpenApiParser.ConsoleApp { public static class Run { - public static WireMockServer RunServer(string path, string url) + public static WireMockServer RunServer(string path, string url, bool dynamicExamples = true) { var server = WireMockServer.Start(new WireMockServerSettings { @@ -31,7 +31,9 @@ public static WireMockServer RunServer(string path, string url) var settings = new WireMockOpenApiParserSettings { - PathPatternToUse = ExampleValueType.Wildcard + DynamicExamples = dynamicExamples, + PathPatternToUse = ExampleValueType.Wildcard, + HeaderPatternToUse = ExampleValueType.Wildcard }; server.WithMappingFromOpenApiFile(path, settings, out var diag); diff --git a/examples/WireMock.Net.OpenApiParser.ConsoleApp/WireMock.Net.OpenApiParser.ConsoleApp.csproj b/examples/WireMock.Net.OpenApiParser.ConsoleApp/WireMock.Net.OpenApiParser.ConsoleApp.csproj index 4f3c7264b..4e8a2d101 100644 --- a/examples/WireMock.Net.OpenApiParser.ConsoleApp/WireMock.Net.OpenApiParser.ConsoleApp.csproj +++ b/examples/WireMock.Net.OpenApiParser.ConsoleApp/WireMock.Net.OpenApiParser.ConsoleApp.csproj @@ -1,8 +1,8 @@ - + Exe - netcoreapp2.1 + net5.0 diff --git a/src/WireMock.Net.OpenApiParser/Extensions/DictionaryExtensions.cs b/src/WireMock.Net.OpenApiParser/Extensions/DictionaryExtensions.cs new file mode 100644 index 000000000..e9924ed11 --- /dev/null +++ b/src/WireMock.Net.OpenApiParser/Extensions/DictionaryExtensions.cs @@ -0,0 +1,21 @@ +#if NET46 || NETSTANDARD2_0 +using System.Collections.Generic; + +namespace WireMock.Net.OpenApiParser.Extensions +{ + internal static class DictionaryExtensions + { + public static bool TryAdd(this Dictionary dictionary, TKey key, TValue value) + { + if (dictionary is null || dictionary.ContainsKey(key)) + { + return false; + } + + dictionary[key] = value; + + return true; + } + } +} +#endif diff --git a/src/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs b/src/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs index fd332fa94..656e1224b 100644 --- a/src/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs +++ b/src/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs @@ -19,6 +19,8 @@ namespace WireMock.Net.OpenApiParser.Mappers { internal class OpenApiPathsMapper { + private const string HeaderContentType = "Content-Type"; + private readonly WireMockOpenApiParserSettings _settings; private readonly ExampleValueGenerator _exampleValueGenerator; @@ -248,6 +250,7 @@ private object MapPropertyAsJObject(OpenApiSchema openApiSchema, string key) return new JProperty(key, _exampleValueGenerator.GetExampleValue(openApiSchema)); } } + private string MapPathWithParameters(string path, IEnumerable parameters) { if (parameters == null) @@ -258,7 +261,8 @@ private string MapPathWithParameters(string path, IEnumerable string newPath = path; foreach (var parameter in parameters) { - newPath = newPath.Replace($"{{{parameter.Name}}}", GetExampleValue(parameter.Schema, _settings.PathPatternToUse)); + var exampleMatcherModel = GetExampleMatcherModel(parameter.Schema, _settings.PathPatternToUse); + newPath = newPath.Replace($"{{{parameter.Name}}}", exampleMatcherModel.Pattern as string); } return newPath; @@ -305,19 +309,12 @@ private IDictionary MapHeaders(string responseContentType, IDict { var mappedHeaders = headers.ToDictionary( item => item.Key, - item => GetExampleValue(null, _settings.HeaderPatternToUse) as object + item => GetExampleMatcherModel(null, _settings.HeaderPatternToUse).Pattern ); if (!string.IsNullOrEmpty(responseContentType)) { - if (!mappedHeaders.ContainsKey("Content-Type")) - { - mappedHeaders.Add("Content-Type", responseContentType); - } - else - { - mappedHeaders["Content-Type"] = responseContentType; - } + mappedHeaders.TryAdd(HeaderContentType, responseContentType); } return mappedHeaders.Keys.Any() ? mappedHeaders : null; @@ -331,11 +328,7 @@ private IList MapQueryParameters(IEnumerable query Name = qp.Name, Matchers = new[] { - new MatcherModel - { - Name = "ExactMatcher", - Pattern = GetDefaultValueAsStringForSchemaType(qp.Schema) - } + GetExampleMatcherModel(qp.Schema, _settings.QueryParameterPatternToUse) } }) .ToList(); @@ -351,11 +344,7 @@ private IList MapRequestHeaders(IEnumerable heade Name = qp.Name, Matchers = new[] { - new MatcherModel - { - Name = "ExactMatcher", - Pattern = GetDefaultValueAsStringForSchemaType(qp.Schema) - } + GetExampleMatcherModel(qp.Schema, _settings.HeaderPatternToUse) } }) .ToList(); @@ -363,30 +352,26 @@ private IList MapRequestHeaders(IEnumerable heade return list.Any() ? list : null; } - private string GetDefaultValueAsStringForSchemaType(OpenApiSchema schema) + private MatcherModel GetExampleMatcherModel(OpenApiSchema schema, ExampleValueType type) { - var value = _exampleValueGenerator.GetExampleValue(schema); - - switch (value) + return type switch { - case string valueAsString: - return valueAsString; + ExampleValueType.Value => new MatcherModel { Name = "ExactMatcher", Pattern = GetExampleValueAsStringForSchemaType(schema) }, - default: - return value.ToString(); - } + _ => new MatcherModel { Name = "WildcardMatcher", Pattern = "*" } + }; } - private string GetExampleValue(OpenApiSchema schema, ExampleValueType type) + private string GetExampleValueAsStringForSchemaType(OpenApiSchema schema) { - switch (type) + var value = _exampleValueGenerator.GetExampleValue(schema); + + return value switch { - case ExampleValueType.Value: - return GetDefaultValueAsStringForSchemaType(schema); + string valueAsString => valueAsString, - default: - return "*"; - } + _ => value.ToString(), + }; } } } \ No newline at end of file diff --git a/src/WireMock.Net.OpenApiParser/Settings/WireMockOpenApiParserSettings.cs b/src/WireMock.Net.OpenApiParser/Settings/WireMockOpenApiParserSettings.cs index e56f4d20e..4a0ec4395 100644 --- a/src/WireMock.Net.OpenApiParser/Settings/WireMockOpenApiParserSettings.cs +++ b/src/WireMock.Net.OpenApiParser/Settings/WireMockOpenApiParserSettings.cs @@ -1,4 +1,4 @@ -using WireMock.Net.OpenApiParser.Types; +using WireMock.Net.OpenApiParser.Types; namespace WireMock.Net.OpenApiParser.Settings { @@ -22,10 +22,15 @@ public class WireMockOpenApiParserSettings /// public ExampleValueType HeaderPatternToUse { get; set; } = ExampleValueType.Value; + /// + /// The example value type to use when generating a Query Parameter + /// + public ExampleValueType QueryParameterPatternToUse { get; set; } = ExampleValueType.Value; + /// /// The example values to use /// - public IWireMockOpenApiParserExampleValues ExampleValues { get; set; } = new WireMockOpenApiParserExampleValues(); + public IWireMockOpenApiParserExampleValues ExampleValues { get; set; } /// /// Are examples generated dynamically? diff --git a/src/WireMock.Net.OpenApiParser/Types/ExampleValueType.cs b/src/WireMock.Net.OpenApiParser/Types/ExampleValueType.cs index 698a114db..19d631703 100644 --- a/src/WireMock.Net.OpenApiParser/Types/ExampleValueType.cs +++ b/src/WireMock.Net.OpenApiParser/Types/ExampleValueType.cs @@ -1,4 +1,4 @@ -namespace WireMock.Net.OpenApiParser.Types +namespace WireMock.Net.OpenApiParser.Types { /// /// The example value to use @@ -6,7 +6,9 @@ public enum ExampleValueType { /// - /// Use a generated example value based on the SchemaType (default). + /// 1. Use a generated example value based on the SchemaType (default). + /// 2. If there is no example value defined in the schema, + /// then the will be used (custom, fixed or dynamic). /// Value, diff --git a/src/WireMock.Net.OpenApiParser/Utils/ExampleValueGenerator.cs b/src/WireMock.Net.OpenApiParser/Utils/ExampleValueGenerator.cs index 64f8406fc..b9e7f409b 100644 --- a/src/WireMock.Net.OpenApiParser/Utils/ExampleValueGenerator.cs +++ b/src/WireMock.Net.OpenApiParser/Utils/ExampleValueGenerator.cs @@ -17,13 +17,17 @@ public ExampleValueGenerator(WireMockOpenApiParserSettings settings) { _settings = Guard.NotNull(settings, nameof(settings)); - if (_settings.DynamicExamples) + // Check if user provided an own implementation + if (settings.ExampleValues is null) { - _settings.ExampleValues = new WireMockOpenApiParserDynamicExampleValues(); - } - else - { - _settings.ExampleValues = new WireMockOpenApiParserExampleValues(); + if (_settings.DynamicExamples) + { + _settings.ExampleValues = new WireMockOpenApiParserDynamicExampleValues(); + } + else + { + _settings.ExampleValues = new WireMockOpenApiParserExampleValues(); + } } }