diff --git a/src/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs b/src/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs index fdb403cf..5af315b6 100644 --- a/src/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs +++ b/src/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs @@ -23,12 +23,14 @@ internal class OpenApiPathsMapper private const string HeaderContentType = "Content-Type"; private readonly WireMockOpenApiParserSettings _settings; - private readonly ExampleValueGenerator _exampleValueGenerator; + private readonly IExampleValueGenerator _exampleValueGenerator; + private readonly IExampleValueGenerator _regexExampleValueGenerator; public OpenApiPathsMapper(WireMockOpenApiParserSettings settings) { _settings = Guard.NotNull(settings); _exampleValueGenerator = new ExampleValueGenerator(settings); + _regexExampleValueGenerator = new RegexExampleValueGenerator(settings); } public IReadOnlyList ToMappingModels(OpenApiPaths? paths, IList servers) @@ -349,7 +351,9 @@ internal class OpenApiPathsMapper private string GetExampleValueAsStringForSchemaType(OpenApiSchema? schema, ExampleValueType exampleValueType) { - var value = _exampleValueGenerator.GetExampleValue(schema); + var value = exampleValueType == ExampleValueType.Regex ? + _regexExampleValueGenerator.GetExampleValue(schema) : + _exampleValueGenerator.GetExampleValue(schema); return value switch { diff --git a/src/WireMock.Net.OpenApiParser/Utils/ExampleValueGenerator.cs b/src/WireMock.Net.OpenApiParser/Utils/ExampleValueGenerator.cs index 9edde43d..504144ac 100644 --- a/src/WireMock.Net.OpenApiParser/Utils/ExampleValueGenerator.cs +++ b/src/WireMock.Net.OpenApiParser/Utils/ExampleValueGenerator.cs @@ -8,7 +8,7 @@ using WireMock.Net.OpenApiParser.Types; namespace WireMock.Net.OpenApiParser.Utils; -internal class ExampleValueGenerator +internal class ExampleValueGenerator : IExampleValueGenerator { private readonly IWireMockOpenApiParserExampleValues _exampleValues; diff --git a/src/WireMock.Net.OpenApiParser/Utils/IExampleValueGenerator.cs b/src/WireMock.Net.OpenApiParser/Utils/IExampleValueGenerator.cs new file mode 100644 index 00000000..64c9cc7f --- /dev/null +++ b/src/WireMock.Net.OpenApiParser/Utils/IExampleValueGenerator.cs @@ -0,0 +1,8 @@ +using Microsoft.OpenApi.Models; + +namespace WireMock.Net.OpenApiParser.Utils; + +internal interface IExampleValueGenerator +{ + object GetExampleValue(OpenApiSchema? schema); +} \ No newline at end of file diff --git a/src/WireMock.Net.OpenApiParser/Utils/RegexExampleValueGenerator.cs b/src/WireMock.Net.OpenApiParser/Utils/RegexExampleValueGenerator.cs index a21b6def..e2a63f52 100644 --- a/src/WireMock.Net.OpenApiParser/Utils/RegexExampleValueGenerator.cs +++ b/src/WireMock.Net.OpenApiParser/Utils/RegexExampleValueGenerator.cs @@ -1,4 +1,3 @@ -using System.Linq; using Microsoft.OpenApi.Models; using Stef.Validation; using WireMock.Net.OpenApiParser.Extensions; @@ -7,47 +6,35 @@ using WireMock.Net.OpenApiParser.Types; namespace WireMock.Net.OpenApiParser.Utils; -internal class RegexExampleValueGenerator +internal class RegexExampleValueGenerator : IExampleValueGenerator { public RegexExampleValueGenerator(WireMockOpenApiParserSettings settings) { Guard.NotNull(settings); } - public string GetExampleValue(OpenApiSchema? schema) + public object GetExampleValue(OpenApiSchema? schema) { - var schemaExample = schema?.Example; - var schemaEnum = schema?.Enum?.FirstOrDefault(); - switch (schema?.GetSchemaType()) { case SchemaType.Boolean: - return @"^(true|false)$"; + return @"(true|false)"; case SchemaType.Integer: - return @"^-?\d+$"; + return @"-?\d+$"; case SchemaType.Number: - return @"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$"; + return @"[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?"; default: - switch (schema?.GetSchemaFormat()) + return schema?.GetSchemaFormat() switch { - case SchemaFormat.Date: - return @"^(\d{4})-([01]\d)-([0-3]\d)$"; - - case SchemaFormat.DateTime: - return @"^(\d{4})-([01]\d)-([0-3]\d)T([0-2]\d):([0-5]\d):([0-5]\d)(\.\d+)?(Z|[+-][0-2]\d:[0-5]\d)$"; - - case SchemaFormat.Byte: - return @"^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"; - - case SchemaFormat.Binary: - return @"^[a-zA-Z0-9\+/]*={0,3}$"; - - default: - return ".*"; - } + SchemaFormat.Date => @"(\d{4})-([01]\d)-([0-3]\d)", + SchemaFormat.DateTime => @"(\d{4})-([01]\d)-([0-3]\d)T([0-2]\d):([0-5]\d):([0-5]\d)(\.\d+)?(Z|[+-][0-2]\d:[0-5]\d)", + SchemaFormat.Byte => @"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)", + SchemaFormat.Binary => @"[a-zA-Z0-9\+/]*={0,3}", + _ => ".*" + }; } } } \ No newline at end of file