Support examples random data generation (#673)

* Add Support Random Generation in Examples, this commit add the bool property DynamicExamples.

* Comments applied

* Remove  '#pragma warning disable 1591'
This commit is contained in:
Daniel L. Romero
2021-10-30 02:13:24 -05:00
committed by GitHub
parent ae2a05e86b
commit 77ee123340
6 changed files with 108 additions and 12 deletions

View File

@@ -0,0 +1,52 @@
using System;
namespace WireMock.Net.OpenApiParser.Settings
{
/// <summary>
/// A interface defining the example values to use for the different types.
/// </summary>
public interface IWireMockOpenApiParserExampleValues
{
/// <summary>
/// An example value for a Boolean.
/// </summary>
bool Boolean { get; set; }
/// <summary>
/// An example value for an Integer.
/// </summary>
int Integer { get; set; }
/// <summary>
/// An example value for a Float.
/// </summary>
float Float { get; set; }
/// <summary>
/// An example value for a Double.
/// </summary>
double Double { get; set; }
/// <summary>
/// An example value for a Date.
/// </summary>
Func<DateTime> Date { get; set; }
/// <summary>
/// An example value for a DateTime.
/// </summary>
Func<DateTime> DateTime { get; set; }
/// <summary>
/// An example value for Bytes.
/// </summary>
byte[] Bytes { get; set; }
/// <summary>
/// An example value for a Object.
/// </summary>
object Object { get; set; }
/// <summary>
/// An example value for a String.
/// </summary>
string String { get; set; }
}
}

View File

@@ -0,0 +1,31 @@
using System;
using RandomDataGenerator.FieldOptions;
using RandomDataGenerator.Randomizers;
namespace WireMock.Net.OpenApiParser.Settings
{
/// <summary>
/// A class defining the random example values to use for the different types.
/// </summary>
public class WireMockOpenApiParserDynamicExampleValues : IWireMockOpenApiParserExampleValues
{
/// <inheritdoc />
public bool Boolean { get { return RandomizerFactory.GetRandomizer(new FieldOptionsBoolean()).Generate() ?? true; } set { } }
/// <inheritdoc />
public int Integer { get { return RandomizerFactory.GetRandomizer(new FieldOptionsInteger()).Generate() ?? 42; } set { } }
/// <inheritdoc />
public float Float { get { return RandomizerFactory.GetRandomizer(new FieldOptionsFloat()).Generate() ?? 4.2f; } set { } }
/// <inheritdoc />
public double Double { get { return RandomizerFactory.GetRandomizer(new FieldOptionsDouble()).Generate() ?? 4.2d; } set { } }
/// <inheritdoc />
public Func<DateTime> Date { get { return () => RandomizerFactory.GetRandomizer(new FieldOptionsDateTime()).Generate() ?? System.DateTime.UtcNow.Date; } set { } }
/// <inheritdoc />
public Func<DateTime> DateTime { get { return () => RandomizerFactory.GetRandomizer(new FieldOptionsDateTime()).Generate() ?? System.DateTime.UtcNow; } set { } }
/// <inheritdoc />
public byte[] Bytes { get { return RandomizerFactory.GetRandomizer(new FieldOptionsBytes()).Generate(); } set { } }
/// <inheritdoc />
public object Object { get; set; } = "example-object";
/// <inheritdoc />
public string String { get { return RandomizerFactory.GetRandomizer(new FieldOptionsTextWords()).Generate() ?? "example-string"; } set { } }
}
}

View File

@@ -5,26 +5,25 @@ namespace WireMock.Net.OpenApiParser.Settings
/// <summary>
/// A class defining the example values to use for the different types.
/// </summary>
public class WireMockOpenApiParserExampleValues
public class WireMockOpenApiParserExampleValues : IWireMockOpenApiParserExampleValues
{
#pragma warning disable 1591
/// <inheritdoc />
public bool Boolean { get; set; } = true;
/// <inheritdoc />
public int Integer { get; set; } = 42;
/// <inheritdoc />
public float Float { get; set; } = 4.2f;
/// <inheritdoc />
public double Double { get; set; } = 4.2d;
/// <inheritdoc />
public Func<DateTime> Date { get; set; } = () => System.DateTime.UtcNow.Date;
/// <inheritdoc />
public Func<DateTime> DateTime { get; set; } = () => System.DateTime.UtcNow;
/// <inheritdoc />
public byte[] Bytes { get; set; } = { 48, 49, 50 };
/// <inheritdoc />
public object Object { get; set; } = "example-object";
/// <inheritdoc />
public string String { get; set; } = "example-string";
#pragma warning restore 1591
}
}

View File

@@ -25,6 +25,11 @@ namespace WireMock.Net.OpenApiParser.Settings
/// <summary>
/// The example values to use
/// </summary>
public WireMockOpenApiParserExampleValues ExampleValues { get; } = new WireMockOpenApiParserExampleValues();
public IWireMockOpenApiParserExampleValues ExampleValues { get; set; } = new WireMockOpenApiParserExampleValues();
/// <summary>
/// Are examples generated dynamically?
/// </summary>
public bool DynamicExamples { get; set; } = false;
}
}

View File

@@ -13,6 +13,14 @@ namespace WireMock.Net.OpenApiParser.Utils
public ExampleValueGenerator(WireMockOpenApiParserSettings settings)
{
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
if (_settings.DynamicExamples)
{
_settings.ExampleValues = new WireMockOpenApiParserDynamicExampleValues();
}
else
{
_settings.ExampleValues = new WireMockOpenApiParserExampleValues();
}
}
public object GetExampleValue(OpenApiSchema schema)

View File

@@ -25,6 +25,7 @@
<PackageReference Include="RamlToOpenApiConverter" Version="0.1.1" />
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" PrivateAssets="All" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
<PackageReference Include="RandomDataGenerator.Net" Version="1.0.13" />
<PackageReference Include="Stef.Validation" Version="0.0.3" />
</ItemGroup>