using System; using System.IO; using System.Linq; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.Readers; using WireMock.Server; namespace WireMock.Net.OpenApiParser.Extensions { 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 public static IWireMockServer WithMappingFromOpenApiFile(this IWireMockServer server, string path, out OpenApiDiagnostic diagnostic) { if (server == null) { throw new ArgumentNullException(nameof(server)); } if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } var mappings = new WireMockOpenApiParser().FromFile(path, 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 public static IWireMockServer WithMappingFromOpenApiStream(this IWireMockServer server, Stream stream, out OpenApiDiagnostic diagnostic) { var mappings = new WireMockOpenApiParser().FromStream(stream, 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. public static IWireMockServer WithMappingFromOpenApiDocument(this IWireMockServer server, OpenApiDocument document) { var mappings = new WireMockOpenApiParser().FromDocument(document); return server.WithMapping(mappings.ToArray()); } } }