Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support examples random data generation #673

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -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 { } }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public class WireMockOpenApiParserSettings
/// <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;
}
}
8 changes: 8 additions & 0 deletions src/WireMock.Net.OpenApiParser/Utils/ExampleValueGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ internal class ExampleValueGenerator
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down