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

Java/request info instantiation test change #1982

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
34 changes: 27 additions & 7 deletions src/Kiota.Builder/Refiners/JavaRefiner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,22 +307,42 @@ private static void RemoveClassNamePrefixFromNestedClasses(CodeElement currentEl
.Where(static x => x.Type.ActionOf && x.IsOfKind(CodeParameterKind.RequestConfiguration))
.SelectMany(static x => x.Type.AllTypes)
.Select(static x => x.TypeDefinition)
.OfType<CodeClass>();
.OfType<CodeClass>()
.Where(x => x.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));

// {
// x => x.SelectMany(static x => x.Properties)
// .Where(static x => x.IsOfKind(CodePropertyKind.QueryParameters))
// .SelectMany(static x => x.Type.AllTypes)
// .Select(static x => x.TypeDefinition)
// .Select(static x => x.TypeDefinition)
// .OfType<CodeClass>()
// )
// .Where(x => x.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));

// .SelectMany(static x => x.Properties)
// .Where(static x => x.IsOfKind(CodePropertyKind.QueryParameters))
// .SelectMany(static x => x.Type.AllTypes)
// .Select(static x => x.TypeDefinition)
// .OfType<CodeClass>()
// .Where(x => x.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));

// ensure we do not miss out the types present in request configuration objects i.e. the query parameters
var nestedQueryParameters = innerClasses
.SelectMany(static x => x.Properties)
.Where(static x => x.IsOfKind(CodePropertyKind.QueryParameters))
.SelectMany(static x => x.Type.AllTypes)
.Select(static x => x.TypeDefinition)
.OfType<CodeClass>();
.OfType<CodeClass>().Union(innerClasses)
.Where(x => x.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase));


var nestedClasses = new List<CodeClass>();
nestedClasses.AddRange(innerClasses);
nestedClasses.AddRange(nestedQueryParameters);
// var nestedClasses = new List<CodeClass>();
// nestedClasses.AddRange(innerClasses);
// nestedClasses.AddRange(nestedQueryParameters);

