Skip to content

Commit

Permalink
Merge pull request microsoft#4723 from microsoft/bugfix/missing-mushing
Browse files Browse the repository at this point in the history
fix: adds missing mushing of all of scenarios
  • Loading branch information
andrueastman authored May 28, 2024
2 parents c3406e3 + a6feb0e commit a1f1276
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 89 deletions.
8 changes: 8 additions & 0 deletions src/Kiota.Builder/CodeDOM/CodeClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public CodeComposedTypeBase? OriginalComposedType
{
get; set;
}
public string GetComponentSchemaName(CodeNamespace modelsNamespace)
{
if (Kind is not CodeClassKind.Model ||
Parent is not CodeNamespace parentNamespace ||
!parentNamespace.IsChildOf(modelsNamespace))
return string.Empty;
return $"{parentNamespace.Name[(modelsNamespace.Name.Length + 1)..]}.{Name}";
}
public CodeIndexer? Indexer => InnerChildElements.Values.OfType<CodeIndexer>().FirstOrDefault(static x => !x.IsLegacyIndexer);
public void AddIndexer(params CodeIndexer[] indexers)
{
Expand Down
35 changes: 26 additions & 9 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 && !schemas[0].HasAnyProperty() ?
return schemas.Count == 1 && !schemas[0].HasAnyProperty() && string.IsNullOrEmpty(schemas[0].Reference?.Id) ?
schemas.FlattenEmptyEntries(subsequentGetter, 1) :
schemas;
}
Expand Down Expand Up @@ -84,19 +84,36 @@ public static bool IsInherited(this OpenApiSchema? schema)
isRootSchemaMeaningful);
}

internal static OpenApiSchema? MergeIntersectionSchemaEntries(this OpenApiSchema? schema, HashSet<OpenApiSchema>? schemasToExclude = default)
internal static OpenApiSchema? MergeAllOfSchemaEntries(this OpenApiSchema? schema, HashSet<OpenApiSchema>? schemasToExclude = default, Func<OpenApiSchema, bool>? filter = default)
{
return schema.MergeIntersectionSchemaEntries(schemasToExclude, true, filter);
}

internal static OpenApiSchema? MergeIntersectionSchemaEntries(this OpenApiSchema? schema, HashSet<OpenApiSchema>? schemasToExclude = default, bool overrideIntersection = false, Func<OpenApiSchema, bool>? filter = default)
{
if (schema is null) return null;
if (!schema.IsIntersection()) return schema;
if (!schema.IsIntersection() && !overrideIntersection) return schema;
var result = new OpenApiSchema(schema);
result.AllOf.Clear();
var meaningfulSchemas = schema.AllOf
.Where(static x => x.IsSemanticallyMeaningful() || x.AllOf.Any())
.Select(x => MergeIntersectionSchemaEntries(x, schemasToExclude))
.Where(x => (x.IsSemanticallyMeaningful() || x.AllOf.Any()) && (filter == null || filter(x)))
.Select(x => MergeIntersectionSchemaEntries(x, schemasToExclude, overrideIntersection, filter))
.Where(x => x is not null && (schemasToExclude is null || !schemasToExclude.Contains(x)))
.OfType<OpenApiSchema>()
.ToArray();
meaningfulSchemas.FlattenEmptyEntries(static x => x.AllOf).Union(meaningfulSchemas).SelectMany(static x => x.Properties).ToList().ForEach(x => result.Properties.TryAdd(x.Key, x.Value));
var entriesToMerge = meaningfulSchemas.FlattenEmptyEntries(static x => x.AllOf).Union(meaningfulSchemas).ToArray();
if (entriesToMerge.Select(static x => x.Discriminator).OfType<OpenApiDiscriminator>().FirstOrDefault() is OpenApiDiscriminator discriminator)
if (result.Discriminator is null)
result.Discriminator = discriminator;
else if (string.IsNullOrEmpty(result.Discriminator.PropertyName) && !string.IsNullOrEmpty(discriminator.PropertyName))
result.Discriminator.PropertyName = discriminator.PropertyName;
else if (discriminator.Mapping?.Any() ?? false)
result.Discriminator.Mapping = discriminator.Mapping.ToDictionary(static x => x.Key, static x => x.Value);

foreach (var propertyToMerge in entriesToMerge.SelectMany(static x => x.Properties))
{
result.Properties.TryAdd(propertyToMerge.Key, propertyToMerge.Value);
}
return result;
}

Expand Down Expand Up @@ -225,8 +242,8 @@ internal static string GetDiscriminatorPropertyName(this OpenApiSchema schema)
return oneOfDiscriminatorPropertyName;
if (schema.AnyOf.Select(GetDiscriminatorPropertyName).FirstOrDefault(static x => !string.IsNullOrEmpty(x)) is string anyOfDiscriminatorPropertyName)
return anyOfDiscriminatorPropertyName;
if (schema.AllOf.Any())
return GetDiscriminatorPropertyName(schema.AllOf[^1]);
if (schema.AllOf.Select(GetDiscriminatorPropertyName).FirstOrDefault(static x => !string.IsNullOrEmpty(x)) is string allOfDiscriminatorPropertyName)
return allOfDiscriminatorPropertyName;

return string.Empty;
}
Expand Down Expand Up @@ -260,7 +277,7 @@ private static IEnumerable<string> GetAllInheritanceSchemaReferences(string curr
ArgumentNullException.ThrowIfNull(inheritanceIndex);
if (inheritanceIndex.TryGetValue(currentReferenceId, out var dependents))
return dependents.Keys.Union(dependents.Keys.SelectMany(x => GetAllInheritanceSchemaReferences(x, inheritanceIndex))).Distinct(StringComparer.OrdinalIgnoreCase);
return Enumerable.Empty<string>();
return [];
}
}

Loading

0 comments on commit a1f1276

Please sign in to comment.