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

fixes a bug where a null schema would make the generation process derail #185

Merged
merged 3 commits into from
May 31, 2021
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
18 changes: 9 additions & 9 deletions src/Kiota.Builder/Extensions/OpenApiSchemaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ public static string GetSchemaTitle(this OpenApiSchema schema) {
public static IEnumerable<string> GetSchemaReferenceIds(this OpenApiSchema schema, HashSet<OpenApiSchema> visitedSchemas = null) {
if(visitedSchemas == null)
visitedSchemas = new();
if(!visitedSchemas.Contains(schema)) {
if(schema != null && !visitedSchemas.Contains(schema)) {
visitedSchemas.Add(schema);
var result = new List<string>();
if(!string.IsNullOrEmpty(schema.Reference?.Id))
result.Add(schema.Reference.Id);
if(!string.IsNullOrEmpty(schema.Items?.Reference?.Id))
result.Add(schema.Items.Reference.Id);
if(schema.Properties != null)
result.AddRange(schema.Properties.Values.SelectMany(x => x.GetSchemaReferenceIds(visitedSchemas)));
if(schema.AnyOf != null)
result.AddRange(schema.AnyOf.SelectMany(x => x.GetSchemaReferenceIds(visitedSchemas)));
if(schema.AllOf != null)
result.AddRange(schema.AllOf.SelectMany(x => x.GetSchemaReferenceIds(visitedSchemas)));
if(schema.OneOf != null)
result.AddRange(schema.OneOf.SelectMany(x => x.GetSchemaReferenceIds(visitedSchemas)));
var subSchemaReferences = (schema.Properties?.Values ?? Enumerable.Empty<OpenApiSchema>())
.Union(schema.AnyOf ?? Enumerable.Empty<OpenApiSchema>())
.Union(schema.AllOf ?? Enumerable.Empty<OpenApiSchema>())
.Union(schema.OneOf ?? Enumerable.Empty<OpenApiSchema>())
.SelectMany(x => x.GetSchemaReferenceIds(visitedSchemas))
.ToList();// this to list is important otherwise the any marks the schemas as visited and add range doesn't find anything
if(subSchemaReferences.Any())
result.AddRange(subSchemaReferences);
return result;
} else
return Enumerable.Empty<string>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@

namespace Kiota.Builder.Extensions.Tests {
public class OpenApiSchemaExtensionsTests {
[Fact]
public void Defensive() {
Assert.Empty(OpenApiSchemaExtensions.GetSchemaReferenceIds(null));
var schema = new OpenApiSchema{
AnyOf = null
};
Assert.Null(schema.AnyOf);
Assert.Empty(schema.GetSchemaReferenceIds());
schema = new() {
AllOf = null
};
Assert.Null(schema.AllOf);
Assert.Empty(schema.GetSchemaReferenceIds());
schema = new() {
OneOf = null
};
Assert.Null(schema.OneOf);
Assert.Empty(schema.GetSchemaReferenceIds());
schema = new() {
Properties = null
};
Assert.Null(schema.Properties);
Assert.Empty(schema.GetSchemaReferenceIds());
}
[Fact]
public void GetSchemaTitleAllOf() {
var schema = new OpenApiSchema {
Expand All @@ -18,8 +42,8 @@ public void GetSchemaTitleAllOf() {
}
};
var names = schema.GetSchemaTitles();
Assert.Equal("microsoft.graph.entity", names.First());
Assert.Equal("microsoft.graph.user", names.Last());
Assert.Contains("microsoft.graph.entity", names);
Assert.Contains("microsoft.graph.user", names);
Assert.Equal("microsoft.graph.user", schema.GetSchemaTitle());
}
[Fact]
Expand All @@ -39,8 +63,8 @@ public void GetSchemaTitleAllOfNested() {
}
};
var names = schema.GetSchemaTitles();
Assert.Equal("microsoft.graph.entity", names.First());
Assert.Equal("microsoft.graph.user", names.Last());
Assert.Contains("microsoft.graph.entity", names);
Assert.Contains("microsoft.graph.user", names);
Assert.Equal("microsoft.graph.user", schema.GetSchemaTitle());
}
[Fact]
Expand All @@ -56,8 +80,8 @@ public void GetSchemaTitleAnyOf() {
}
};
var names = schema.GetSchemaTitles();
Assert.Equal("microsoft.graph.entity", names.First());
Assert.Equal("microsoft.graph.user", names.Last());
Assert.Contains("microsoft.graph.entity", names);
Assert.Contains("microsoft.graph.user", names);
Assert.Equal("microsoft.graph.user", schema.GetSchemaTitle());
}
[Fact]
Expand All @@ -73,8 +97,8 @@ public void GetSchemaTitleOneOf() {
}
};
var names = schema.GetSchemaTitles();
Assert.Equal("microsoft.graph.entity", names.First());
Assert.Equal("microsoft.graph.user", names.Last());
Assert.Contains("microsoft.graph.entity", names);
Assert.Contains("microsoft.graph.user", names);
Assert.Equal("microsoft.graph.user", schema.GetSchemaTitle());
}
[Fact]
Expand All @@ -85,7 +109,7 @@ public void GetSchemaTitleItems() {
},
};
var names = schema.GetSchemaTitles();
Assert.Equal("microsoft.graph.entity", names.First());
Assert.Contains("microsoft.graph.entity", names);
Assert.Equal("microsoft.graph.entity", schema.GetSchemaTitle());
Assert.Single(names);
}
Expand All @@ -95,7 +119,7 @@ public void GetSchemaTitleTitle() {
Title = "microsoft.graph.entity"
};
var names = schema.GetSchemaTitles();
Assert.Equal("microsoft.graph.entity", names.First());
Assert.Contains("microsoft.graph.entity", names);
Assert.Equal("microsoft.graph.entity", schema.GetSchemaTitle());
Assert.Single(names);
}
Expand Down Expand Up @@ -124,8 +148,8 @@ public void GetReferenceIdsAllOf() {
}
};
var names = schema.GetSchemaReferenceIds();
Assert.Equal("microsoft.graph.entity", names.First());
Assert.Equal("microsoft.graph.user", names.Last());
Assert.Contains("microsoft.graph.entity", names);
Assert.Contains("microsoft.graph.user", names);
}
[Fact]
public void GetReferenceIdsAllOfNested() {
Expand All @@ -148,8 +172,8 @@ public void GetReferenceIdsAllOfNested() {
}
};
var names = schema.GetSchemaReferenceIds();
Assert.Equal("microsoft.graph.entity", names.First());
Assert.Equal("microsoft.graph.user", names.Last());
Assert.Contains("microsoft.graph.entity", names);
Assert.Contains("microsoft.graph.user", names);
}
[Fact]
public void GetReferenceIdsAnyOf() {
Expand All @@ -168,8 +192,8 @@ public void GetReferenceIdsAnyOf() {
}
};
var names = schema.GetSchemaReferenceIds();
Assert.Equal("microsoft.graph.entity", names.First());
Assert.Equal("microsoft.graph.user", names.Last());
Assert.Contains("microsoft.graph.entity", names);
Assert.Contains("microsoft.graph.user", names);
}
[Fact]
public void GetReferenceIdsOneOf() {
Expand All @@ -188,8 +212,8 @@ public void GetReferenceIdsOneOf() {
}
};
var names = schema.GetSchemaReferenceIds();
Assert.Equal("microsoft.graph.entity", names.First());
Assert.Equal("microsoft.graph.user", names.Last());
Assert.Contains("microsoft.graph.entity", names);
Assert.Contains("microsoft.graph.user", names);
}
[Fact]
public void GetReferenceIdsItems() {
Expand All @@ -201,7 +225,7 @@ public void GetReferenceIdsItems() {
},
};
var names = schema.GetSchemaReferenceIds();
Assert.Equal("microsoft.graph.entity", names.First());
Assert.Contains("microsoft.graph.entity", names);
Assert.Single(names);
}
[Fact]
Expand All @@ -212,7 +236,7 @@ public void GetReferenceIdsTitle() {
}
};
var names = schema.GetSchemaReferenceIds();
Assert.Equal("microsoft.graph.entity", names.First());
Assert.Contains("microsoft.graph.entity", names);
Assert.Single(names);
}
[Fact]
Expand Down