Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: adds missing mushing of all of scenarios #4723

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
{
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 Expand Up @@ -72,7 +80,7 @@
}
public override IEnumerable<CodeProperty> AddProperty(params CodeProperty[] properties)
{
if (properties == null || properties.Any(static x => x == null))

Check warning on line 83 in src/Kiota.Builder/CodeDOM/CodeClass.cs

View workflow job for this annotation

GitHub Actions / Build

Collection-specific "Exists" method should be used instead of the "Any" extension. (https://rules.sonarsource.com/csharp/RSPEC-6605)
throw new ArgumentNullException(nameof(properties));
if (properties.Length == 0)
throw new ArgumentOutOfRangeException(nameof(properties));
Expand Down
33 changes: 25 additions & 8 deletions src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs
Original file line number Diff line number Diff line change
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
Loading