// Copyright © WireMock.Net using System; using Microsoft.OpenApi; using RandomDataGenerator.FieldOptions; using RandomDataGenerator.Randomizers; namespace WireMock.Net.OpenApiParser.Settings; /// /// A class defining the random example values to use for the different types. /// public class WireMockOpenApiParserDynamicExampleValues : IWireMockOpenApiParserExampleValues { /// public virtual bool Boolean => RandomizerFactory.GetRandomizer(new FieldOptionsBoolean()).Generate() ?? true; /// public virtual int Integer => RandomizerFactory.GetRandomizer(new FieldOptionsInteger()).Generate() ?? 42; /// public virtual float Float => RandomizerFactory.GetRandomizer(new FieldOptionsFloat()).Generate() ?? 4.2f; /// public virtual decimal Decimal => SafeConvertFloatToDecimal(RandomizerFactory.GetRandomizer(new FieldOptionsFloat()).Generate() ?? 4.2f); /// public virtual Func Date => () => RandomizerFactory.GetRandomizer(new FieldOptionsDateTime()).Generate() ?? System.DateTime.UtcNow.Date; /// public virtual Func DateTime => () => RandomizerFactory.GetRandomizer(new FieldOptionsDateTime()).Generate() ?? System.DateTime.UtcNow; /// public virtual byte[] Bytes => RandomizerFactory.GetRandomizer(new FieldOptionsBytes()).Generate(); /// public virtual object Object => "example-object"; /// public virtual string String => RandomizerFactory.GetRandomizer(new FieldOptionsTextRegex { Pattern = @"^[0-9]{2}[A-Z]{5}[0-9]{2}" }).Generate() ?? "example-string"; /// public virtual IOpenApiSchema? Schema { get; set; } /// /// Safely converts a float to a decimal, ensuring the value stays within the bounds of a decimal. /// /// The float value to convert. /// A decimal value within the valid range of a decimal. private static decimal SafeConvertFloatToDecimal(float value) { return value switch { < (float)decimal.MinValue => decimal.MinValue, > (float)decimal.MaxValue => decimal.MaxValue, _ => (decimal)value }; } }