From 9767b3ff16673faf0b793b47c58160bd47dcb1fe Mon Sep 17 00:00:00 2001 From: Andrew Omondi Date: Mon, 9 Dec 2024 12:23:30 +0300 Subject: [PATCH] fix: fixes bug in generation of clients with primitive referenced in allof --- CHANGELOG.md | 2 ++ .../Extensions/OpenApiSchemaExtensions.cs | 6 ++++- .../OpenApiSchemaExtensionsTests.cs | 26 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a89c897ab..9be682ff28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- Fixed a bug in generation when a referenced schema in an allOf was a primitive [#5701](https://github.com/microsoft/kiota/issues/5701). + ## [1.21.0] - 2024-12-05 ### Added diff --git a/src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs b/src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs index 28774211e4..cec1a3cf2b 100644 --- a/src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs +++ b/src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs @@ -77,7 +77,11 @@ public static bool IsInclusiveUnion(this OpenApiSchema? schema, uint exclusiveMi public static bool IsInherited(this OpenApiSchema? schema) { if (schema is null) return false; - var meaningfulMemberSchemas = schema.AllOf.FlattenSchemaIfRequired(static x => x.AllOf).Where(static x => x.IsSemanticallyMeaningful(ignoreEnums: true, ignoreArrays: true, ignoreType: true)).ToArray(); + var meaningfulMemberSchemas = schema.AllOf.FlattenSchemaIfRequired(static x => x.AllOf) + .Where(static x => x.IsSemanticallyMeaningful(ignoreEnums: true, ignoreArrays: true, ignoreType: true)) + // the next line ensures the meaningful schema are objects as it won't make sense inheriting from a primitive despite it being meaningful. + .Where(static x => string.IsNullOrEmpty(x.Reference?.Id) || string.IsNullOrEmpty(x.Type) || "object".Equals(x.Type, StringComparison.OrdinalIgnoreCase)) + .ToArray(); var isRootSchemaMeaningful = schema.IsSemanticallyMeaningful(ignoreEnums: true, ignoreArrays: true, ignoreType: true); return meaningfulMemberSchemas.Count(static x => !string.IsNullOrEmpty(x.Reference?.Id)) == 1 && (meaningfulMemberSchemas.Count(static x => string.IsNullOrEmpty(x.Reference?.Id)) == 1 || diff --git a/tests/Kiota.Builder.Tests/Extensions/OpenApiSchemaExtensionsTests.cs b/tests/Kiota.Builder.Tests/Extensions/OpenApiSchemaExtensionsTests.cs index 800a449504..6ca611be28 100644 --- a/tests/Kiota.Builder.Tests/Extensions/OpenApiSchemaExtensionsTests.cs +++ b/tests/Kiota.Builder.Tests/Extensions/OpenApiSchemaExtensionsTests.cs @@ -567,6 +567,32 @@ public void IsIntersection() }; Assert.False(schema.IsInherited()); Assert.False(schema.IsIntersection()); + + schema = new OpenApiSchema + { + Title = "Trader Id", + AllOf = new List { + new () + { + Title = "UserId", + Description = "unique identifier", + Type = "string", + Pattern = "^[1-9][0-9]*$", + Example = new OpenApiString("1323232"), + Reference = new OpenApiReference + { + Id = "UserId" // This property makes the schema "meaningful" + } + } + }, + Reference = new OpenApiReference + { + Id = "TraderId" // This property makes the schema "meaningful" + } + }; + + Assert.False(schema.IsInherited()); + Assert.False(schema.IsIntersection()); } [Fact] public void MergesIntersection()