foreach(var innerClass in nestedClasses) {
if(innerClass.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
foreach(var innerClass in nestedQueryParameters) {
//if(innerClass.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
innerClass.Name = innerClass.Name[prefix.Length..];

if(innerClass.IsOfKind(CodeClassKind.RequestConfiguration))
Expand Down
29 changes: 12 additions & 17 deletions src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -402,25 +402,25 @@ private static void WriteDeserializerBodyForIntersectionModel(CodeClass parentCl
}
writer.WriteLine($"return new {DeserializerReturnType}();");
}
private const string DeserializerVarName = "deserializerMap";
private void WriteDeserializerBodyForInheritedModel(CodeMethod method, CodeClass parentClass, LanguageWriter writer, bool inherits) {
var fieldToSerialize = parentClass.GetPropertiesOfKind(CodePropertyKind.Custom);
writer.WriteLines(
$"final {parentClass.Name.ToFirstCharacterUpperCase()} currentObject = this;",
$"return new {DeserializerReturnType}({(inherits ? "super." + method.Name.ToFirstCharacterLowerCase()+ "()" : fieldToSerialize.Count())}) {{{{");
$"final {DeserializerReturnType} {DeserializerVarName} = new {DeserializerReturnType}({(inherits ? "super." + method.Name.ToFirstCharacterLowerCase()+ "()" : fieldToSerialize.Count())});");
if(fieldToSerialize.Any()) {
writer.IncreaseIndent();
fieldToSerialize
.Where(static x => !x.ExistsInBaseType && x.Setter != null)
.OrderBy(static x => x.Name)
.Select(x =>
$"this.put(\"{x.SerializationName ?? x.Name.ToFirstCharacterLowerCase()}\", (n) -> {{ currentObject.{x.Setter.Name.ToFirstCharacterLowerCase()}(n.{GetDeserializationMethodName(x.Type, method)}); }});")
$"{DeserializerVarName}.put(\"{x.SerializationName ?? x.Name.ToFirstCharacterLowerCase()}\", (n) -> {{ currentObject.{x.Setter.Name.ToFirstCharacterLowerCase()}(n.{GetDeserializationMethodName(x.Type, method)}); }});")
.ToList()
.ForEach(x => writer.WriteLine(x));
writer.DecreaseIndent();
}
writer.WriteLine("}};");
writer.WriteLine($"return {DeserializerVarName};");
}
private const string FactoryMethodName = "createFromDiscriminatorValue";
private const string ExecuterExceptionVar = "executionException";
private void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requestParams, LanguageWriter writer) {
if(codeElement.HttpMethod == null) throw new InvalidOperationException("http method cannot be null");
var returnType = conventions.GetTypeString(codeElement.ReturnType, codeElement, false);
Expand All @@ -431,20 +431,18 @@ private void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requ
var errorMappingVarName = "null";
if(codeElement.ErrorMappings.Any()) {
errorMappingVarName = "errorMapping";
writer.WriteLine($"final HashMap<String, ParsableFactory<? extends Parsable>> {errorMappingVarName} = new HashMap<String, ParsableFactory<? extends Parsable>>({codeElement.ErrorMappings.Count()}) {{{{");
writer.IncreaseIndent();
writer.WriteLine($"final HashMap<String, ParsableFactory<? extends Parsable>> {errorMappingVarName} = new HashMap<String, ParsableFactory<? extends Parsable>>();");
foreach(var errorMapping in codeElement.ErrorMappings) {
writer.WriteLine($"put(\"{errorMapping.Key.ToUpperInvariant()}\", {errorMapping.Value.Name.ToFirstCharacterUpperCase()}::{FactoryMethodName});");
writer.WriteLine($"{errorMappingVarName}.put(\"{errorMapping.Key.ToUpperInvariant()}\", {errorMapping.Value.Name.ToFirstCharacterUpperCase()}::{FactoryMethodName});");
}
writer.CloseBlock("}};");
}
var factoryParameter = codeElement.ReturnType is CodeType returnCodeType && returnCodeType.TypeDefinition is CodeClass ? $"{returnType}::{FactoryMethodName}" : $"{returnType}.class";
writer.WriteLine($"return this.requestAdapter.{sendMethodName}({RequestInfoVarName}, {factoryParameter}, {errorMappingVarName});");
writer.DecreaseIndent();
writer.StartBlock("} catch (URISyntaxException ex) {");
writer.StartBlock($"return new java.util.concurrent.CompletableFuture<{returnType}>() {{{{");
writer.WriteLine("this.completeExceptionally(ex);");
writer.CloseBlock("}};");
writer.WriteLine($"java.util.concurrent.CompletableFuture<{returnType}> {ExecuterExceptionVar} = new java.util.concurrent.CompletableFuture<{returnType}>();");
writer.WriteLine($"{ExecuterExceptionVar}.completeExceptionally(ex);");
writer.WriteLine($"return {ExecuterExceptionVar};");
writer.CloseBlock();
}
private string GetSendRequestMethodName(bool isCollection, string returnType, bool isEnum)
Expand Down Expand Up @@ -485,11 +483,8 @@ private void WriteRequestGeneratorBody(CodeMethod codeElement, RequestParams req
var urlTemplateParamsProperty = currentClass.GetPropertyOfKind(CodePropertyKind.PathParameters);
var urlTemplateProperty = currentClass.GetPropertyOfKind(CodePropertyKind.UrlTemplate);
var requestAdapterProperty = currentClass.GetPropertyOfKind(CodePropertyKind.RequestAdapter);
writer.WriteLine($"final RequestInformation {RequestInfoVarName} = new RequestInformation() {{{{");
writer.IncreaseIndent();
writer.WriteLine($"httpMethod = HttpMethod.{codeElement.HttpMethod?.ToString().ToUpperInvariant()};");
writer.DecreaseIndent();
writer.WriteLine("}};");
writer.WriteLine($"final RequestInformation {RequestInfoVarName} = new RequestInformation();");
writer.WriteLine($"{RequestInfoVarName}.httpMethod = HttpMethod.{codeElement.HttpMethod?.ToString().ToUpperInvariant()};");
writer.WriteLines($"{RequestInfoVarName}.urlTemplate = {GetPropertyCall(urlTemplateProperty, "\"\"")};",
$"{RequestInfoVarName}.pathParameters = {GetPropertyCall(urlTemplateParamsProperty, "null")};");
if(codeElement.AcceptedResponseTypes.Any())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class CodeMethodWriterTests : IDisposable {
private readonly CodeMethod method;
private readonly CodeClass parentClass;
private readonly CodeNamespace root;
private const string ExecuterExceptionVar = "executionException";
private const string MethodName = "methodName";
private const string ReturnTypeName = "Somecustomtype";
private const string MethodDescription = "some description";
Expand Down Expand Up @@ -499,9 +500,8 @@ public void WritesRequestExecutorBody() {
Assert.Contains("put(\"5XX\", Error5XX::createFromDiscriminatorValue);", result);
Assert.Contains("put(\"403\", Error403::createFromDiscriminatorValue);", result);
Assert.Contains("sendAsync", result);
Assert.Contains("return new java.util.concurrent.CompletableFuture<Somecustomtype>() {{", result);
Assert.Contains("this.completeExceptionally(ex);", result);
Assert.Contains("}};", result);
Assert.Contains($"java.util.concurrent.CompletableFuture<Somecustomtype> {ExecuterExceptionVar} = new java.util.concurrent.CompletableFuture<Somecustomtype>();", result);
Assert.Contains($"{ExecuterExceptionVar}.completeExceptionally(ex);", result);
AssertExtensions.CurlyBracesAreClosed(result);
}
[Fact]
Expand Down