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 all 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 @@ 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 @@
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 @@
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)

Check warning on line 92 in src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
{
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 @@
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 @@
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