Skip to content

Commit 208d0fd

Browse files
committed
Add tests to validate
1 parent e3310f5 commit 208d0fd

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,5 +495,21 @@ public void ParseSchemaWithConstWorks()
495495
var schemaString = writer.ToString();
496496
schemaString.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral());
497497
}
498+
499+
[Fact]
500+
public void ParseSchemaWithUnrecognizedKeywordsWorks()
501+
{
502+
var input = @"{
503+
""type"": ""string"",
504+
""format"": ""date-time"",
505+
""customKeyword"": ""customValue"",
506+
""anotherKeyword"": 42,
507+
""x-test"": ""test""
508+
}
509+
";
510+
var schema = OpenApiModelFactory.Parse<OpenApiSchema>(input, OpenApiSpecVersion.OpenApi3_1, out _, "json");
511+
schema.UnrecognizedKeywords.Should().HaveCount(2);
512+
}
513+
498514
}
499515
}

test/Microsoft.OpenApi.Readers.Tests/V31Tests/Samples/OpenApiSchema/schemaWithJsonSchemaKeywords.yaml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,3 @@ required:
2828
- name
2929

3030
$dynamicAnchor: "addressDef"
31-
definitions:
32-
address:
33-
$dynamicAnchor: "addressDef"
34-
type: "object"
35-
properties:
36-
street:
37-
type: "string"
38-
city:
39-
type: "string"
40-
postalCode:
41-
type: "string"

test/Microsoft.OpenApi.Tests/Models/OpenApiSchemaTests.cs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -602,15 +602,42 @@ public void OpenApiWalkerVisitsOpenApiSchemaNot()
602602
// Assert
603603
visitor.Titles.Count.Should().Be(2);
604604
}
605-
}
606605

607-
internal class SchemaVisitor : OpenApiVisitorBase
608-
{
609-
public List<string> Titles = new();
606+
[Fact]
607+
public void SerializeSchemaWithUnrecognizedPropertiesWorks()
608+
{
609+
// Arrange
610+
var schema = new OpenApiSchema
611+
{
612+
UnrecognizedKeywords = new Dictionary<string, JsonNode>()
613+
{
614+
["customKeyWord"] = "bar",
615+
["anotherKeyword"] = 42
616+
}
617+
};
610618

611-
public override void Visit(OpenApiSchema schema)
619+
var expected = @"{
620+
""unrecognizedProperties"": {
621+
""customKeyWord"": ""bar"",
622+
""anotherKeyword"": 42
623+
}
624+
}";
625+
626+
// Act
627+
var actual = schema.SerializeAsJson(OpenApiSpecVersion.OpenApi3_1);
628+
629+
// Assert
630+
actual.MakeLineBreaksEnvironmentNeutral().Should().Be(expected.MakeLineBreaksEnvironmentNeutral());
631+
}
632+
633+
internal class SchemaVisitor : OpenApiVisitorBase
612634
{
613-
Titles.Add(schema.Title);
635+
public List<string> Titles = new();
636+
637+
public override void Visit(OpenApiSchema schema)
638+
{
639+
Titles.Add(schema.Title);
640+
}
614641
}
615642
}
616643
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ namespace Microsoft.OpenApi.Models
512512
public const string Type = "type";
513513
public const string UnevaluatedProperties = "unevaluatedProperties";
514514
public const string UniqueItems = "uniqueItems";
515+
public const string UnrecognizedKeywords = "unrecognizedKeywords";
515516
public const string Url = "url";
516517
public const string V2ReferenceUri = "https://registry/definitions/";
517518
public const string V31ExclusiveMaximum = "exclusiveMaximum";
@@ -925,6 +926,7 @@ namespace Microsoft.OpenApi.Models
925926
public virtual bool UnEvaluatedProperties { get; set; }
926927
public virtual bool UnevaluatedProperties { get; set; }
927928
public virtual bool? UniqueItems { get; set; }
929+
public virtual System.Collections.Generic.IDictionary<string, System.Text.Json.Nodes.JsonNode> UnrecognizedKeywords { get; set; }
928930
public virtual bool UnresolvedReference { get; set; }
929931
public virtual decimal? V31ExclusiveMaximum { get; set; }
930932
public virtual decimal? V31ExclusiveMinimum { get; set; }
@@ -1859,6 +1861,7 @@ namespace Microsoft.OpenApi.Writers
18591861
public static void WriteOptionalCollection<T>(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IEnumerable<T> elements, System.Action<Microsoft.OpenApi.Writers.IOpenApiWriter, T> action) { }
18601862
public static void WriteOptionalMap(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IDictionary<string, bool> elements, System.Action<Microsoft.OpenApi.Writers.IOpenApiWriter, bool> action) { }
18611863
public static void WriteOptionalMap(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IDictionary<string, string> elements, System.Action<Microsoft.OpenApi.Writers.IOpenApiWriter, string> action) { }
1864+
public static void WriteOptionalMap(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IDictionary<string, System.Text.Json.Nodes.JsonNode> elements, System.Action<Microsoft.OpenApi.Writers.IOpenApiWriter, System.Text.Json.Nodes.JsonNode> action) { }
18621865
public static void WriteOptionalMap<T>(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IDictionary<string, T> elements, System.Action<Microsoft.OpenApi.Writers.IOpenApiWriter, T> action)
18631866
where T : Microsoft.OpenApi.Interfaces.IOpenApiElement { }
18641867
public static void WriteOptionalMap<T>(this Microsoft.OpenApi.Writers.IOpenApiWriter writer, string name, System.Collections.Generic.IDictionary<string, T> elements, System.Action<Microsoft.OpenApi.Writers.IOpenApiWriter, string, T> action)

0 commit comments

Comments
 (0)