Fix OpenApiPathsMapper (#1122)

* Fix OpenApiPathsMapper

* utc

* s
This commit is contained in:
Stef Heyenrath
2024-07-22 21:36:45 +02:00
committed by GitHub
parent 422e7c9b5e
commit d79f6f128d
7 changed files with 38778 additions and 37 deletions

View File

@@ -1,5 +1,6 @@
// Copyright © WireMock.Net
using System.Linq;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Interfaces;
using Microsoft.OpenApi.Models;
@@ -16,7 +17,7 @@ internal static class OpenApiSchemaExtensions
{
value = false;
if (schema.Extensions.TryGetValue("x-nullable", out IOpenApiExtension e) && e is OpenApiBoolean openApiBoolean)
if (schema.Extensions.TryGetValue("x-nullable", out var e) && e is OpenApiBoolean openApiBoolean)
{
value = openApiBoolean.Value;
return true;
@@ -27,32 +28,30 @@ internal static class OpenApiSchemaExtensions
public static SchemaType GetSchemaType(this OpenApiSchema? schema)
{
switch (schema?.Type)
if (schema == null)
{
case "object":
return SchemaType.Object;
case "array":
return SchemaType.Array;
case "integer":
return SchemaType.Integer;
case "number":
return SchemaType.Number;
case "boolean":
return SchemaType.Boolean;
case "string":
return SchemaType.String;
case "file":
return SchemaType.File;
default:
return SchemaType.Unknown;
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)

View File

@@ -193,7 +193,8 @@ internal class OpenApiPathsMapper
}
else
{
jArray.Add(MapSchemaToObject(schema.Items, name));
var arrayItem = MapSchemaToObject(schema.Items, name: null); // Set name to null to force JObject instead of JProperty
jArray.Add(arrayItem);
}
}
@@ -219,12 +220,9 @@ internal class OpenApiPathsMapper
if (schema.AllOf.Count > 0)
{
foreach (var property in schema.AllOf)
foreach (var group in schema.AllOf.SelectMany(p => p.Properties).GroupBy(x => x.Key))
{
foreach (var item in property.Properties)
{
propertyAsJObject.Add(MapPropertyAsJObject(item.Value, item.Key));
}
propertyAsJObject.Add(MapPropertyAsJObject(group.First().Value, group.Key));
}
}

View File

@@ -39,27 +39,35 @@ public class WireMockOpenApiParserSettings
public IWireMockOpenApiParserExampleValues? ExampleValues { get; set; }
/// <summary>
/// Is a Header match case insensitive? (default is true).
/// Is a Header match case-insensitive?
///
/// Default is <c>true</c>.
/// </summary>
public bool HeaderPatternIgnoreCase { get; set; } = true;
/// <summary>
/// Is a Query Parameter match case insensitive? (default is true).
/// Is a Query Parameter match case-insensitive?
///
/// Default is <c>true</c>.
/// </summary>
public bool QueryParameterPatternIgnoreCase { get; set; } = true;
/// <summary>
/// Is a Request Body match case insensitive? (default is true).
/// Is a Request Body match case-insensitive?
///
/// Default is <c>true</c>.
/// </summary>
public bool RequestBodyIgnoreCase { get; set; } = true;
/// <summary>
/// Is a ExampleValue match case insensitive? (default is true).
/// Is a ExampleValue match case-insensitive?
///
/// Default is <c>true</c>.
/// </summary>
public bool IgnoreCaseExampleValues { get; set; } = true;
/// <summary>
/// Are examples generated dynamically? (default is false).
/// Are examples generated dynamically?
/// </summary>
public bool DynamicExamples { get; set; } = false;
public bool DynamicExamples { get; set; }
}

View File

@@ -0,0 +1,51 @@
#if !(NET452 || NET461 || NETCOREAPP3_1)
using System;
using System.IO;
using System.Threading.Tasks;
using Moq;
using VerifyXunit;
using WireMock.Net.OpenApiParser;
using WireMock.Net.OpenApiParser.Settings;
using Xunit;
namespace WireMock.Net.Tests.OpenApiParser;
[UsesVerify]
public class WireMockOpenApiParserTests
{
private readonly Mock<IWireMockOpenApiParserExampleValues> _exampleValuesMock = new();
private readonly WireMockOpenApiParser _sut = new();
public WireMockOpenApiParserTests()
{
_exampleValuesMock.SetupGet(e => e.Boolean).Returns(true);
_exampleValuesMock.SetupGet(e => e.Integer).Returns(42);
_exampleValuesMock.SetupGet(e => e.Float).Returns(1.1f);
_exampleValuesMock.SetupGet(e => e.Double).Returns(2.2d);
_exampleValuesMock.SetupGet(e => e.String).Returns("example-string");
_exampleValuesMock.SetupGet(e => e.Object).Returns("example-object");
_exampleValuesMock.SetupGet(e => e.Bytes).Returns("Stef"u8.ToArray());
_exampleValuesMock.SetupGet(e => e.Date).Returns(() => new DateTime(2024, 6, 19));
_exampleValuesMock.SetupGet(e => e.DateTime).Returns(() => new DateTime(2024, 6, 19, 12, 34, 56, DateTimeKind.Utc));
}
[Fact]
public async Task FromText_ShouldReturnMappings()
{
// Arrange
var settings = new WireMockOpenApiParserSettings
{
ExampleValues = _exampleValuesMock.Object
};
var openApiDocument = await File.ReadAllTextAsync(Path.Combine("OpenApiParser", "payroc-openapi-spec.yaml"));
// Act
var mappings = _sut.FromText(openApiDocument, settings, out _);
// Verify
await Verifier.Verify(mappings);
}
}
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -128,6 +128,9 @@
<None Update="OpenApiParser\*.yml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="OpenApiParser\*.yaml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="cert.pem">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>