mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-26 02:38:54 +02:00
Use ILRepack to include Microsoft.OpenApi as internal (#1290)
* . * Use ILRepack to include Microsoft.OpenApi as internal * ... * OpenApiSpecificationVersion * . * 080 * 4
This commit is contained in:
@@ -6,20 +6,35 @@ using System.IO;
|
||||
using System.Text;
|
||||
using JetBrains.Annotations;
|
||||
using Microsoft.OpenApi.Models;
|
||||
using Microsoft.OpenApi.Readers;
|
||||
using Microsoft.OpenApi.Reader;
|
||||
using Microsoft.OpenApi.YamlReader;
|
||||
using RamlToOpenApiConverter;
|
||||
using WireMock.Admin.Mappings;
|
||||
using WireMock.Net.OpenApiParser.Mappers;
|
||||
using WireMock.Net.OpenApiParser.Models;
|
||||
using WireMock.Net.OpenApiParser.Settings;
|
||||
using OpenApiDiagnostic = WireMock.Net.OpenApiParser.Models.OpenApiDiagnostic;
|
||||
|
||||
namespace WireMock.Net.OpenApiParser;
|
||||
|
||||
/// <summary>
|
||||
/// Parse a OpenApi/Swagger/V2/V3 or Raml to WireMock.Net MappingModels.
|
||||
/// Parse a OpenApi/Swagger/V2/V3/V3.1 to WireMock.Net MappingModels.
|
||||
/// </summary>
|
||||
public class WireMockOpenApiParser : IWireMockOpenApiParser
|
||||
{
|
||||
private readonly OpenApiStreamReader _reader = new();
|
||||
private readonly OpenApiReaderSettings _readerSettings;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="WireMockOpenApiParser"/> class.
|
||||
/// </summary>
|
||||
public WireMockOpenApiParser()
|
||||
{
|
||||
_readerSettings = new OpenApiReaderSettings();
|
||||
_readerSettings.AddMicrosoftExtensionParsers();
|
||||
_readerSettings.AddJsonReader();
|
||||
_readerSettings.TryAddReader(OpenApiConstants.Yaml, new OpenApiYamlReader());
|
||||
_readerSettings.TryAddReader(OpenApiConstants.Yml, new OpenApiYamlReader());
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
@@ -40,8 +55,7 @@ public class WireMockOpenApiParser : IWireMockOpenApiParser
|
||||
}
|
||||
else
|
||||
{
|
||||
var reader = new OpenApiStreamReader();
|
||||
document = reader.Read(File.OpenRead(path), out diagnostic);
|
||||
document = Read(File.OpenRead(path), out diagnostic);
|
||||
}
|
||||
|
||||
return FromDocument(document, settings);
|
||||
@@ -49,23 +63,25 @@ public class WireMockOpenApiParser : IWireMockOpenApiParser
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
public IReadOnlyList<MappingModel> FromDocument(OpenApiDocument document, WireMockOpenApiParserSettings? settings = null)
|
||||
public IReadOnlyList<MappingModel> FromDocument(object document, WireMockOpenApiParserSettings? settings = null)
|
||||
{
|
||||
return new OpenApiPathsMapper(settings ?? new WireMockOpenApiParserSettings()).ToMappingModels(document.Paths, document.Servers);
|
||||
var openApiDocument = document as OpenApiDocument ?? throw new ArgumentException("The document should be a Microsoft.OpenApi.Models.OpenApiDocument", nameof(document));
|
||||
|
||||
return new OpenApiPathsMapper(settings ?? new WireMockOpenApiParserSettings()).ToMappingModels(openApiDocument.Paths, openApiDocument.Servers ?? []);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
public IReadOnlyList<MappingModel> FromStream(Stream stream, out OpenApiDiagnostic diagnostic)
|
||||
{
|
||||
return FromDocument(_reader.Read(stream, out diagnostic));
|
||||
return FromDocument(Read(stream, out diagnostic));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
[PublicAPI]
|
||||
public IReadOnlyList<MappingModel> FromStream(Stream stream, WireMockOpenApiParserSettings settings, out OpenApiDiagnostic diagnostic)
|
||||
{
|
||||
return FromDocument(_reader.Read(stream, out diagnostic), settings);
|
||||
return FromDocument(Read(stream, out diagnostic), settings);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
@@ -81,4 +97,25 @@ public class WireMockOpenApiParser : IWireMockOpenApiParser
|
||||
{
|
||||
return FromStream(new MemoryStream(Encoding.UTF8.GetBytes(text)), settings, out diagnostic);
|
||||
}
|
||||
|
||||
private OpenApiDocument Read(Stream stream, out OpenApiDiagnostic diagnostic)
|
||||
{
|
||||
if (stream is not MemoryStream memoryStream)
|
||||
{
|
||||
memoryStream = ReadStreamIntoMemoryStream(stream);
|
||||
}
|
||||
|
||||
var result = OpenApiDocument.Load(memoryStream, settings: _readerSettings);
|
||||
|
||||
diagnostic = OpenApiMapper.Map(result.Diagnostic) ?? new OpenApiDiagnostic();
|
||||
return result.Document ?? throw new InvalidOperationException("The document is null.");
|
||||
}
|
||||
|
||||
private static MemoryStream ReadStreamIntoMemoryStream(Stream stream)
|
||||
{
|
||||
var memoryStream = new MemoryStream();
|
||||
stream.CopyTo(memoryStream);
|
||||
memoryStream.Position = 0;
|
||||
return memoryStream;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user