Skip to content

Commit

Permalink
Merge pull request #1904 from microsoft/fixgeneration
Browse files Browse the repository at this point in the history
Follow up to #1901
  • Loading branch information
andrueastman authored Oct 18, 2022
2 parents a9a13b5 + 5bc1805 commit 1622aca
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Missing host/server is now considered a warning instead of a critical error. [#1896](https://github.com/microsoft/kiota/issues/1896)
- Fixed a bug where info and show commands would crash in case of invalid description URL. [#1894](https://github.com/microsoft/kiota/issues/1894)
- Show command now reads descriptions directly from APIs.guru instead of their origin. [#1897](https://github.com/microsoft/kiota/issues/1897)
- Fixed a classnames having the same name as extensions would cause generation to fail. [#1892](https://github.com/microsoft/kiota/issues/1892)

## [0.6.0] - 2022-10-06

Expand Down
16 changes: 8 additions & 8 deletions src/Kiota.Builder/Extensions/OpenApiUrlTreeNodeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public static string GetClassName(this OpenApiUrlTreeNode currentNode, HashSet<s
.SkipLast(1)
.Last()
.ToFirstCharacterUpperCase();

}
return (prefix +
rawClassName
?.Split('.', StringSplitOptions.RemoveEmptyEntries)
.Except(SegmentsToSkipForClassNames, StringComparer.OrdinalIgnoreCase)
.LastOrDefault() +
suffix)
.CleanupSymbolName();

var classNameSegments = rawClassName?.Split('.', StringSplitOptions.RemoveEmptyEntries).AsEnumerable() ?? Enumerable.Empty<string>();
// only apply the exceptions if we had multiple segments.
// Otherwise a single segment class name like `Json` will be returned as an empty string.
if (classNameSegments.Count() > 1)
classNameSegments = classNameSegments.Except(SegmentsToSkipForClassNames, StringComparer.OrdinalIgnoreCase);

return (prefix + classNameSegments.LastOrDefault() +suffix).CleanupSymbolName();
}
private static readonly HashSet<string> SegmentsToSkipForClassNames = new(6, StringComparer.OrdinalIgnoreCase) {
"json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,4 +230,68 @@ public void GetsClassNameWithIndexerAndExtension() {
var result = node.Children["reviews"].Children["{resource-type}.json"].GetClassName(new(){"application/json"});
Assert.Equal("ResourceType", result);
}
[Fact]
public void GetsClassNameWithSegmentsToSkipForClassNames() {
var doc = new OpenApiDocument
{
Paths = new(),
};
doc.Paths.Add("/reviews/{resource-type}.json", new()
{
Operations = new Dictionary<OperationType, OpenApiOperation> {
{
OperationType.Get, new() {
Parameters = new List<OpenApiParameter> {
new() {
Name = "resource-type",
In = ParameterLocation.Path,
Required = true,
Schema = new() {
Type = "string"
},
Style = ParameterStyle.Simple,
}
},
Responses = new OpenApiResponses()
{
{
"200", new()
{
Content = new Dictionary<string, OpenApiMediaType>()
{
{
"application/json", new()
{
Schema = new ()
{
Type = "object",
Title = "json",
Reference = new OpenApiReference()
{
Id = "microsoft.graph.json"
}
}
}
}
}
}
}
}
}
}
}
});

var node = OpenApiUrlTreeNode.Create(doc, Label);
var result = node.Children["reviews"].Children["{resource-type}.json"].GetClassName(new(){"application/json"});
Assert.Equal("ResourceType", result);

// Get the responseSchema with a type "microsoft.graph.json"
var responseSchema = node.Children["reviews"].Children["{resource-type}.json"].PathItems["default"].Operations[0].Responses["200"].Content["application/json"].Schema;
var responseClassName = node.Children["reviews"].Children["{resource-type}.json"]
.GetClassName(new() { "application/json" },schema: responseSchema);

// validate that we get a valid class name
Assert.Equal("Json",responseClassName);
}
}

0 comments on commit 1622aca

Please sign in to comment.