From 266227f4e7905bfea585cbc1022068f39b612ba7 Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Tue, 8 Oct 2024 14:56:06 +0300 Subject: [PATCH] fixes incompatibility with new odata primitive format. --- CHANGELOG.md | 1 + .../Extensions/OpenApiSchemaExtensions.cs | 18 +++++- .../OpenApiSchemaExtensionsTests.cs | 62 +++++++++++++++++++ 3 files changed, 80 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 123721dca8..e32ee9597e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Fixed incompatibility with detecting odata primitives after conversion library updates at [OpenAPI.NET.OData#581](https://github.com/microsoft/OpenAPI.NET.OData/issues/581); - Fixed cyclic dependencies in generated Go code. [#2834](https://github.com/microsoft/kiota/issues/2834) - Fixed a bug where default output folder is created on plugin edit and generate commands. [#5510](https://github.com/microsoft/kiota/issues/5429) diff --git a/src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs b/src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs index 375d4f6ae7..c5ed4b24e9 100644 --- a/src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs +++ b/src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs @@ -132,7 +132,7 @@ public static bool IsExclusiveUnion(this OpenApiSchema? schema) "number", "integer", }; - public static bool IsODataPrimitiveType(this OpenApiSchema schema) + private static bool IsODataPrimitiveTypeBackwardCompatible(this OpenApiSchema schema) { return schema.IsExclusiveUnion() && schema.OneOf.Count == 3 && @@ -146,6 +146,22 @@ public static bool IsODataPrimitiveType(this OpenApiSchema schema) schema.AnyOf.Count(static x => oDataTypes.Contains(x.Type)) == 1 && schema.AnyOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase)) == 1; } + public static bool IsODataPrimitiveType(this OpenApiSchema schema) + { + return schema.IsExclusiveUnion() && + schema.OneOf.Count == 3 && + schema.OneOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase) && (x.Enum?.Any() ?? false)) == 1 && + schema.OneOf.Count(static x => oDataTypes.Contains(x.Type)) == 1 && + schema.OneOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase)) == 2 + || + schema.IsInclusiveUnion() && + schema.AnyOf.Count == 3 && + schema.AnyOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase) && (x.Enum?.Any() ?? false)) == 1 && + schema.AnyOf.Count(static x => oDataTypes.Contains(x.Type)) == 1 && + schema.AnyOf.Count(static x => "string".Equals(x.Type, StringComparison.OrdinalIgnoreCase)) == 2 + || + schema.IsODataPrimitiveTypeBackwardCompatible(); + } public static bool IsEnum(this OpenApiSchema schema) { if (schema is null) return false; diff --git a/tests/Kiota.Builder.Tests/Extensions/OpenApiSchemaExtensionsTests.cs b/tests/Kiota.Builder.Tests/Extensions/OpenApiSchemaExtensionsTests.cs index 08d46a13c7..d0c7a81f9f 100644 --- a/tests/Kiota.Builder.Tests/Extensions/OpenApiSchemaExtensionsTests.cs +++ b/tests/Kiota.Builder.Tests/Extensions/OpenApiSchemaExtensionsTests.cs @@ -785,4 +785,66 @@ public void IsEnumDoesNotMaskUnions() }; Assert.False(schema.IsEnum()); } + [Fact] + public void IsOdataPrimitive() + { + var schema = new OpenApiSchema + { + OneOf = new List + { + new () + { + Type = "number", + Format = "double", + Nullable = true + }, + new () + { + Type = "string", + Nullable = true + }, + new () + { + Enum = new List() + { + new OpenApiString("INF"), + new OpenApiString("INF"), + new OpenApiString("NaN"), + }, + Type = "string", + Nullable = true + } + } + }; + Assert.True(schema.IsODataPrimitiveType()); + } + [Fact] + public void IsOdataPrimitiveBackwardCompatible() + { + var schema = new OpenApiSchema + { + OneOf = new List + { + new () + { + Type = "number", + Format = "double", + }, + new () + { + Type = "string", + }, + new () + { + Enum = new List() + { + new OpenApiString("INF"), + new OpenApiString("INF"), + new OpenApiString("NaN"), + } + } + } + }; + Assert.True(schema.IsODataPrimitiveType()); + } }