diff --git a/tests/Kiota.Builder.Tests/Extensions/OpenApiSchemaExtensionsTests.cs b/tests/Kiota.Builder.Tests/Extensions/OpenApiSchemaExtensionsTests.cs index 6ca611be28..6418313f40 100644 --- a/tests/Kiota.Builder.Tests/Extensions/OpenApiSchemaExtensionsTests.cs +++ b/tests/Kiota.Builder.Tests/Extensions/OpenApiSchemaExtensionsTests.cs @@ -673,6 +673,289 @@ public void MergesIntersectionRecursively() Assert.Equal("description", result.Description); Assert.True(result.Deprecated); } + + public class MergeSingleInclusiveUnionInheritanceOrIntersectionSchemaEntries + { + [Fact] + public void DoesMergeWithInheritance() + { + var schema = new OpenApiSchema() + { + Type = "object", + AnyOf = [ + new() { + Properties = new Dictionary() { + ["one"] = new OpenApiSchema(), + }, + AllOf = [ + new() { + Reference = new() { + Id = "BaseClass" + }, + }, + new() { + Type = "object", + Properties = new Dictionary() { + ["firstName"] = new OpenApiSchema(), + ["lastName"] = new OpenApiSchema() + } + }, + ] + }, + ], + }; + + var result = schema.MergeSingleInclusiveUnionInheritanceOrIntersectionSchemaEntries(); + Assert.True(schema.AnyOf[0].IsInherited()); + Assert.NotNull(result); + Assert.True(result.IsInherited()); + Assert.Contains("one", result.Properties.Keys); + Assert.Empty(result.AnyOf); + Assert.Equal(2, result.AllOf.Count); + } + [Fact] + public void DoesMergeWithIntersection() + { + var schema = new OpenApiSchema() + { + Type = "object", + AnyOf = [ + new() { + Properties = new Dictionary() { + ["one"] = new OpenApiSchema(), + }, + AllOf = [ + new() { + Type = "object", + Properties = new Dictionary() { + ["first"] = new OpenApiSchema(), + } + }, + new() { + Type = "object", + Properties = new Dictionary() { + ["second"] = new OpenApiSchema(), + } + }, + new() { + Type = "object", + Properties = new Dictionary() { + ["third"] = new OpenApiSchema(), + } + }, + ] + }, + ], + }; + + var result = schema.MergeSingleInclusiveUnionInheritanceOrIntersectionSchemaEntries(); + Assert.NotNull(result); + Assert.True(schema.AnyOf[0].IsIntersection()); + Assert.True(result.IsIntersection()); + Assert.Contains("one", result.Properties.Keys); + Assert.Empty(result.AnyOf); + Assert.Equal(3, result.AllOf.Count); + } + [Fact] + public void DoesNotMergeWithMoreThanOneInclusiveEntry() + { + var schema = new OpenApiSchema() + { + Type = "object", + AnyOf = [ + new() { + Properties = new Dictionary() { + ["one"] = new OpenApiSchema(), + }, + AllOf = [ + new() { + Reference = new() { + Id = "BaseClass" + }, + }, + new() { + Type = "object", + Properties = new Dictionary() { + ["firstName"] = new OpenApiSchema(), + ["lastName"] = new OpenApiSchema() + } + }, + ] + }, + new() { Type = "object" }, + ], + }; + + var result = schema.MergeSingleInclusiveUnionInheritanceOrIntersectionSchemaEntries(); + Assert.Null(result); + } + [Fact] + public void DoesNotMergeWithoutInheritanceOrIntersection() + { + var schema = new OpenApiSchema() + { + Type = "object", + AnyOf = [ + new() { + AllOf = [ + new() { + Type = "object", + Properties = new Dictionary() { + ["firstName"] = new OpenApiSchema(), + ["lastName"] = new OpenApiSchema() + } + }, + ] + }, + ], + }; + + var result = schema.MergeSingleInclusiveUnionInheritanceOrIntersectionSchemaEntries(); + Assert.Null(result); + } + } + + public class MergeSingleExclusiveUnionInheritanceOrIntersectionSchemaEntries + { + [Fact] + public void DoesMergeWithInheritance() + { + var schema = new OpenApiSchema() + { + Type = "object", + OneOf = [ + new() { + Properties = new Dictionary() { + ["one"] = new OpenApiSchema(), + }, + AllOf = [ + new() { + Reference = new() { + Id = "BaseClass" + }, + }, + new() { + Type = "object", + Properties = new Dictionary() { + ["firstName"] = new OpenApiSchema(), + ["lastName"] = new OpenApiSchema() + } + }, + ] + }, + ], + }; + + var result = schema.MergeSingleExclusiveUnionInheritanceOrIntersectionSchemaEntries(); + Assert.True(schema.OneOf[0].IsInherited()); + Assert.NotNull(result); + Assert.True(result.IsInherited()); + Assert.Contains("one", result.Properties.Keys); + Assert.Empty(result.OneOf); + Assert.Equal(2, result.AllOf.Count); + } + [Fact] + public void DoesMergeWithIntersection() + { + var schema = new OpenApiSchema() + { + Type = "object", + OneOf = [ + new() { + Properties = new Dictionary() { + ["one"] = new OpenApiSchema(), + }, + AllOf = [ + new() { + Type = "object", + Properties = new Dictionary() { + ["first"] = new OpenApiSchema(), + } + }, + new() { + Type = "object", + Properties = new Dictionary() { + ["second"] = new OpenApiSchema(), + } + }, + new() { + Type = "object", + Properties = new Dictionary() { + ["third"] = new OpenApiSchema(), + } + }, + ] + }, + ], + }; + + var result = schema.MergeSingleExclusiveUnionInheritanceOrIntersectionSchemaEntries(); + Assert.NotNull(result); + Assert.True(schema.OneOf[0].IsIntersection()); + Assert.True(result.IsIntersection()); + Assert.Contains("one", result.Properties.Keys); + Assert.Empty(result.OneOf); + Assert.Equal(3, result.AllOf.Count); + } + [Fact] + public void DoesNotMergeWithMoreThanOneExclusiveEntry() + { + var schema = new OpenApiSchema() + { + Type = "object", + OneOf = [ + new() { + Properties = new Dictionary() { + ["one"] = new OpenApiSchema(), + }, + AllOf = [ + new() { + Reference = new() { + Id = "BaseClass" + }, + }, + new() { + Type = "object", + Properties = new Dictionary() { + ["firstName"] = new OpenApiSchema(), + ["lastName"] = new OpenApiSchema() + } + }, + ] + }, + new() { Type = "object" }, + ], + }; + + var result = schema.MergeSingleExclusiveUnionInheritanceOrIntersectionSchemaEntries(); + Assert.Null(result); + } + [Fact] + public void DoesNotMergeWithoutInheritanceOrIntersection() + { + var schema = new OpenApiSchema() + { + Type = "object", + OneOf = [ + new() { + AllOf = [ + new() { + Type = "object", + Properties = new Dictionary() { + ["firstName"] = new OpenApiSchema(), + ["lastName"] = new OpenApiSchema() + } + }, + ] + }, + ], + }; + + var result = schema.MergeSingleExclusiveUnionInheritanceOrIntersectionSchemaEntries(); + Assert.Null(result); + } + } + [Fact] public void IsArrayFalseOnEmptyItems() {