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

replaces custom url tree node by open API provided class #179

Merged
merged 6 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Expands code coverage to 88% #147
- Removes json assumption for request body to support multiple formats #170
- Escapes language reserved keywords #184
- Replaces custom URL tree node by class provided by OpenAPI.net #179

## [0.0.4] - 2021-04-28

Expand Down
5 changes: 5 additions & 0 deletions src/Kiota.Builder/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
namespace Kiota.Builder {
public static class Constants {
public const string DefaultOpenApiLabel = "default";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
using System.Linq;
using System.Text.RegularExpressions;
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Services;

namespace Kiota.Builder.Extensions {
public static class OpenApiUrlSpaceNodeExtensions {
public static class OpenApiUrlTreeNodeExtensions {

// where component id and the value is the set of openapiurlNode referencing it
public static Dictionary<string, HashSet<OpenApiUrlSpaceNode>> GetComponentsReferenceIndex(this OpenApiUrlSpaceNode rootNode) {
var result = new Dictionary<string, HashSet<OpenApiUrlSpaceNode>>(StringComparer.OrdinalIgnoreCase);
AddAllPathsEntries(rootNode, result);
public static Dictionary<string, HashSet<OpenApiUrlTreeNode>> GetComponentsReferenceIndex(this OpenApiUrlTreeNode rootNode, string label) {
var result = new Dictionary<string, HashSet<OpenApiUrlTreeNode>>(StringComparer.OrdinalIgnoreCase);
AddAllPathsEntries(rootNode, result, label);
return result;
}
private static void AddAllPathsEntries(OpenApiUrlSpaceNode currentNode, Dictionary<string, HashSet<OpenApiUrlSpaceNode>> index) {
if(currentNode == null)
private static void AddAllPathsEntries(OpenApiUrlTreeNode currentNode, Dictionary<string, HashSet<OpenApiUrlTreeNode>> index, string label) {
if(currentNode == null || string.IsNullOrEmpty(label))
return;

if(currentNode.PathItem != null && currentNode.HasOperations()) {
var nodeOperations = currentNode.PathItem.Operations.Values;
if(currentNode.PathItems.ContainsKey(label) && currentNode.HasOperations(label)) {
var nodeOperations = currentNode.PathItems[label].Operations.Values;
var requestSchemasFirstLevel = nodeOperations.SelectMany(x => x.RequestBody?.Content?.Values?.Select(y => y.Schema) ?? Enumerable.Empty<OpenApiSchema>());
var responseSchemasFirstLevel = nodeOperations.SelectMany(x =>
x?.Responses?.Values?.SelectMany(y =>
Expand All @@ -35,12 +36,13 @@ private static void AddAllPathsEntries(OpenApiUrlSpaceNode currentNode, Dictiona

if(currentNode.Children != null)
foreach(var child in currentNode.Children.Values)
AddAllPathsEntries(child, index);
AddAllPathsEntries(child, index, label);
}
internal static string GetNodeNamespaceFromPath(this OpenApiUrlSpaceNode currentNode, string prefix) =>
public static string GetNodeNamespaceFromPath(this OpenApiUrlTreeNode currentNode, string prefix) =>
prefix +
((currentNode?.Path?.Contains(pathNameSeparator) ?? false) ?
"." + currentNode?.Path
(string.IsNullOrEmpty(prefix) ? string.Empty : ".")
+ currentNode?.Path
?.Split(pathNameSeparator, StringSplitOptions.RemoveEmptyEntries)
?.Where(x => !x.StartsWith('{'))
?.Aggregate((x, y) => $"{x}.{y}") :
Expand All @@ -51,25 +53,30 @@ internal static string GetNodeNamespaceFromPath(this OpenApiUrlSpaceNode current
///<summary>
/// Returns the class name for the node with more or less precision depending on the provided arguments
///</summary>
internal static string GetClassName(this OpenApiUrlSpaceNode currentNode, string suffix = default, string prefix = default, OpenApiOperation operation = default) {
public static string GetClassName(this OpenApiUrlTreeNode currentNode, string suffix = default, string prefix = default, OpenApiOperation operation = default) {
var rawClassName = operation?.GetResponseSchema()?.Reference?.GetClassName() ??
currentNode?.GetIdentifier()?.ReplaceValueIdentifier();
if((currentNode?.DoesNodeBelongToItemSubnamespace() ?? false) && idClassNameCleanup.IsMatch(rawClassName))
rawClassName = idClassNameCleanup.Replace(rawClassName, string.Empty);
return prefix + rawClassName + suffix;
}
internal static bool DoesNodeBelongToItemSubnamespace(this OpenApiUrlSpaceNode currentNode) =>
(currentNode?.Segment?.StartsWith("{") ?? false) && (currentNode?.Segment?.EndsWith("}") ?? false);
internal static bool HasOperations(this OpenApiUrlSpaceNode currentNode) => currentNode?.PathItem?.Operations?.Any() ?? false;
internal static bool IsParameter(this OpenApiUrlSpaceNode currentNode)
public static string GetPathItemDescription(this OpenApiUrlTreeNode currentNode, string label, string defaultValue = default) =>
!string.IsNullOrEmpty(label) && (currentNode?.PathItems.ContainsKey(label) ?? false) ?
currentNode.PathItems[label].Description ??
currentNode.PathItems[label].Summary ??
defaultValue :
defaultValue;
public static bool DoesNodeBelongToItemSubnamespace(this OpenApiUrlTreeNode currentNode) =>
(currentNode?.Segment.StartsWith("{") ?? false) && currentNode.Segment.EndsWith("}");
public static bool IsParameter(this OpenApiUrlTreeNode currentNode)
{
return currentNode?.Segment?.StartsWith("{") ?? false;
return currentNode?.Segment.StartsWith("{") ?? false;
}
internal static bool IsFunction(this OpenApiUrlSpaceNode currentNode)
public static bool IsFunction(this OpenApiUrlTreeNode currentNode)
{
return currentNode?.Segment?.Contains("(") ?? false;
return currentNode?.Segment.Contains("(") ?? false;
}
internal static string GetIdentifier(this OpenApiUrlSpaceNode currentNode)
public static string GetIdentifier(this OpenApiUrlTreeNode currentNode)
{
if(currentNode == null) return string.Empty;
string identifier;
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Kiota.Builder.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.OpenApi" Version="1.2.3" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.2.3" />
<PackageReference Include="Microsoft.OpenApi" Version="1.3.0-preview" />
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.3.0-preview" />
</ItemGroup>

</Project>
Loading