From d32ec681cbe9f289e1ed22ac5b5185f22c3927e9 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Thu, 2 May 2024 14:56:32 -0400 Subject: [PATCH] - fixes inheritance trimming Signed-off-by: Vincent Biret --- .../Extensions/OpenApiSchemaExtensions.cs | 13 +++++++------ src/Kiota.Builder/KiotaBuilder.cs | 9 ++++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs b/src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs index d1e647a223..2cf9b5eb75 100644 --- a/src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs +++ b/src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs @@ -29,7 +29,7 @@ public static IEnumerable GetSchemaNames(this OpenApiSchema schema, bool internal static IEnumerable FlattenSchemaIfRequired(this IList schemas, Func> subsequentGetter) { if (schemas is null) return []; - return schemas.Count == 1 ? + return schemas.Count == 1 && !schemas[0].Properties.Any() ? schemas.FlattenEmptyEntries(subsequentGetter, 1) : schemas; } @@ -57,7 +57,7 @@ public static bool IsArray(this OpenApiSchema? schema) (schema.Items.IsComposedEnum() || schema.Items.IsEnum() || schema.Items.IsSemanticallyMeaningful() || - FlattenEmptyEntries(new OpenApiSchema[] { schema.Items }, static x => x.AnyOf.Union(x.AllOf).Union(x.OneOf).ToList(), 1).FirstOrDefault() is OpenApiSchema flat && flat.IsSemanticallyMeaningful()); + FlattenEmptyEntries([schema.Items], static x => x.AnyOf.Union(x.AllOf).Union(x.OneOf).ToList(), 1).FirstOrDefault() is OpenApiSchema flat && flat.IsSemanticallyMeaningful()); } public static bool IsObject(this OpenApiSchema? schema) @@ -73,10 +73,11 @@ public static bool IsInclusiveUnion(this OpenApiSchema? schema) public static bool IsInherited(this OpenApiSchema? schema) { if (schema is null) return false; - var meaningfulSchemas = schema.AllOf.FlattenSchemaIfRequired(static x => x.AllOf).Where(static x => x.IsSemanticallyMeaningful()).ToArray(); - return meaningfulSchemas.Count(static x => !string.IsNullOrEmpty(x.Reference?.Id)) == 1 && - (meaningfulSchemas.Count(static x => string.IsNullOrEmpty(x.Reference?.Id)) == 1 || - schema.IsSemanticallyMeaningful()); + var meaningfulMemberSchemas = schema.AllOf.FlattenSchemaIfRequired(static x => x.AllOf).Where(static x => x.IsSemanticallyMeaningful()).ToArray(); + var isRootSchemaMeaningful = schema.IsSemanticallyMeaningful(); + return meaningfulMemberSchemas.Count(static x => !string.IsNullOrEmpty(x.Reference?.Id)) == 1 && + (meaningfulMemberSchemas.Count(static x => string.IsNullOrEmpty(x.Reference?.Id)) == 1 || + isRootSchemaMeaningful); } internal static OpenApiSchema? MergeIntersectionSchemaEntries(this OpenApiSchema? schema) diff --git a/src/Kiota.Builder/KiotaBuilder.cs b/src/Kiota.Builder/KiotaBuilder.cs index cdd8849783..55d2399e12 100644 --- a/src/Kiota.Builder/KiotaBuilder.cs +++ b/src/Kiota.Builder/KiotaBuilder.cs @@ -1550,8 +1550,11 @@ private CodeType CreateModelDeclarationAndType(OpenApiUrlTreeNode currentNode, O } private CodeType CreateInheritedModelDeclaration(OpenApiUrlTreeNode currentNode, OpenApiSchema schema, OpenApiOperation? operation, string classNameSuffix, CodeNamespace codeNamespace, bool isRequestBody, string typeNameForInlineSchema) { - var rootSchemaIsMeaningful = schema.IsSemanticallyMeaningful(); - var allOfs = (rootSchemaIsMeaningful ? new OpenApiSchema[] { schema } : []).Union(schema.AllOf.FlattenSchemaIfRequired(static x => x.AllOf)); + var isRootSchemaMeaningful = schema.IsSemanticallyMeaningful(); + var allOfs = schema.AllOf.FlattenSchemaIfRequired(static x => x.AllOf) + .Union(isRootSchemaMeaningful ? + [schema] : + Array.Empty()); CodeElement? codeDeclaration = null; var codeNamespaceFromParent = GetShortestNamespace(codeNamespace, schema); foreach (var currentSchema in allOfs) @@ -1559,7 +1562,7 @@ private CodeType CreateInheritedModelDeclaration(OpenApiUrlTreeNode currentNode, var referenceId = GetReferenceIdFromOriginalSchema(currentSchema, schema); var shortestNamespaceName = GetModelsNamespaceNameFromReferenceId(referenceId); var shortestNamespace = string.IsNullOrEmpty(referenceId) ? codeNamespaceFromParent : rootNamespace?.FindOrAddNamespace(shortestNamespaceName); - var className = (currentSchema.GetSchemaName(rootSchemaIsMeaningful && currentSchema == schema) is string cName && !string.IsNullOrEmpty(cName) ? + var className = (currentSchema.GetSchemaName(isRootSchemaMeaningful && currentSchema == schema) is string cName && !string.IsNullOrEmpty(cName) ? cName : (!string.IsNullOrEmpty(typeNameForInlineSchema) ? typeNameForInlineSchema :