Skip to content

Commit

Permalink
Merge pull request #185 from microsoft/bugfix/null-ref-exp-schema
Browse files Browse the repository at this point in the history
fixes a bug where a null schema would make the generation process derail
  • Loading branch information
baywet authored May 31, 2021
2 parents 699a3d9 + 386219f commit b0ffef8
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 29 deletions.
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

0 comments on commit b0ffef8

Please sign in to comment.