Skip to content

Commit

Permalink
- fixes inheritance trimming
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed May 7, 2024
1 parent c1a0403 commit d32ec68
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
13 changes: 7 additions & 6 deletions src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static IEnumerable<string> GetSchemaNames(this OpenApiSchema schema, bool
internal static IEnumerable<OpenApiSchema> FlattenSchemaIfRequired(this IList<OpenApiSchema> schemas, Func<OpenApiSchema, IList<OpenApiSchema>> subsequentGetter)
{
if (schemas is null) return [];
return schemas.Count == 1 ?
return schemas.Count == 1 && !schemas[0].Properties.Any() ?
schemas.FlattenEmptyEntries(subsequentGetter, 1) :
schemas;
}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions src/Kiota.Builder/KiotaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1550,16 +1550,19 @@ 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<OpenApiSchema>());
CodeElement? codeDeclaration = null;
var codeNamespaceFromParent = GetShortestNamespace(codeNamespace, schema);
foreach (var currentSchema in allOfs)
{
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 :
Expand Down

0 comments on commit d32ec68

Please sign in to comment.