diff --git a/WireMock.Net Solution.sln b/WireMock.Net Solution.sln index 56b43bb5..7965831f 100644 --- a/WireMock.Net Solution.sln +++ b/WireMock.Net Solution.sln @@ -128,8 +128,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WireMock.Net.Middleware.Tes EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.AwesomeAssertions", "src\WireMock.Net.AwesomeAssertions\WireMock.Net.AwesomeAssertions.csproj", "{7753670F-7C7F-44BF-8BC7-08325588E60C}" EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src-preview", "src-preview", "{02EA681E-C7D8-13C7-8484-4AC65E1B71E8}" -EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WireMock.Net.OpenApiParser", "src\WireMock.Net.OpenApiParser\WireMock.Net.OpenApiParser.csproj", "{E5B03EEF-822C-4295-952B-4479AD30082B}" EndProject Global diff --git a/src-old/WireMock.Net.OpenApiParser/Extensions/DictionaryExtensions.cs b/src-old/WireMock.Net.OpenApiParser/Extensions/DictionaryExtensions.cs deleted file mode 100644 index 8034318e..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Extensions/DictionaryExtensions.cs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright © WireMock.Net - -#if NET46 || NETSTANDARD2_0 -using System.Collections.Generic; - -namespace WireMock.Net.OpenApiParser.Extensions; - -internal static class DictionaryExtensions -{ - public static bool TryAdd(this Dictionary? dictionary, TKey key, TValue value) - { - if (dictionary is null || dictionary.ContainsKey(key)) - { - return false; - } - - dictionary[key] = value; - - return true; - } -} -#endif \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Extensions/OpenApiSchemaExtensions.cs b/src-old/WireMock.Net.OpenApiParser/Extensions/OpenApiSchemaExtensions.cs deleted file mode 100644 index efd4bfcd..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Extensions/OpenApiSchemaExtensions.cs +++ /dev/null @@ -1,92 +0,0 @@ -// Copyright © WireMock.Net - -using System.Linq; -using Microsoft.OpenApi.Any; -using Microsoft.OpenApi.Interfaces; -using Microsoft.OpenApi.Models; -using WireMock.Net.OpenApiParser.Types; - -namespace WireMock.Net.OpenApiParser.Extensions; - -internal static class OpenApiSchemaExtensions -{ - /// - /// https://stackoverflow.com/questions/48111459/how-to-define-a-property-that-can-be-string-or-null-in-openapi-swagger - /// - public static bool TryGetXNullable(this OpenApiSchema schema, out bool value) - { - value = false; - - if (schema.Extensions.TryGetValue("x-nullable", out var e) && e is OpenApiBoolean openApiBoolean) - { - value = openApiBoolean.Value; - return true; - } - - return false; - } - - public static SchemaType GetSchemaType(this OpenApiSchema? schema) - { - if (schema == null) - { - return SchemaType.Unknown; - } - - if (schema.Type == null) - { - if (schema.AllOf.Any() || schema.AnyOf.Any()) - { - return SchemaType.Object; - } - } - - return schema.Type switch - { - "object" => SchemaType.Object, - "array" => SchemaType.Array, - "integer" => SchemaType.Integer, - "number" => SchemaType.Number, - "boolean" => SchemaType.Boolean, - "string" => SchemaType.String, - "file" => SchemaType.File, - _ => SchemaType.Unknown - }; - } - - public static SchemaFormat GetSchemaFormat(this OpenApiSchema? schema) - { - switch (schema?.Format) - { - case "float": - return SchemaFormat.Float; - - case "double": - return SchemaFormat.Double; - - case "int32": - return SchemaFormat.Int32; - - case "int64": - return SchemaFormat.Int64; - - case "date": - return SchemaFormat.Date; - - case "date-time": - return SchemaFormat.DateTime; - - case "password": - return SchemaFormat.Password; - - case "byte": - return SchemaFormat.Byte; - - case "binary": - return SchemaFormat.Binary; - - default: - return SchemaFormat.Undefined; - } - } -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Extensions/WireMockServerExtensions.cs b/src-old/WireMock.Net.OpenApiParser/Extensions/WireMockServerExtensions.cs deleted file mode 100644 index 494d4f37..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Extensions/WireMockServerExtensions.cs +++ /dev/null @@ -1,96 +0,0 @@ -// Copyright © WireMock.Net - -using System.IO; -using System.Linq; -using JetBrains.Annotations; -using Microsoft.OpenApi.Models; -using Microsoft.OpenApi.Readers; -using Stef.Validation; -using WireMock.Net.OpenApiParser.Settings; -using WireMock.Server; - -namespace WireMock.Net.OpenApiParser.Extensions; - -/// -/// Some extension methods for . -/// -public static class WireMockServerExtensions -{ - /// - /// Register the mappings via an OpenAPI (swagger) V2 or V3 file. - /// - /// The WireMockServer instance - /// Path containing OpenAPI file to parse and use the mappings. - /// Returns diagnostic object containing errors detected during parsing - [PublicAPI] - public static IWireMockServer WithMappingFromOpenApiFile(this IWireMockServer server, string path, out OpenApiDiagnostic diagnostic) - { - return WithMappingFromOpenApiFile(server, path, new WireMockOpenApiParserSettings(), out diagnostic); - } - - /// - /// Register the mappings via an OpenAPI (swagger) V2 or V3 file. - /// - /// The WireMockServer instance - /// Path containing OpenAPI file to parse and use the mappings. - /// Additional settings - /// Returns diagnostic object containing errors detected during parsing - [PublicAPI] - public static IWireMockServer WithMappingFromOpenApiFile(this IWireMockServer server, string path, WireMockOpenApiParserSettings settings, out OpenApiDiagnostic diagnostic) - { - Guard.NotNull(server); - Guard.NotNullOrEmpty(path); - - var mappings = new WireMockOpenApiParser().FromFile(path, settings, out diagnostic); - - return server.WithMapping(mappings.ToArray()); - } - - /// - /// Register the mappings via an OpenAPI (swagger) V2 or V3 stream. - /// - /// The WireMockServer instance - /// Stream containing OpenAPI description to parse and use the mappings. - /// Returns diagnostic object containing errors detected during parsing - [PublicAPI] - public static IWireMockServer WithMappingFromOpenApiStream(this IWireMockServer server, Stream stream, out OpenApiDiagnostic diagnostic) - { - return WithMappingFromOpenApiStream(server, stream, new WireMockOpenApiParserSettings(), out diagnostic); - } - - /// - /// Register the mappings via an OpenAPI (swagger) V2 or V3 stream. - /// - /// The WireMockServer instance - /// Stream containing OpenAPI description to parse and use the mappings. - /// Additional settings - /// Returns diagnostic object containing errors detected during parsing - [PublicAPI] - public static IWireMockServer WithMappingFromOpenApiStream(this IWireMockServer server, Stream stream, WireMockOpenApiParserSettings settings, out OpenApiDiagnostic diagnostic) - { - Guard.NotNull(server); - Guard.NotNull(stream); - Guard.NotNull(settings); - - var mappings = new WireMockOpenApiParser().FromStream(stream, settings, out diagnostic); - - return server.WithMapping(mappings.ToArray()); - } - - /// - /// Register the mappings via an OpenAPI (swagger) V2 or V3 document. - /// - /// The WireMockServer instance - /// The OpenAPI document to use as mappings. - /// Additional settings [optional]. - [PublicAPI] - public static IWireMockServer WithMappingFromOpenApiDocument(this IWireMockServer server, OpenApiDocument document, WireMockOpenApiParserSettings? settings = null) - { - Guard.NotNull(server); - Guard.NotNull(document); - - var mappings = new WireMockOpenApiParser().FromDocument(document, settings); - - return server.WithMapping(mappings.ToArray()); - } -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/IWireMockOpenApiParser.cs b/src-old/WireMock.Net.OpenApiParser/IWireMockOpenApiParser.cs deleted file mode 100644 index e3b2b728..00000000 --- a/src-old/WireMock.Net.OpenApiParser/IWireMockOpenApiParser.cs +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright © WireMock.Net - -using System.Collections.Generic; -using System.IO; -using Microsoft.OpenApi.Models; -using Microsoft.OpenApi.Readers; -using WireMock.Admin.Mappings; -using WireMock.Net.OpenApiParser.Settings; - -namespace WireMock.Net.OpenApiParser; - -/// -/// Parse a OpenApi/Swagger/V2/V3 or Raml to WireMock MappingModels. -/// -public interface IWireMockOpenApiParser -{ - /// - /// Generate from a file-path. - /// - /// The path to read the OpenApi/Swagger/V2/V3 or Raml file. - /// OpenApiDiagnostic output - /// MappingModel - IReadOnlyList FromFile(string path, out OpenApiDiagnostic diagnostic); - - /// - /// Generate from a file-path. - /// - /// The path to read the OpenApi/Swagger/V2/V3 or Raml file. - /// Additional settings - /// OpenApiDiagnostic output - /// MappingModel - IReadOnlyList FromFile(string path, WireMockOpenApiParserSettings settings, out OpenApiDiagnostic diagnostic); - - /// - /// Generate from an . - /// - /// The source OpenApiDocument - /// Additional settings [optional] - /// MappingModel - IReadOnlyList FromDocument(OpenApiDocument document, WireMockOpenApiParserSettings? settings = null); - - /// - /// Generate from a . - /// - /// The source stream - /// OpenApiDiagnostic output - /// MappingModel - IReadOnlyList FromStream(Stream stream, out OpenApiDiagnostic diagnostic); - - /// - /// Generate from a . - /// - /// The source stream - /// Additional settings - /// OpenApiDiagnostic output - /// MappingModel - IReadOnlyList FromStream(Stream stream, WireMockOpenApiParserSettings settings, out OpenApiDiagnostic diagnostic); - - /// - /// Generate from a . - /// - /// The source text - /// OpenApiDiagnostic output - /// MappingModel - IReadOnlyList FromText(string text, out OpenApiDiagnostic diagnostic); - - /// - /// Generate from a . - /// - /// The source text - /// Additional settings - /// OpenApiDiagnostic output - /// MappingModel - IReadOnlyList FromText(string text, WireMockOpenApiParserSettings settings, out OpenApiDiagnostic diagnostic); -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs b/src-old/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs deleted file mode 100644 index 6056aecb..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Mappers/OpenApiPathsMapper.cs +++ /dev/null @@ -1,399 +0,0 @@ -// Copyright © WireMock.Net - -using System; -using System.Collections.Generic; -using System.Diagnostics.CodeAnalysis; -using System.IO; -using System.Linq; -using Microsoft.OpenApi; -using Microsoft.OpenApi.Any; -using Microsoft.OpenApi.Models; -using Microsoft.OpenApi.Writers; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using Stef.Validation; -using WireMock.Admin.Mappings; -using WireMock.Net.OpenApiParser.Extensions; -using WireMock.Net.OpenApiParser.Settings; -using WireMock.Net.OpenApiParser.Types; -using WireMock.Net.OpenApiParser.Utils; - -namespace WireMock.Net.OpenApiParser.Mappers; - -internal class OpenApiPathsMapper -{ - private const string HeaderContentType = "Content-Type"; - - private readonly WireMockOpenApiParserSettings _settings; - private readonly ExampleValueGenerator _exampleValueGenerator; - - public OpenApiPathsMapper(WireMockOpenApiParserSettings settings) - { - _settings = Guard.NotNull(settings); - _exampleValueGenerator = new ExampleValueGenerator(settings); - } - - public IReadOnlyList ToMappingModels(OpenApiPaths? paths, IList servers) - { - return paths? - .OrderBy(p => p.Key) - .Select(p => MapPath(p.Key, p.Value, servers)) - .SelectMany(x => x) - .ToArray() ?? - Array.Empty(); - } - - private IReadOnlyList MapPaths(OpenApiPaths? paths, IList servers) - { - return paths? - .OrderBy(p => p.Key) - .Select(p => MapPath(p.Key, p.Value, servers)) - .SelectMany(x => x) - .ToArray() ?? - Array.Empty(); - } - - private IReadOnlyList MapPath(string path, OpenApiPathItem pathItem, IList servers) - { - return pathItem.Operations.Select(o => MapOperationToMappingModel(path, o.Key.ToString().ToUpperInvariant(), o.Value, servers)).ToArray(); - } - - private MappingModel MapOperationToMappingModel(string path, string httpMethod, OpenApiOperation operation, IList servers) - { - var queryParameters = operation.Parameters.Where(p => p.In == ParameterLocation.Query); - var pathParameters = operation.Parameters.Where(p => p.In == ParameterLocation.Path); - var headers = operation.Parameters.Where(p => p.In == ParameterLocation.Header); - - var response = operation.Responses.FirstOrDefault(); - - TryGetContent(response.Value?.Content, out OpenApiMediaType? responseContent, out string? responseContentType); - var responseSchema = response.Value?.Content?.FirstOrDefault().Value?.Schema; - var responseExample = responseContent?.Example; - var responseSchemaExample = responseContent?.Schema?.Example; - - var body = responseExample != null ? MapOpenApiAnyToJToken(responseExample) : - responseSchemaExample != null ? MapOpenApiAnyToJToken(responseSchemaExample) : - MapSchemaToObject(responseSchema); - - var requestBodyModel = new BodyModel(); - if (operation.RequestBody != null && operation.RequestBody.Content != null && operation.RequestBody.Required) - { - var request = operation.RequestBody.Content; - TryGetContent(request, out OpenApiMediaType? requestContent, out _); - - var requestBodySchema = operation.RequestBody.Content.First().Value?.Schema; - var requestBodyExample = requestContent!.Example; - var requestBodySchemaExample = requestContent.Schema?.Example; - - var requestBodyMapped = requestBodyExample != null ? MapOpenApiAnyToJToken(requestBodyExample) : - requestBodySchemaExample != null ? MapOpenApiAnyToJToken(requestBodySchemaExample) : - MapSchemaToObject(requestBodySchema); - - requestBodyModel = MapRequestBody(requestBodyMapped); - } - - if (!int.TryParse(response.Key, out var httpStatusCode)) - { - httpStatusCode = 200; - } - - return new MappingModel - { - Guid = Guid.NewGuid(), - Request = new RequestModel - { - Methods = [httpMethod], - Path = PathUtils.Combine(MapBasePath(servers), MapPathWithParameters(path, pathParameters)), - Params = MapQueryParameters(queryParameters), - Headers = MapRequestHeaders(headers), - Body = requestBodyModel - }, - Response = new ResponseModel - { - StatusCode = httpStatusCode, - Headers = MapHeaders(responseContentType, response.Value?.Headers), - BodyAsJson = body - } - }; - } - - private BodyModel? MapRequestBody(object? requestBody) - { - if (requestBody == null) - { - return null; - } - - return new BodyModel - { - Matcher = new MatcherModel - { - Name = "JsonMatcher", - Pattern = JsonConvert.SerializeObject(requestBody, Formatting.Indented), - IgnoreCase = _settings.RequestBodyIgnoreCase - } - }; - } - - private static bool TryGetContent(IDictionary? contents, [NotNullWhen(true)] out OpenApiMediaType? openApiMediaType, [NotNullWhen(true)] out string? contentType) - { - openApiMediaType = null; - contentType = null; - - if (contents == null || contents.Values.Count == 0) - { - return false; - } - - if (contents.TryGetValue("application/json", out var content)) - { - openApiMediaType = content; - contentType = "application/json"; - } - else - { - var first = contents.FirstOrDefault(); - openApiMediaType = first.Value; - contentType = first.Key; - } - - return true; - } - - private object? MapSchemaToObject(OpenApiSchema? schema, string? name = null) - { - if (schema == null) - { - return null; - } - - switch (schema.GetSchemaType()) - { - case SchemaType.Array: - var jArray = new JArray(); - for (int i = 0; i < _settings.NumberOfArrayItems; i++) - { - if (schema.Items.Properties.Count > 0) - { - var arrayItem = new JObject(); - foreach (var property in schema.Items.Properties) - { - var objectValue = MapSchemaToObject(property.Value, property.Key); - if (objectValue is JProperty jp) - { - arrayItem.Add(jp); - } - else - { - arrayItem.Add(new JProperty(property.Key, objectValue)); - } - } - - jArray.Add(arrayItem); - } - else - { - var arrayItem = MapSchemaToObject(schema.Items, name: null); // Set name to null to force JObject instead of JProperty - jArray.Add(arrayItem); - } - } - - if (schema.AllOf.Count > 0) - { - jArray.Add(MapSchemaAllOfToObject(schema)); - } - - return jArray; - - case SchemaType.Boolean: - case SchemaType.Integer: - case SchemaType.Number: - case SchemaType.String: - return _exampleValueGenerator.GetExampleValue(schema); - - case SchemaType.Object: - var propertyAsJObject = new JObject(); - foreach (var schemaProperty in schema.Properties) - { - propertyAsJObject.Add(MapPropertyAsJObject(schemaProperty.Value, schemaProperty.Key)); - } - - if (schema.AllOf.Count > 0) - { - foreach (var group in schema.AllOf.SelectMany(p => p.Properties).GroupBy(x => x.Key)) - { - propertyAsJObject.Add(MapPropertyAsJObject(group.First().Value, group.Key)); - } - } - - return name != null ? new JProperty(name, propertyAsJObject) : propertyAsJObject; - - default: - return null; - } - } - - private JObject MapSchemaAllOfToObject(OpenApiSchema schema) - { - var arrayItem = new JObject(); - foreach (var property in schema.AllOf) - { - foreach (var item in property.Properties) - { - arrayItem.Add(MapPropertyAsJObject(item.Value, item.Key)); - } - } - return arrayItem; - } - - private object MapPropertyAsJObject(OpenApiSchema openApiSchema, string key) - { - if (openApiSchema.GetSchemaType() == SchemaType.Object || openApiSchema.GetSchemaType() == SchemaType.Array) - { - var mapped = MapSchemaToObject(openApiSchema, key); - if (mapped is JProperty jp) - { - return jp; - } - - return new JProperty(key, mapped); - } - - // bool propertyIsNullable = openApiSchema.Nullable || (openApiSchema.TryGetXNullable(out bool x) && x); - return new JProperty(key, _exampleValueGenerator.GetExampleValue(openApiSchema)); - } - - private string MapPathWithParameters(string path, IEnumerable? parameters) - { - if (parameters == null) - { - return path; - } - - string newPath = path; - foreach (var parameter in parameters) - { - var exampleMatcherModel = GetExampleMatcherModel(parameter.Schema, _settings.PathPatternToUse); - newPath = newPath.Replace($"{{{parameter.Name}}}", exampleMatcherModel.Pattern as string); - } - - return newPath; - } - - private string MapBasePath(IList? servers) - { - if (servers == null || servers.Count == 0) - { - return string.Empty; - } - - OpenApiServer server = servers.First(); - if (Uri.TryCreate(server.Url, UriKind.RelativeOrAbsolute, out Uri uriResult)) - { - return uriResult.IsAbsoluteUri ? uriResult.AbsolutePath : uriResult.ToString(); - } - - return string.Empty; - } - - private JToken? MapOpenApiAnyToJToken(IOpenApiAny? any) - { - if (any == null) - { - return null; - } - - using var outputString = new StringWriter(); - var writer = new OpenApiJsonWriter(outputString); - any.Write(writer, OpenApiSpecVersion.OpenApi3_0); - - if (any.AnyType == AnyType.Array) - { - return JArray.Parse(outputString.ToString()); - } - - return JObject.Parse(outputString.ToString()); - } - - private IDictionary? MapHeaders(string? responseContentType, IDictionary? headers) - { - var mappedHeaders = headers?.ToDictionary( - item => item.Key, - _ => GetExampleMatcherModel(null, _settings.HeaderPatternToUse).Pattern! - ) ?? new Dictionary(); - - if (!string.IsNullOrEmpty(responseContentType)) - { - mappedHeaders.TryAdd(HeaderContentType, responseContentType!); - } - - return mappedHeaders.Keys.Any() ? mappedHeaders : null; - } - - private IList? MapQueryParameters(IEnumerable queryParameters) - { - var list = queryParameters - .Where(req => req.Required) - .Select(qp => new ParamModel - { - Name = qp.Name, - IgnoreCase = _settings.QueryParameterPatternIgnoreCase, - Matchers = new[] - { - GetExampleMatcherModel(qp.Schema, _settings.QueryParameterPatternToUse) - } - }) - .ToList(); - - return list.Any() ? list : null; - } - - private IList? MapRequestHeaders(IEnumerable headers) - { - var list = headers - .Where(req => req.Required) - .Select(qp => new HeaderModel - { - Name = qp.Name, - IgnoreCase = _settings.HeaderPatternIgnoreCase, - Matchers = new[] - { - GetExampleMatcherModel(qp.Schema, _settings.HeaderPatternToUse) - } - }) - .ToList(); - - return list.Any() ? list : null; - } - - private MatcherModel GetExampleMatcherModel(OpenApiSchema? schema, ExampleValueType type) - { - return type switch - { - ExampleValueType.Value => new MatcherModel - { - Name = "ExactMatcher", - Pattern = GetExampleValueAsStringForSchemaType(schema), - IgnoreCase = _settings.IgnoreCaseExampleValues - }, - - _ => new MatcherModel - { - Name = "WildcardMatcher", - Pattern = "*" - } - }; - } - - private string GetExampleValueAsStringForSchemaType(OpenApiSchema? schema) - { - var value = _exampleValueGenerator.GetExampleValue(schema); - - return value switch - { - string valueAsString => valueAsString, - - _ => value.ToString(), - }; - } -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Properties/AssemblyInfo.cs b/src-old/WireMock.Net.OpenApiParser/Properties/AssemblyInfo.cs deleted file mode 100644 index fb354ea5..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Properties/AssemblyInfo.cs +++ /dev/null @@ -1,5 +0,0 @@ -// Copyright © WireMock.Net - -using System.Runtime.CompilerServices; - -[assembly: InternalsVisibleTo("WireMock.Net.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e138ec44d93acac565953052636eb8d5e7e9f27ddb030590055cd1a0ab2069a5623f1f77ca907d78e0b37066ca0f6d63da7eecc3fcb65b76aa8ebeccf7ebe1d11264b8404cd9b1cbbf2c83f566e033b3e54129f6ef28daffff776ba7aebbc53c0d635ebad8f45f78eb3f7e0459023c218f003416e080f96a1a3c5ffeb56bee9e")] \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Settings/IWireMockOpenApiParserExampleValues.cs b/src-old/WireMock.Net.OpenApiParser/Settings/IWireMockOpenApiParserExampleValues.cs deleted file mode 100644 index eecfed7e..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Settings/IWireMockOpenApiParserExampleValues.cs +++ /dev/null @@ -1,62 +0,0 @@ -// Copyright © WireMock.Net - -using System; -using Microsoft.OpenApi.Models; - -namespace WireMock.Net.OpenApiParser.Settings; - -/// -/// A interface defining the example values to use for the different types. -/// -public interface IWireMockOpenApiParserExampleValues -{ - /// - /// An example value for a Boolean. - /// - bool Boolean { get; } - - /// - /// An example value for an Integer. - /// - int Integer { get; } - - /// - /// An example value for a Float. - /// - float Float { get; } - - /// - /// An example value for a Double. - /// - double Double { get; } - - /// - /// An example value for a Date. - /// - Func Date { get; } - - /// - /// An example value for a DateTime. - /// - Func DateTime { get; } - - /// - /// An example value for Bytes. - /// - byte[] Bytes { get; } - - /// - /// An example value for a Object. - /// - object Object { get; } - - /// - /// An example value for a String. - /// - string String { get; } - - /// - /// OpenApi Schema to generate dynamic examples more accurate - /// - OpenApiSchema? Schema { get; set; } -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Settings/WireMockOpenApiParserDynamicExampleValues.cs b/src-old/WireMock.Net.OpenApiParser/Settings/WireMockOpenApiParserDynamicExampleValues.cs deleted file mode 100644 index 46b2c766..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Settings/WireMockOpenApiParserDynamicExampleValues.cs +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright © WireMock.Net - -using System; -using Microsoft.OpenApi.Models; -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 double Double => RandomizerFactory.GetRandomizer(new FieldOptionsDouble()).Generate() ?? 4.2d; - - /// - 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 OpenApiSchema? Schema { get; set; } -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Settings/WireMockOpenApiParserExampleValues.cs b/src-old/WireMock.Net.OpenApiParser/Settings/WireMockOpenApiParserExampleValues.cs deleted file mode 100644 index 99665cf0..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Settings/WireMockOpenApiParserExampleValues.cs +++ /dev/null @@ -1,42 +0,0 @@ -// Copyright © WireMock.Net - -using System; -using Microsoft.OpenApi.Models; - -namespace WireMock.Net.OpenApiParser.Settings; - -/// -/// A class defining the example values to use for the different types. -/// -public class WireMockOpenApiParserExampleValues : IWireMockOpenApiParserExampleValues -{ - /// - public virtual bool Boolean => true; - - /// - public virtual int Integer => 42; - - /// - public virtual float Float => 4.2f; - - /// - public virtual double Double => 4.2d; - - /// - public virtual Func Date { get; } = () => System.DateTime.UtcNow.Date; - - /// - public virtual Func DateTime { get; } = () => System.DateTime.UtcNow; - - /// - public virtual byte[] Bytes { get; } = { 48, 49, 50 }; - - /// - public virtual object Object => "example-object"; - - /// - public virtual string String => "example-string"; - - /// - public virtual OpenApiSchema? Schema { get; set; } = new(); -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Settings/WireMockOpenApiParserSettings.cs b/src-old/WireMock.Net.OpenApiParser/Settings/WireMockOpenApiParserSettings.cs deleted file mode 100644 index ea3cc600..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Settings/WireMockOpenApiParserSettings.cs +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright © WireMock.Net - -using WireMock.Net.OpenApiParser.Types; - -namespace WireMock.Net.OpenApiParser.Settings; - -/// -/// The WireMockOpenApiParser Settings -/// -public class WireMockOpenApiParserSettings -{ - /// - /// The number of array items to generate (default is 3). - /// - public int NumberOfArrayItems { get; set; } = 3; - - /// - /// The example value type to use when generating a Path - /// - public ExampleValueType PathPatternToUse { get; set; } = ExampleValueType.Value; - - /// - /// The example value type to use when generating a Header - /// - public ExampleValueType HeaderPatternToUse { get; set; } = ExampleValueType.Value; - - /// - /// The example value type to use when generating a Query Parameter - /// - public ExampleValueType QueryParameterPatternToUse { get; set; } = ExampleValueType.Value; - - /// - /// The example values to use. - /// - /// Default implementations are: - /// - - /// - - /// - public IWireMockOpenApiParserExampleValues? ExampleValues { get; set; } - - /// - /// Is a Header match case-insensitive? - /// - /// Default is true. - /// - public bool HeaderPatternIgnoreCase { get; set; } = true; - - /// - /// Is a Query Parameter match case-insensitive? - /// - /// Default is true. - /// - public bool QueryParameterPatternIgnoreCase { get; set; } = true; - - /// - /// Is a Request Body match case-insensitive? - /// - /// Default is true. - /// - public bool RequestBodyIgnoreCase { get; set; } = true; - - /// - /// Is a ExampleValue match case-insensitive? - /// - /// Default is true. - /// - public bool IgnoreCaseExampleValues { get; set; } = true; - - /// - /// Are examples generated dynamically? - /// - public bool DynamicExamples { get; set; } -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Types/ExampleValueType.cs b/src-old/WireMock.Net.OpenApiParser/Types/ExampleValueType.cs deleted file mode 100644 index 98856b3a..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Types/ExampleValueType.cs +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright © WireMock.Net - -namespace WireMock.Net.OpenApiParser.Types; - -/// -/// The example value to use -/// -public enum ExampleValueType -{ - /// - /// 1. Use a generated example value based on the SchemaType (default). - /// 2. If there is no example value defined in the schema, - /// then the will be used (custom, fixed or dynamic). - /// - Value, - - /// - /// Just use a Wildcard (*) character. - /// - Wildcard -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Types/SchemaFormat.cs b/src-old/WireMock.Net.OpenApiParser/Types/SchemaFormat.cs deleted file mode 100644 index c5370238..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Types/SchemaFormat.cs +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright © WireMock.Net - -namespace WireMock.Net.OpenApiParser.Types; - -internal enum SchemaFormat -{ - Float, - - Double, - - Int32, - - Int64, - - Date, - - DateTime, - - Password, - - Byte, - - Binary, - - Undefined -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Types/SchemaType.cs b/src-old/WireMock.Net.OpenApiParser/Types/SchemaType.cs deleted file mode 100644 index 4b5df64d..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Types/SchemaType.cs +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright © WireMock.Net - -namespace WireMock.Net.OpenApiParser.Types; - -internal enum SchemaType -{ - Object, - - Array, - - String, - - Integer, - - Number, - - Boolean, - - File, - - Unknown -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Utils/DateTimeUtils.cs b/src-old/WireMock.Net.OpenApiParser/Utils/DateTimeUtils.cs deleted file mode 100644 index c0108295..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Utils/DateTimeUtils.cs +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright © WireMock.Net - -using System; -using System.Globalization; - -namespace WireMock.Net.OpenApiParser.Utils; - -internal static class DateTimeUtils -{ - public static string ToRfc3339DateTime(DateTime dateTime) - { - return dateTime.ToString("yyyy-MM-dd'T'HH:mm:ss.fffzzz", DateTimeFormatInfo.InvariantInfo); - } - - public static string ToRfc3339Date(DateTime dateTime) - { - return dateTime.ToString("yyyy-MM-dd", DateTimeFormatInfo.InvariantInfo); - } -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Utils/ExampleValueGenerator.cs b/src-old/WireMock.Net.OpenApiParser/Utils/ExampleValueGenerator.cs deleted file mode 100644 index 4039a358..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Utils/ExampleValueGenerator.cs +++ /dev/null @@ -1,119 +0,0 @@ -// Copyright © WireMock.Net - -using System.Linq; -using Microsoft.OpenApi.Any; -using Microsoft.OpenApi.Models; -using Stef.Validation; -using WireMock.Net.OpenApiParser.Extensions; -using WireMock.Net.OpenApiParser.Settings; -using WireMock.Net.OpenApiParser.Types; - -namespace WireMock.Net.OpenApiParser.Utils; - -internal class ExampleValueGenerator -{ - private readonly IWireMockOpenApiParserExampleValues _exampleValues; - - public ExampleValueGenerator(WireMockOpenApiParserSettings settings) - { - Guard.NotNull(settings); - - // Check if user provided an own implementation - if (settings.ExampleValues is null) - { - if (settings.DynamicExamples) - { - _exampleValues = new WireMockOpenApiParserDynamicExampleValues(); - } - else - { - _exampleValues = new WireMockOpenApiParserExampleValues(); - } - } - else - { - _exampleValues = settings.ExampleValues; - } - } - - public object GetExampleValue(OpenApiSchema? schema) - { - var schemaExample = schema?.Example; - var schemaEnum = schema?.Enum?.FirstOrDefault(); - - _exampleValues.Schema = schema; - - switch (schema?.GetSchemaType()) - { - case SchemaType.Boolean: - var exampleBoolean = schemaExample as OpenApiBoolean; - return exampleBoolean?.Value ?? _exampleValues.Boolean; - - case SchemaType.Integer: - switch (schema?.GetSchemaFormat()) - { - case SchemaFormat.Int64: - var exampleLong = schemaExample as OpenApiLong; - var enumLong = schemaEnum as OpenApiLong; - var valueLongEnumOrExample = enumLong?.Value ?? exampleLong?.Value; - return valueLongEnumOrExample ?? _exampleValues.Integer; - - default: - var exampleInteger = schemaExample as OpenApiInteger; - var enumInteger = schemaEnum as OpenApiInteger; - var valueIntegerEnumOrExample = enumInteger?.Value ?? exampleInteger?.Value; - return valueIntegerEnumOrExample ?? _exampleValues.Integer; - } - - case SchemaType.Number: - switch (schema?.GetSchemaFormat()) - { - case SchemaFormat.Float: - var exampleFloat = schemaExample as OpenApiFloat; - var enumFloat = schemaEnum as OpenApiFloat; - var valueFloatEnumOrExample = enumFloat?.Value ?? exampleFloat?.Value; - return valueFloatEnumOrExample ?? _exampleValues.Float; - - default: - var exampleDouble = schemaExample as OpenApiDouble; - var enumDouble = schemaEnum as OpenApiDouble; - var valueDoubleEnumOrExample = enumDouble?.Value ?? exampleDouble?.Value; - return valueDoubleEnumOrExample ?? _exampleValues.Double; - } - - default: - switch (schema?.GetSchemaFormat()) - { - case SchemaFormat.Date: - var exampleDate = schemaExample as OpenApiDate; - var enumDate = schemaEnum as OpenApiDate; - var valueDateEnumOrExample = enumDate?.Value ?? exampleDate?.Value; - return DateTimeUtils.ToRfc3339Date(valueDateEnumOrExample ?? _exampleValues.Date()); - - case SchemaFormat.DateTime: - var exampleDateTime = schemaExample as OpenApiDateTime; - var enumDateTime = schemaEnum as OpenApiDateTime; - var valueDateTimeEnumOrExample = enumDateTime?.Value ?? exampleDateTime?.Value; - return DateTimeUtils.ToRfc3339DateTime(valueDateTimeEnumOrExample?.DateTime ?? _exampleValues.DateTime()); - - case SchemaFormat.Byte: - var exampleByte = schemaExample as OpenApiByte; - var enumByte = schemaEnum as OpenApiByte; - var valueByteEnumOrExample = enumByte?.Value ?? exampleByte?.Value; - return valueByteEnumOrExample ?? _exampleValues.Bytes; - - case SchemaFormat.Binary: - var exampleBinary = schemaExample as OpenApiBinary; - var enumBinary = schemaEnum as OpenApiBinary; - var valueBinaryEnumOrExample = enumBinary?.Value ?? exampleBinary?.Value; - return valueBinaryEnumOrExample ?? _exampleValues.Object; - - default: - var exampleString = schemaExample as OpenApiString; - var enumString = schemaEnum as OpenApiString; - var valueStringEnumOrExample = enumString?.Value ?? exampleString?.Value; - return valueStringEnumOrExample ?? _exampleValues.String; - } - } - } -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/Utils/PathUtils.cs b/src-old/WireMock.Net.OpenApiParser/Utils/PathUtils.cs deleted file mode 100644 index c46b3923..00000000 --- a/src-old/WireMock.Net.OpenApiParser/Utils/PathUtils.cs +++ /dev/null @@ -1,27 +0,0 @@ -// Copyright © WireMock.Net - -namespace WireMock.Net.OpenApiParser.Utils; - -internal static class PathUtils -{ - internal static string Combine(params string[] paths) - { - if (paths.Length == 0) - { - return string.Empty; - } - - var result = paths[0].Trim().TrimEnd('/'); - - for (int i = 1; i < paths.Length; i++) - { - var nextPath = paths[i].Trim().TrimStart('/').TrimEnd('/'); - if (!string.IsNullOrEmpty(nextPath)) - { - result += '/' + nextPath; - } - } - - return result; - } -} \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/WireMock.Net.OpenApiParser.csproj b/src-old/WireMock.Net.OpenApiParser/WireMock.Net.OpenApiParser.csproj deleted file mode 100644 index 9fac3c76..00000000 --- a/src-old/WireMock.Net.OpenApiParser/WireMock.Net.OpenApiParser.csproj +++ /dev/null @@ -1,37 +0,0 @@ - - - - An OpenApi (swagger) parser to generate MappingModel or mapping.json file. - net46;netstandard2.0;netstandard2.1 - true - wiremock;openapi;OAS;raml;converter;parser;openapiparser - {D3804228-91F4-4502-9595-39584E5AADAD} - true - ../WireMock.Net/WireMock.Net.ruleset - true - ../WireMock.Net/WireMock.Net.snk - true - MIT - - - - true - - - - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - - - - - - - - - - \ No newline at end of file diff --git a/src-old/WireMock.Net.OpenApiParser/WireMockOpenApiParser.cs b/src-old/WireMock.Net.OpenApiParser/WireMockOpenApiParser.cs deleted file mode 100644 index c027eed9..00000000 --- a/src-old/WireMock.Net.OpenApiParser/WireMockOpenApiParser.cs +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright © WireMock.Net - -using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using JetBrains.Annotations; -using Microsoft.OpenApi.Models; -using Microsoft.OpenApi.Readers; -using RamlToOpenApiConverter; -using WireMock.Admin.Mappings; -using WireMock.Net.OpenApiParser.Mappers; -using WireMock.Net.OpenApiParser.Settings; - -namespace WireMock.Net.OpenApiParser; - -/// -/// Parse a OpenApi/Swagger/V2/V3 or Raml to WireMock.Net MappingModels. -/// -public class WireMockOpenApiParser : IWireMockOpenApiParser -{ - private readonly OpenApiStreamReader _reader = new(); - - /// - [PublicAPI] - public IReadOnlyList FromFile(string path, out OpenApiDiagnostic diagnostic) - { - return FromFile(path, new WireMockOpenApiParserSettings(), out diagnostic); - } - - /// - [PublicAPI] - public IReadOnlyList FromFile(string path, WireMockOpenApiParserSettings settings, out OpenApiDiagnostic diagnostic) - { - OpenApiDocument document; - if (Path.GetExtension(path).EndsWith("raml", StringComparison.OrdinalIgnoreCase)) - { - diagnostic = new OpenApiDiagnostic(); - document = new RamlConverter().ConvertToOpenApiDocument(path); - } - else - { - var reader = new OpenApiStreamReader(); - document = reader.Read(File.OpenRead(path), out diagnostic); - } - - return FromDocument(document, settings); - } - - /// - [PublicAPI] - public IReadOnlyList FromDocument(OpenApiDocument document, WireMockOpenApiParserSettings? settings = null) - { - return new OpenApiPathsMapper(settings ?? new WireMockOpenApiParserSettings()).ToMappingModels(document.Paths, document.Servers); - } - - /// - [PublicAPI] - public IReadOnlyList FromStream(Stream stream, out OpenApiDiagnostic diagnostic) - { - return FromDocument(_reader.Read(stream, out diagnostic)); - } - - /// - [PublicAPI] - public IReadOnlyList FromStream(Stream stream, WireMockOpenApiParserSettings settings, out OpenApiDiagnostic diagnostic) - { - return FromDocument(_reader.Read(stream, out diagnostic), settings); - } - - /// - [PublicAPI] - public IReadOnlyList FromText(string text, out OpenApiDiagnostic diagnostic) - { - return FromStream(new MemoryStream(Encoding.UTF8.GetBytes(text)), out diagnostic); - } - - /// - [PublicAPI] - public IReadOnlyList FromText(string text, WireMockOpenApiParserSettings settings, out OpenApiDiagnostic diagnostic) - { - return FromStream(new MemoryStream(Encoding.UTF8.GetBytes(text)), settings, out diagnostic); - } -} \ No newline at end of file