diff --git a/CHANGELOG.md b/CHANGELOG.md index c36774f222..f5038230b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs index ac60397f51..5c00c1ff53 100644 --- a/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs +++ b/src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs @@ -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)) diff --git a/src/Kiota.Builder/Writers/Java/CodePropertyWriter.cs b/src/Kiota.Builder/Writers/Java/CodePropertyWriter.cs index 68dfa0c763..aa089e9ff2 100644 --- a/src/Kiota.Builder/Writers/Java/CodePropertyWriter.cs +++ b/src/Kiota.Builder/Writers/Java/CodePropertyWriter.cs @@ -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"); diff --git a/src/Kiota.Builder/Writers/Java/JavaConventionService.cs b/src/Kiota.Builder/Writers/Java/JavaConventionService.cs index c932cf5dc2..0108ad4b87 100644 --- a/src/Kiota.Builder/Writers/Java/JavaConventionService.cs +++ b/src/Kiota.Builder/Writers/Java/JavaConventionService.cs @@ -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) diff --git a/tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs index 7f56c8f4ca..4fc0406737 100644 --- a/tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs @@ -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(); diff --git a/tests/Kiota.Builder.Tests/Writers/Java/CodePropertyWriterTests.cs b/tests/Kiota.Builder.Tests/Writers/Java/CodePropertyWriterTests.cs index fbbf76b1fc..cc4b958c8d 100644 --- a/tests/Kiota.Builder.Tests/Writers/Java/CodePropertyWriterTests.cs +++ b/tests/Kiota.Builder.Tests/Writers/Java/CodePropertyWriterTests.cs @@ -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); + } }