Skip to content

Commit 3c927e9

Browse files
committed
chore: add tests and update public API
1 parent 9ae9565 commit 3c927e9

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

test/Microsoft.OpenApi.Readers.Tests/V31Tests/OpenApiSchemaTests.cs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using FluentAssertions;
1010
using FluentAssertions.Equivalency;
1111
using Microsoft.OpenApi.Models;
12+
using Microsoft.OpenApi.Extensions;
1213
using Microsoft.OpenApi.Models.Interfaces;
1314
using Microsoft.OpenApi.Reader;
1415
using Microsoft.OpenApi.Tests;
@@ -31,7 +32,7 @@ public static MemoryStream GetMemoryStream(string fileName)
3132

3233
public OpenApiSchemaTests()
3334
{
34-
OpenApiReaderRegistry.RegisterReader("yaml", new OpenApiYamlReader());
35+
OpenApiReaderRegistry.RegisterReader("yaml", new OpenApiYamlReader());
3536
}
3637

3738
[Fact]
@@ -298,8 +299,8 @@ public void CloningSchemaWithExamplesAndEnumsShouldSucceed()
298299
clone.Default = 6;
299300

300301
// Assert
301-
Assert.Equivalent(new int[] {1, 2, 3, 4}, clone.Enum.Select(static x => x.GetValue<int>()).ToArray());
302-
Assert.Equivalent(new int[] {2, 3, 4}, clone.Examples.Select(static x => x.GetValue<int>()).ToArray());
302+
Assert.Equivalent(new int[] { 1, 2, 3, 4 }, clone.Enum.Select(static x => x.GetValue<int>()).ToArray());
303+
Assert.Equivalent(new int[] { 2, 3, 4 }, clone.Examples.Select(static x => x.GetValue<int>()).ToArray());
303304
Assert.Equivalent(6, clone.Default.GetValue<int>());
304305
}
305306

@@ -398,7 +399,7 @@ public void SerializeSchemaWithTypeArrayAndNullableDoesntEmitType()
398399
schema.SerializeAsV2(new OpenApiYamlWriter(writer));
399400
var schemaString = writer.ToString();
400401

401-
Assert.Equal(expected.MakeLineBreaksEnvironmentNeutral(), schemaString.MakeLineBreaksEnvironmentNeutral());
402+
Assert.Equal(expected.MakeLineBreaksEnvironmentNeutral(), schemaString.MakeLineBreaksEnvironmentNeutral());
402403
}
403404

404405
[Theory]
@@ -506,7 +507,7 @@ public async Task ParseSchemaWithConstWorks()
506507
}
507508

508509
[Fact]
509-
public void ParseSchemaWithUnrecognizedKeywordsWorks()
510+
public void ParseSchemaWithUnrecognizedKeywordsWorks()
510511
{
511512
var input = @"{
512513
""type"": ""string"",
@@ -520,5 +521,36 @@ public void ParseSchemaWithUnrecognizedKeywordsWorks()
520521
Assert.Equal(2, schema.UnrecognizedKeywords.Count);
521522
}
522523

524+
[Theory]
525+
[InlineData(JsonSchemaType.Integer | JsonSchemaType.String, new[] { "integer", "string" })]
526+
[InlineData(JsonSchemaType.Integer | JsonSchemaType.Null, new[] { "integer", "null" })]
527+
[InlineData(JsonSchemaType.Integer, new[] { "integer" })]
528+
public void NormalizeFlaggableJsonSchemaTypeEnumWorks(JsonSchemaType type, string[] expected)
529+
{
530+
var schema = new OpenApiSchema
531+
{
532+
Type = type
533+
};
534+
535+
var actual = schema.Type.ToIdentifier();
536+
Assert.Equal(expected, actual);
537+
}
538+
539+
[Theory]
540+
[InlineData(new[] { "integer", "string" }, JsonSchemaType.Integer | JsonSchemaType.String)]
541+
[InlineData(new[] { "integer", "null" }, JsonSchemaType.Integer | JsonSchemaType.Null)]
542+
[InlineData(new[] { "integer" }, JsonSchemaType.Integer)]
543+
public void ArrayIdentifierToEnumConversionWorks(string[] type, JsonSchemaType expected)
544+
{
545+
var actual = type.ToJsonSchemaType();
546+
Assert.Equal(expected, actual);
547+
}
548+
549+
[Fact]
550+
public void StringIdentifierToEnumConversionWorks()
551+
{
552+
var actual = "integer".ToJsonSchemaType();
553+
Assert.Equal(JsonSchemaType.Integer, actual);
554+
}
523555
}
524556
}

test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,10 @@ namespace Microsoft.OpenApi.Extensions
190190
{
191191
public static System.Type MapOpenApiPrimitiveTypeToSimpleType(this Microsoft.OpenApi.Models.OpenApiSchema schema) { }
192192
public static Microsoft.OpenApi.Models.OpenApiSchema MapTypeToOpenApiPrimitiveType(this System.Type type) { }
193-
public static string? ToIdentifier(this Microsoft.OpenApi.Models.JsonSchemaType schemaType) { }
194-
public static string? ToIdentifier(this Microsoft.OpenApi.Models.JsonSchemaType? schemaType) { }
193+
public static string[] ToIdentifier(this Microsoft.OpenApi.Models.JsonSchemaType schemaType) { }
194+
public static string[]? ToIdentifier(this Microsoft.OpenApi.Models.JsonSchemaType? schemaType) { }
195195
public static Microsoft.OpenApi.Models.JsonSchemaType ToJsonSchemaType(this string identifier) { }
196+
public static Microsoft.OpenApi.Models.JsonSchemaType ToJsonSchemaType(this string[] identifier) { }
196197
}
197198
}
198199
namespace Microsoft.OpenApi.Interfaces

0 commit comments

Comments
 (0)