-
Notifications
You must be signed in to change notification settings - Fork 215
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
Feature/http language support #5778
Open
koros
wants to merge
41
commits into
main
Choose a base branch
from
feature/http-language-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
41 commits
Select commit
Hold shift + click to select a range
8dd143d
initial http genertion support
koros c7e352a
add http language generation
koros c2c7443
remove unused code elements from the DOM
koros 3a25dab
merge if statements
koros 9bcac7d
Merge branch 'main' into feature/http-language-support
koros 5154280
remove unused code in the refiner
koros 80d771c
Merge branch 'main' into feature/http-language-support
koros bac834f
write all properties including ones in the base class
koros 31c3c53
Remove unused code and enhance JSON request body output
koros 0ec5815
decode the parameter names before writing to http snippet files
koros f033a45
Merge branch 'main' into feature/http-language-support
koros f2f1c8e
write all the path parameter variables
koros ec4aea7
Exclude the generic pathParameters variable from being written to the…
koros da3bf05
add comments and documentation info. to Writers/HTTP/CodeClassDeclara…
koros 69ba9ac
Merge branch 'main' into feature/http-language-support
koros 3852185
Use URL Template to write actual urls
koros 7508ebd
use double curly braces for all variables
koros 0c7e0ee
Merge branch 'main' into feature/http-language-support
koros 41345d5
add unit tests for HttpPathSegmenter
koros cfabf37
unit tests for CodeEnum writer
koros cfbbefe
format code
koros ff4caed
Merge branch 'main' into feature/http-language-support
koros d9f7584
remove more unnecessary code from the HttpConventionService
koros 69bac1f
rename http namespace to use proper casing
koros fc39c89
remove unused method
koros 6900e7e
Merge branch 'main' into feature/http-language-support
koros 2ade0bd
Update HTTP Reserved Names Provider with Additional Reserved Keywords
koros 0343d2a
Merge branch 'main' into feature/http-language-support
koros 0fa1c0d
remove reserved names provider; add more unit tests
koros 106cbae
format code
koros 4f2bda2
Merge branch 'main' into feature/http-language-support
koros 41dbd77
reset .vscode/launch.json
koros 7d30713
add more unit tests
koros 4ccc263
address pr comments
koros d9da636
address pr comments
koros ef519d1
address pr comments
koros 51c6e25
format code
koros cfb3024
Merge branch 'main' into feature/http-language-support
koros fdfabb7
restore formating for nested json objects
koros 407f61a
address pr comments
koros 54c3d8f
Merge branch 'main' into feature/http-language-support
Onokaev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,5 +9,6 @@ public enum GenerationLanguage | |
Go, | ||
Swift, | ||
Ruby, | ||
CLI | ||
CLI, | ||
HTTP | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Kiota.Builder.CodeDOM; | ||
using Kiota.Builder.Extensions; | ||
|
||
namespace Kiota.Builder.PathSegmenters; | ||
public class HttpPathSegmenter(string rootPath, string clientNamespaceName) : CommonPathSegmenter(rootPath, clientNamespaceName) | ||
{ | ||
public override string FileSuffix => ".http"; | ||
public override string NormalizeNamespaceSegment(string segmentName) => segmentName.ToFirstCharacterUpperCase(); | ||
public override string NormalizeFileName(CodeElement currentElement) | ||
{ | ||
return GetLastFileNameSegment(currentElement).ToFirstCharacterUpperCase(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Kiota.Builder.CodeDOM; | ||
using Kiota.Builder.Configuration; | ||
using Kiota.Builder.Extensions; | ||
|
||
namespace Kiota.Builder.Refiners; | ||
public class HttpRefiner(GenerationConfiguration configuration) : CommonLanguageRefiner(configuration) | ||
{ | ||
public override Task RefineAsync(CodeNamespace generatedCode, CancellationToken cancellationToken) | ||
{ | ||
return Task.Run(() => | ||
{ | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
CapitalizeNamespacesFirstLetters(generatedCode); | ||
ReplaceIndexersByMethodsWithParameter( | ||
generatedCode, | ||
false, | ||
static x => $"By{x.ToFirstCharacterUpperCase()}", | ||
static x => x.ToFirstCharacterUpperCase(), | ||
GenerationLanguage.HTTP); | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
ReplaceReservedNames( | ||
generatedCode, | ||
new HttpReservedNamesProvider(), | ||
x => $"{x}_escaped"); | ||
RemoveCancellationParameter(generatedCode); | ||
ConvertUnionTypesToWrapper( | ||
generatedCode, | ||
_configuration.UsesBackingStore, | ||
static s => s | ||
); | ||
cancellationToken.ThrowIfCancellationRequested(); | ||
SetBaseUrlForRequestBuilderMethods(generatedCode, GetBaseUrl(generatedCode)); | ||
// Remove unused code from the DOM e.g Models, BarrelInitializers, e.t.c | ||
RemoveUnusedCodeElements(generatedCode); | ||
}, cancellationToken); | ||
} | ||
|
||
private string? GetBaseUrl(CodeElement element) | ||
{ | ||
return element.GetImmediateParentOfType<CodeNamespace>() | ||
.GetRootNamespace()? | ||
.FindChildByName<CodeClass>(_configuration.ClientClassName)? | ||
.Methods? | ||
.FirstOrDefault(static x => x.IsOfKind(CodeMethodKind.ClientConstructor))? | ||
.BaseUrl; | ||
} | ||
|
||
private static void CapitalizeNamespacesFirstLetters(CodeElement current) | ||
{ | ||
if (current is CodeNamespace currentNamespace) | ||
currentNamespace.Name = currentNamespace.Name.Split('.').Select(static x => x.ToFirstCharacterUpperCase()).Aggregate(static (x, y) => $"{x}.{y}"); | ||
CrawlTree(current, CapitalizeNamespacesFirstLetters); | ||
} | ||
|
||
private static void SetBaseUrlForRequestBuilderMethods(CodeElement current, string? baseUrl) | ||
{ | ||
if (baseUrl is not null && current is CodeClass codeClass && codeClass.IsOfKind(CodeClassKind.RequestBuilder)) | ||
{ | ||
// Add a new property named BaseUrl and set its value to the baseUrl string | ||
var baseUrlProperty = new CodeProperty | ||
{ | ||
Name = "BaseUrl", | ||
Kind = CodePropertyKind.Custom, | ||
Access = AccessModifier.Private, | ||
DefaultValue = baseUrl, | ||
Type = new CodeType { Name = "string", IsExternal = true } | ||
}; | ||
codeClass.AddProperty(baseUrlProperty); | ||
} | ||
CrawlTree(current, (element) => SetBaseUrlForRequestBuilderMethods(element, baseUrl)); | ||
} | ||
|
||
private void RemoveUnusedCodeElements(CodeElement element) | ||
{ | ||
if (!IsRequestBuilderClass(element) || IsBaseRequestBuilder(element) || IsRequestBuilderClassWithoutAnyHttpOperations(element)) | ||
{ | ||
var parentNameSpace = element.GetImmediateParentOfType<CodeNamespace>(); | ||
parentNameSpace?.RemoveChildElement(element); | ||
} | ||
else | ||
{ | ||
// Add path variables | ||
AddPathParameters(element); | ||
} | ||
CrawlTree(element, RemoveUnusedCodeElements); | ||
} | ||
|
||
private static void AddPathParameters(CodeElement element) | ||
{ | ||
// Target RequestBuilder Classes only | ||
if (element is not CodeClass codeClass) return; | ||
|
||
var parent = element.GetImmediateParentOfType<CodeNamespace>().Parent; | ||
while (parent is not null) | ||
{ | ||
var codeIndexer = parent.GetChildElements(false) | ||
.OfType<CodeClass>() | ||
.FirstOrDefault()? | ||
.GetChildElements(false) | ||
.OfType<CodeMethod>() | ||
.FirstOrDefault(static x => x.IsOfKind(CodeMethodKind.IndexerBackwardCompatibility)); | ||
|
||
if (codeIndexer is not null) | ||
{ | ||
// Retrieve all the parameters of kind CodeParameterKind.Custom | ||
var customParameters = codeIndexer.Parameters | ||
.Where(static param => param.IsOfKind(CodeParameterKind.Custom)) | ||
.ToList(); | ||
|
||
// For each parameter: | ||
foreach (var param in customParameters) | ||
{ | ||
// Create a new property of kind CodePropertyKind.PathParameters using the parameter and add it to the codeClass | ||
var pathParameterProperty = new CodeProperty | ||
{ | ||
Name = param.Name, | ||
Kind = CodePropertyKind.PathParameters, | ||
Type = param.Type, | ||
Access = AccessModifier.Public, | ||
DefaultValue = param.DefaultValue, | ||
SerializationName = param.SerializationName, | ||
Documentation = param.Documentation | ||
}; | ||
codeClass.AddProperty(pathParameterProperty); | ||
} | ||
} | ||
|
||
parent = parent.Parent?.GetImmediateParentOfType<CodeNamespace>(); | ||
} | ||
} | ||
|
||
private static bool IsRequestBuilderClass(CodeElement element) | ||
{ | ||
return element is CodeClass code && code.IsOfKind(CodeClassKind.RequestBuilder); | ||
} | ||
|
||
private bool IsBaseRequestBuilder(CodeElement element) | ||
{ | ||
return element is CodeClass codeClass && | ||
codeClass.Name.Equals(_configuration.ClientClassName, StringComparison.Ordinal); | ||
} | ||
|
||
private static bool IsRequestBuilderClassWithoutAnyHttpOperations(CodeElement element) | ||
{ | ||
return element is CodeClass codeClass && codeClass.IsOfKind(CodeClassKind.RequestBuilder) && | ||
!codeClass.Methods.Any(static method => method.IsOfKind(CodeMethodKind.RequestExecutor)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace Kiota.Builder.Refiners; | ||
public class HttpReservedNamesProvider : IReservedNamesProvider | ||
{ | ||
private readonly Lazy<HashSet<string>> _reservedNames = new(() => new HashSet<string>(StringComparer.OrdinalIgnoreCase) | ||
{ | ||
}); | ||
public HashSet<string> ReservedNames => _reservedNames.Value; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
using Kiota.Builder.CodeDOM; | ||
|
||
namespace Kiota.Builder.Writers.Http; | ||
public class CodeBlockEndWriter : ICodeElementWriter<BlockEnd> | ||
{ | ||
public void WriteCodeElement(BlockEnd codeElement, LanguageWriter writer) | ||
{ | ||
ArgumentNullException.ThrowIfNull(codeElement); | ||
ArgumentNullException.ThrowIfNull(writer); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to call this from the refiner method directly as this would handle the traversal of the tree for you?
My concern here is that if two classes exist in the same namespace, you'll traverse them twice as you will be referencing to the same namespace and select all the children over again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you give an example of how the traversal would look like?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment,
AddPathParameters
is currently called fromRemoveUnusedCodeElements
.Since
RemoveUnusedCodeElements
crawls the tree, it will go through each element in the DOM(line 90).So, if you have two classes in the same namespace, each class may be called as a parameter for
AddPathParameters
meaning that you will get the same namespace twice(for each time you encounter the class) and enumerateALL
the children. (When you get to the parent of the namespace, I believe you will list all the children which were already listed in a previous iteration as they are part of the child elements of the previous parent)Instead, you can simply add a method called directly in the
RefineAsync
(after removing the classes/methods you don't need). The method would simply check if the codeElement is aCodeMethod
and the kind isCodeMethodKind.IndexerBackwardCompatibility
then add the path parameters. And crawl the tree.