This commit is contained in:
Stef Heyenrath
2023-05-30 21:41:30 +02:00
parent 35ffbbc7d9
commit ad3ef83c5e
4 changed files with 27 additions and 28 deletions

View File

@@ -23,12 +23,14 @@ internal class OpenApiPathsMapper
private const string HeaderContentType = "Content-Type"; private const string HeaderContentType = "Content-Type";
private readonly WireMockOpenApiParserSettings _settings; private readonly WireMockOpenApiParserSettings _settings;
private readonly ExampleValueGenerator _exampleValueGenerator; private readonly IExampleValueGenerator _exampleValueGenerator;
private readonly IExampleValueGenerator _regexExampleValueGenerator;
public OpenApiPathsMapper(WireMockOpenApiParserSettings settings) public OpenApiPathsMapper(WireMockOpenApiParserSettings settings)
{ {
_settings = Guard.NotNull(settings); _settings = Guard.NotNull(settings);
_exampleValueGenerator = new ExampleValueGenerator(settings); _exampleValueGenerator = new ExampleValueGenerator(settings);
_regexExampleValueGenerator = new RegexExampleValueGenerator(settings);
} }
public IReadOnlyList<MappingModel> ToMappingModels(OpenApiPaths? paths, IList<OpenApiServer> servers) public IReadOnlyList<MappingModel> ToMappingModels(OpenApiPaths? paths, IList<OpenApiServer> servers)
@@ -349,7 +351,9 @@ internal class OpenApiPathsMapper
private string GetExampleValueAsStringForSchemaType(OpenApiSchema? schema, ExampleValueType exampleValueType) 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 return value switch
{ {

View File

@@ -8,7 +8,7 @@ using WireMock.Net.OpenApiParser.Types;
namespace WireMock.Net.OpenApiParser.Utils; namespace WireMock.Net.OpenApiParser.Utils;
internal class ExampleValueGenerator internal class ExampleValueGenerator : IExampleValueGenerator
{ {
private readonly IWireMockOpenApiParserExampleValues _exampleValues; private readonly IWireMockOpenApiParserExampleValues _exampleValues;

View File

@@ -0,0 +1,8 @@
using Microsoft.OpenApi.Models;
namespace WireMock.Net.OpenApiParser.Utils;
internal interface IExampleValueGenerator
{
object GetExampleValue(OpenApiSchema? schema);
}

View File

@@ -1,4 +1,3 @@
using System.Linq;
using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Models;
using Stef.Validation; using Stef.Validation;
using WireMock.Net.OpenApiParser.Extensions; using WireMock.Net.OpenApiParser.Extensions;
@@ -7,47 +6,35 @@ using WireMock.Net.OpenApiParser.Types;
namespace WireMock.Net.OpenApiParser.Utils; namespace WireMock.Net.OpenApiParser.Utils;
internal class RegexExampleValueGenerator internal class RegexExampleValueGenerator : IExampleValueGenerator
{ {
public RegexExampleValueGenerator(WireMockOpenApiParserSettings settings) public RegexExampleValueGenerator(WireMockOpenApiParserSettings settings)
{ {
Guard.NotNull(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()) switch (schema?.GetSchemaType())
{ {
case SchemaType.Boolean: case SchemaType.Boolean:
return @"^(true|false)$"; return @"(true|false)";
case SchemaType.Integer: case SchemaType.Integer:
return @"^-?\d+$"; return @"-?\d+$";
case SchemaType.Number: case SchemaType.Number:
return @"^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$"; return @"[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?";
default: default:
switch (schema?.GetSchemaFormat()) return schema?.GetSchemaFormat() switch
{ {
case SchemaFormat.Date: SchemaFormat.Date => @"(\d{4})-([01]\d)-([0-3]\d)",
return @"^(\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]?)",
case SchemaFormat.DateTime: SchemaFormat.Binary => @"[a-zA-Z0-9\+/]*={0,3}",
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 ".*";
}
} }
} }
} }