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

Generated Class and CompletableFuture<Void> Annotations #3170

Merged
merged 7 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Replace Javax annotations in favor of Jakarta annotations for Java code generation. [#2810](https://github.com/microsoft/kiota/issues/2810)
- RequestExecuters call overload methods reducing code generation size for Java. [#3150](https://github.com/microsoft/kiota/issues/3150)
- Remove URISyntaxException from Java generated RequestExecutors and RequestGenerators. [#3149](https://github.com/microsoft/kiota/issues/3149)
- Adds 'Generated' annotation to generated Enums and Classes for Java. [#3106](https://github.com/microsoft/kiota/issues/3106)

## [1.5.1] - 2023-08-08

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
namespace Kiota.Builder.Writers.Java;
public class CodeClassDeclarationWriter : BaseElementWriter<ClassDeclaration, JavaConventionService>
{
public static string AutoGenerationHeader => "@Generated(\"com.microsoft.kiota\")";
public CodeClassDeclarationWriter(JavaConventionService conventionService) : base(conventionService) { }
public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWriter writer)
{
Expand Down Expand Up @@ -35,6 +36,7 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit
if (codeElement.Parent is CodeClass parentClass)
conventions.WriteLongDescription(parentClass, writer);
var innerClassStatic = codeElement.Parent is CodeClass currentClass && currentClass.IsOfKind(CodeClassKind.Model) && currentClass.Parent is CodeClass ? "static " : string.Empty; //https://stackoverflow.com/questions/47541459/no-enclosing-instance-is-accessible-must-qualify-the-allocation-with-an-enclosi
writer.WriteLine($"{AutoGenerationHeader}");
writer.WriteLine($"public {innerClassStatic}class {codeElement.Name.ToFirstCharacterUpperCase()}{derivation} {{");
writer.IncreaseIndent();
}
Expand Down
2 changes: 2 additions & 0 deletions src/Kiota.Builder/Writers/Java/CodeEnumWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Kiota.Builder.Writers.Java;
public class CodeEnumWriter : BaseElementWriter<CodeEnum, JavaConventionService>
{
public static string AutoGenerationHeader => "@Generated(\"com.microsoft.kiota\")";
public CodeEnumWriter(JavaConventionService conventionService) : base(conventionService) { }
public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter writer)
{
Expand All @@ -22,6 +23,7 @@ public override void WriteCodeElement(CodeEnum codeElement, LanguageWriter write
string.Empty);
conventions.WriteLongDescription(codeElement, writer);
conventions.WriteDeprecatedAnnotation(codeElement, writer);
writer.WriteLine($"{AutoGenerationHeader}");
writer.WriteLine($"public enum {enumName} implements ValuedEnum {{");
writer.IncreaseIndent();
var lastEnumOption = enumOptions.Last();
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Writers/Java/CodeMethodWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ private void WriteMethodDocumentation(CodeMethod code, LanguageWriter writer, st
.OrderBy(static x => x.Name, StringComparer.OrdinalIgnoreCase)
.Select(x => $"@param {x.Name} {JavaConventionService.RemoveInvalidDescriptionCharacters(x.Documentation.Description)}")
.Union(new[] { returnRemark }));
if (!returnVoid) //Nullable/Nonnull annotations for returns are a part of Method Documentation
if (!returnVoid || code.IsAsync) //Nullable/Nonnull annotations for returns are a part of Method Documentation
writer.WriteLine(code.ReturnType.IsNullable && !code.IsAsync ? "@jakarta.annotation.Nullable" : "@jakarta.annotation.Nonnull");
}
private string GetDeserializationMethodName(CodeTypeBase propType, CodeMethod method)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Kiota.Builder.Tests.Writers.Java;
public class CodeClassDeclarationWriterTests : IDisposable
{
private const string AutoGenerationHeader = "@Generated(\"com.microsoft.kiota\")";
private const string DefaultPath = "./";
private const string DefaultName = "name";
private readonly StringWriter tw;
Expand Down Expand Up @@ -43,6 +44,13 @@ public void WritesSimpleDeclaration()
Assert.Contains("public class", result);
}
[Fact]
public void WritesAutoGeneratedHeader()
{
codeElementWriter.WriteCodeElement(parentClass.StartBlock, writer);
var result = tw.ToString();
Assert.Contains(AutoGenerationHeader, result);
}
[Fact]
public void WritesImplementation()
{
var declaration = parentClass.StartBlock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Kiota.Builder.Tests.Writers.Java;
public class CodeEnumWriterTests : IDisposable
{
private const string AutoGenerationHeader = "@Generated(\"com.microsoft.kiota\")";
private const string DefaultPath = "./";
private const string DefaultName = "name";
private readonly StringWriter tw;
Expand Down Expand Up @@ -49,6 +50,7 @@ public void WritesEnum()
Assert.Contains("@jakarta.annotation.Nullable", result);
Assert.Contains("forValue(@jakarta.annotation.Nonnull final String searchValue)", result);
Assert.Contains("default: return null;", result);
Assert.Contains(AutoGenerationHeader, result);
AssertExtensions.CurlyBracesAreClosed(result);
Assert.Contains(optionName, result);
}
Expand Down
Loading