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

Fixes EnumSet generation in Java #3283

Merged
merged 4 commits into from
Sep 11, 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 @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Aggregate typescript import statements by source [#3232](https://github.com/microsoft/kiota/issues/3232)
- Fixes a bug in dotnet where enums types would not be fully disambiguated when initialized in constructor with default values and existing conflicting property exists [#3233]
- Fixes a bug in generation of flagged enum properties and their serializer/deserializer functions in typescript [https://github.com/microsoft/kiota/issues/3260]
- Fixes missing EnumSet types in method parameter and return types in Java [https://github.com/microsoft/kiota/issues/3272]

## [1.5.1] - 2023-08-08

Expand Down
2 changes: 2 additions & 0 deletions src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public override void WriteCodeElement(CodeMethod codeElement, LanguageWriter wri
if (codeElement.Parent is not CodeClass parentClass) throw new InvalidOperationException("the parent of a method should be a class");

var returnType = conventions.GetTypeString(codeElement.ReturnType, codeElement);
if (codeElement.ReturnType is CodeType { TypeDefinition: CodeEnum { Flags: true }, IsCollection: false })
returnType = $"EnumSet<{returnType}>";
if (codeElement.IsAsync &&
codeElement.IsOfKind(CodeMethodKind.RequestExecutor) &&
returnType.Equals("void", StringComparison.OrdinalIgnoreCase))
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Writers/Java/CodePropertyWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public override void WriteCodeElement(CodeProperty codeElement, LanguageWriter w
defaultValue = $" = new {returnType}()";
goto default;
default:
if (codeElement.Type is CodeType currentType && currentType.TypeDefinition is CodeEnum enumType && enumType.Flags)
if (codeElement.Type is CodeType { TypeDefinition: CodeEnum { Flags: true }, IsCollection: false })
returnType = $"EnumSet<{returnType}>";
if (codeElement.Access != AccessModifier.Private)
writer.WriteLine(codeElement.Type.IsNullable ? "@jakarta.annotation.Nullable" : "@jakarta.annotation.Nonnull");
Expand Down
5 changes: 4 additions & 1 deletion src/Kiota.Builder/Writers/Java/JavaConventionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ public override string GetParameterSignature(CodeParameter parameter, CodeElemen
ArgumentNullException.ThrowIfNull(parameter);
var nullKeyword = parameter.Optional ? "Nullable" : "Nonnull";
var nullAnnotation = parameter.Type.IsNullable ? $"@jakarta.annotation.{nullKeyword} " : string.Empty;
return $"{nullAnnotation}final {GetTypeString(parameter.Type, targetElement)} {parameter.Name.ToFirstCharacterLowerCase()}";
var parameterType = GetTypeString(parameter.Type, targetElement);
if (parameter.Type is CodeType { TypeDefinition: CodeEnum { Flags: true }, IsCollection: false })
parameterType = $"EnumSet<{parameterType}>";
return $"{nullAnnotation}final {parameterType} {parameter.Name.ToFirstCharacterLowerCase()}";
}

public override string GetTypeString(CodeTypeBase code, CodeElement targetElement, bool includeCollectionInformation = true, LanguageWriter? writer = null)
Expand Down
26 changes: 26 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,32 @@ public void WritesSetterToField()
Assert.Contains("this.someProperty = value", result);
}
[Fact]
public void WritesGetterToFieldWithEnumSetParameter()
{
setup();
var codeEnumType = new CodeType
{
Name = "customEnum",
TypeDefinition = new CodeEnum
{
Name = "customEnumType",
Flags = true
}
};
method.AccessedProperty = new CodeProperty
{
Name = "someProperty",
Type = codeEnumType
};
(method.Parent as CodeClass)?.AddProperty(method.AccessedProperty);
method.Kind = CodeMethodKind.Getter;
method.ReturnType = codeEnumType;
writer.Write(method);
var result = tw.ToString();
Assert.Contains("this.someProperty", result);
Assert.Contains("EnumSet<", result);
}
[Fact]
public void WritePeriodAndDurationSetterToField()
{
setup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,23 @@ public void WritesSerializationAnnotation()
var result = tw.ToString();
Assert.Contains("@QueryParameter(name = \"someserializationname\")", result);
}
[Fact]
public void WritesCollectionFlagEnumsAsOneDimensionalArray()
{
property.Kind = CodePropertyKind.Custom;
property.Type = new CodeType
{
Name = "customEnum",
CollectionKind = CodeTypeBase.CodeTypeCollectionKind.Complex
};
(property.Type as CodeType).TypeDefinition = new CodeEnum
{
Name = "customEnumType",
Flags = true,//this is not expected for a collection. So treat as enum collection
};
writer.Write(property);
var result = tw.ToString();
Assert.Contains("List<", result);
Assert.DoesNotContain("EnumSet", result);
}
}
Loading