Skip to content

Commit

Permalink
Merge pull request #5828 from microsoft/fix/discriminator-infinite-lo…
Browse files Browse the repository at this point in the history
…okup

fix: infinite lookup for discriminator
  • Loading branch information
baywet authored Nov 27, 2024
2 parents efac0c1 + 9df3378 commit 443e2a4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed Python error when a class inherits from a base class and implements an interface. [5637](https://github.com/microsoft/kiota/issues/5637)
- Fix anyOf/oneOf generation in TypeScript. [5353](https://github.com/microsoft/kiota/issues/5353)
- Fixed invalid code in Php caused by "*/*/" in property description. [5635](https://github.com/microsoft/kiota/issues/5635)
- Fixed a bug where discriminator property name lookup could end up in an infinite loop. [#5771](https://github.com/microsoft/kiota/issues/5771)
- Fixed TypeScript generation error when generating usings from shaken serializers. [#5634](https://github.com/microsoft/kiota/issues/5634)

## [1.20.0] - 2024-11-07
Expand Down
14 changes: 10 additions & 4 deletions src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,19 +246,25 @@ private static IEnumerable<OpenApiSchema> FlattenEmptyEntries(this IEnumerable<O
}
return result;
}
internal static string GetDiscriminatorPropertyName(this OpenApiSchema schema)
internal static string GetDiscriminatorPropertyName(this OpenApiSchema schema, HashSet<OpenApiSchema>? visitedSchemas = default)
{

if (schema == null)
return string.Empty;

visitedSchemas ??= [];
if (visitedSchemas.Contains(schema))
return string.Empty;
visitedSchemas.Add(schema);

if (!string.IsNullOrEmpty(schema.Discriminator?.PropertyName))
return schema.Discriminator.PropertyName;

if (schema.OneOf.Select(GetDiscriminatorPropertyName).FirstOrDefault(static x => !string.IsNullOrEmpty(x)) is string oneOfDiscriminatorPropertyName)
if (schema.OneOf.Select(x => x.GetDiscriminatorPropertyName(visitedSchemas)).FirstOrDefault(static x => !string.IsNullOrEmpty(x)) is string oneOfDiscriminatorPropertyName)
return oneOfDiscriminatorPropertyName;
if (schema.AnyOf.Select(GetDiscriminatorPropertyName).FirstOrDefault(static x => !string.IsNullOrEmpty(x)) is string anyOfDiscriminatorPropertyName)
if (schema.AnyOf.Select(x => x.GetDiscriminatorPropertyName(visitedSchemas)).FirstOrDefault(static x => !string.IsNullOrEmpty(x)) is string anyOfDiscriminatorPropertyName)
return anyOfDiscriminatorPropertyName;
if (schema.AllOf.Select(GetDiscriminatorPropertyName).FirstOrDefault(static x => !string.IsNullOrEmpty(x)) is string allOfDiscriminatorPropertyName)
if (schema.AllOf.Select(x => x.GetDiscriminatorPropertyName(visitedSchemas)).FirstOrDefault(static x => !string.IsNullOrEmpty(x)) is string allOfDiscriminatorPropertyName)
return allOfDiscriminatorPropertyName;

return string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,4 +847,63 @@ public void IsOdataPrimitiveBackwardCompatible()
};
Assert.True(schema.IsODataPrimitiveType());
}
[Fact]
public void ReturnsEmptyPropertyNameOnCircularReferences()
{
var entitySchema = new OpenApiSchema
{
Reference = new OpenApiReference
{
Id = "microsoft.graph.entity"
},
Properties = new Dictionary<string, OpenApiSchema>
{
["id"] = new OpenApiSchema
{
Reference = new OpenApiReference
{
Id = "microsoft.graph.entity"
}
}
}
};
var userSchema = new OpenApiSchema
{
Reference = new OpenApiReference
{
Id = "microsoft.graph.user"
},
OneOf =
[
entitySchema,
new OpenApiSchema
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>
{
["firstName"] = new OpenApiSchema
{
Reference = new OpenApiReference
{
Id = "microsoft.graph.entity"
}
}
}
}
],
Discriminator = new OpenApiDiscriminator
{
Mapping = new Dictionary<string, string>
{
["microsoft.graph.entity"] = "entity",
["microsoft.graph.user"] = "user"
}
}
};
entitySchema.AllOf =
[
userSchema
];
Assert.Empty(userSchema.GetDiscriminatorPropertyName());
}
}

0 comments on commit 443e2a4

Please sign in to comment.