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

Free Java form enum reflection #3775

Merged
merged 2 commits into from
Nov 22, 2023
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 @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed a bug where serialize method will not write additional data when backingStore is enabled for java. [#3643](https://github.com/microsoft/kiota/pull/3643)
- Fixed a bug where paths with parameters in the same position but different names would make generation fail. [#3756](https://github.com/microsoft/kiota/issues/3756)
- Fixed a bug where a property named constructor would make generation fail. [#3756](https://github.com/microsoft/kiota/issues/3756)
- Removed reflection for Java enum deserialization. [microsoft/kiota-java#843](https://github.com/microsoft/kiota-java/pull/843)

## [1.8.2] - 2023-11-08

Expand Down
2 changes: 1 addition & 1 deletion it/java/basic/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<kiota-java.version>0.9.0</kiota-java.version>
<kiota-java.version>0.10.0</kiota-java.version>
</properties>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion it/java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<kiota-java.version>0.9.0</kiota-java.version>
<kiota-java.version>0.10.0</kiota-java.version>
</properties>

<dependencies>
Expand Down
22 changes: 18 additions & 4 deletions src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,9 @@ private void WriteRequestExecutorBody(CodeMethod codeElement, RequestParams requ
writer.WriteLine($"{errorMappingVarName}.put(\"{errorMapping.Key.ToUpperInvariant()}\", {errorMapping.Value.Name}::{FactoryMethodName});");
}
}
var factoryParameter = codeElement.ReturnType is CodeType returnCodeType && returnCodeType.TypeDefinition is CodeClass ? $"{returnType}::{FactoryMethodName}" : $"{returnType}.class";
var factoryParameter = GetSendRequestFactoryParam(returnType, codeElement.ReturnType.AllTypes.First().TypeDefinition is CodeEnum);
var returnPrefix = codeElement.ReturnType.Name.Equals("void", StringComparison.OrdinalIgnoreCase) ? string.Empty : "return ";
writer.WriteLine($"{returnPrefix}this.requestAdapter.{sendMethodName}({RequestInfoVarName}, {factoryParameter}, {errorMappingVarName});");
writer.WriteLine($"{returnPrefix}this.requestAdapter.{sendMethodName}({RequestInfoVarName}, {errorMappingVarName}, {factoryParameter});");
}
private string GetSendRequestMethodName(bool isCollection, string returnType, bool isEnum)
{
Expand All @@ -519,6 +519,16 @@ private string GetSendRequestMethodName(bool isCollection, string returnType, bo
else if (isCollection) return "sendCollection";
return "send";
}
private string GetSendRequestFactoryParam(string returnType, bool isEnum)
{
if (conventions.PrimitiveTypes.Contains(returnType))
return $"{returnType}.class";
else if (isEnum)
return $"{returnType}::forValue";
else
return $"{returnType}::{FactoryMethodName}";
}

private const string RequestInfoVarName = "requestInfo";
private static void WriteGeneratorOrExecutorMethodCall(CodeMethod codeElement, RequestParams requestParams, CodeClass parentClass, LanguageWriter writer, string prefix, CodeMethodKind codeMethodKind)
{
Expand Down Expand Up @@ -729,11 +739,15 @@ private string GetDeserializationMethodName(CodeTypeBase propType, CodeMethod me
if (currentType.TypeDefinition == null)
return $"getCollectionOfPrimitiveValues({propertyType.ToFirstCharacterUpperCase()}.class)";
else if (currentType.TypeDefinition is CodeEnum enumType)
return $"getCollectionOfEnumValues({enumType.Name}.class)";
return $"getCollectionOfEnumValues({enumType.Name}::forValue)";
else
return $"getCollectionOfObjectValues({propertyType.ToFirstCharacterUpperCase()}::{FactoryMethodName})";
if (currentType.TypeDefinition is CodeEnum currentEnum)
return $"getEnum{(currentEnum.Flags ? "Set" : string.Empty)}Value({propertyType.ToFirstCharacterUpperCase()}.class)";
{
var returnType = propertyType.ToFirstCharacterUpperCase();
return $"getEnum{(currentEnum.Flags ? "Set" : string.Empty)}Value({returnType}::forValue)";
}

}
return propertyType switch
{
Expand Down
Loading