mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-24 01:38:53 +02:00
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:
@@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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 { } }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,26 +5,25 @@ namespace WireMock.Net.OpenApiParser.Settings
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// A class defining the example values to use for the different types.
|
/// A class defining the example values to use for the different types.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class WireMockOpenApiParserExampleValues
|
public class WireMockOpenApiParserExampleValues : IWireMockOpenApiParserExampleValues
|
||||||
{
|
{
|
||||||
#pragma warning disable 1591
|
/// <inheritdoc />
|
||||||
public bool Boolean { get; set; } = true;
|
public bool Boolean { get; set; } = true;
|
||||||
|
/// <inheritdoc />
|
||||||
public int Integer { get; set; } = 42;
|
public int Integer { get; set; } = 42;
|
||||||
|
/// <inheritdoc />
|
||||||
public float Float { get; set; } = 4.2f;
|
public float Float { get; set; } = 4.2f;
|
||||||
|
/// <inheritdoc />
|
||||||
public double Double { get; set; } = 4.2d;
|
public double Double { get; set; } = 4.2d;
|
||||||
|
/// <inheritdoc />
|
||||||
public Func<DateTime> Date { get; set; } = () => System.DateTime.UtcNow.Date;
|
public Func<DateTime> Date { get; set; } = () => System.DateTime.UtcNow.Date;
|
||||||
|
/// <inheritdoc />
|
||||||
public Func<DateTime> DateTime { get; set; } = () => System.DateTime.UtcNow;
|
public Func<DateTime> DateTime { get; set; } = () => System.DateTime.UtcNow;
|
||||||
|
/// <inheritdoc />
|
||||||
public byte[] Bytes { get; set; } = { 48, 49, 50 };
|
public byte[] Bytes { get; set; } = { 48, 49, 50 };
|
||||||
|
/// <inheritdoc />
|
||||||
public object Object { get; set; } = "example-object";
|
public object Object { get; set; } = "example-object";
|
||||||
|
/// <inheritdoc />
|
||||||
public string String { get; set; } = "example-string";
|
public string String { get; set; } = "example-string";
|
||||||
#pragma warning restore 1591
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,11 @@ namespace WireMock.Net.OpenApiParser.Settings
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The example values to use
|
/// The example values to use
|
||||||
/// </summary>
|
/// </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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -13,6 +13,14 @@ namespace WireMock.Net.OpenApiParser.Utils
|
|||||||
public ExampleValueGenerator(WireMockOpenApiParserSettings settings)
|
public ExampleValueGenerator(WireMockOpenApiParserSettings settings)
|
||||||
{
|
{
|
||||||
_settings = settings ?? throw new ArgumentNullException(nameof(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)
|
public object GetExampleValue(OpenApiSchema schema)
|
||||||
|
|||||||
@@ -25,6 +25,7 @@
|
|||||||
<PackageReference Include="RamlToOpenApiConverter" Version="0.1.1" />
|
<PackageReference Include="RamlToOpenApiConverter" Version="0.1.1" />
|
||||||
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" PrivateAssets="All" />
|
<PackageReference Include="JetBrains.Annotations" Version="2020.1.0" PrivateAssets="All" />
|
||||||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.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" />
|
<PackageReference Include="Stef.Validation" Version="0.0.3" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user