Skip to content

Commit

Permalink
Merge pull request #4188 from microsoft/andrueastman/docComments
Browse files Browse the repository at this point in the history
Fixes deprecation information comments
  • Loading branch information
andrueastman authored Feb 16, 2024
2 parents 0b7b586 + 5c46b79 commit 4d4bec5
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 7 deletions.
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 return doc comments for Go/Java/CSharp/TypeScript.
- Fixed type names in doc comments and deprecation noticed across languages.
- Added thrown exceptions in doc comments for Go/CSharp/Java/TypeScript. [#3811](https://github.com/microsoft/kiota/issues/3811)
- Fixed missing type/method names in deprecation comments.[#4186](https://github.com/microsoft/kiota/issues/4186)
- Fixed `cref` tags not closed in doc comments in CSharp generation.
- Deduplicates 4XX and 5XX error mappings when they map to the same type to reduce emitted code. [#4025](https://github.com/microsoft/kiota/issues/4025)
- 📢📢📢 Java generation is now stable! 🚀🚀🚀 special thanks to @andreaTP (Red Hat) for all the help.
Expand Down
6 changes: 4 additions & 2 deletions src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public static void WriteNullableClosing(LanguageWriter writer)
}
private const string ReferenceTypePrefix = "<see cref=\"";
private const string ReferenceTypeSuffix = "\"/>";
#pragma warning disable S1006 // Method overrides should not change parameter defaults
public override void WriteShortDescription(IDocumentedElement element, LanguageWriter writer, string prefix = "<summary>", string suffix = "</summary>")
#pragma warning restore S1006 // Method overrides should not change parameter defaults
{
ArgumentNullException.ThrowIfNull(writer);
ArgumentNullException.ThrowIfNull(element);
Expand Down Expand Up @@ -293,14 +295,14 @@ _ when nameof(String).Equals(parameterType, StringComparison.OrdinalIgnoreCase)
};
return $"{GetDeprecationInformation(parameter)}{parameterType} {parameter.Name.ToFirstCharacterLowerCase()}{defaultValue}";
}
private static string GetDeprecationInformation(IDeprecableElement element)
private string GetDeprecationInformation(IDeprecableElement element)
{
if (element.Deprecation is null || !element.Deprecation.IsDeprecated) return string.Empty;

var versionComment = string.IsNullOrEmpty(element.Deprecation.Version) ? string.Empty : $" as of {element.Deprecation.Version}";
var dateComment = element.Deprecation.Date is null ? string.Empty : $" on {element.Deprecation.Date.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}";
var removalComment = element.Deprecation.RemovalDate is null ? string.Empty : $" and will be removed {element.Deprecation.RemovalDate.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}";
return $"[Obsolete(\"{element.Deprecation.DescriptionTemplate}{versionComment}{dateComment}{removalComment}\")]";
return $"[Obsolete(\"{element.Deprecation.GetDescription(type => GetTypeString(type, (element as CodeElement)!).Split('.', StringSplitOptions.TrimEntries)[^1])}{versionComment}{dateComment}{removalComment}\")]";
}
internal void WriteDeprecationAttribute(IDeprecableElement element, LanguageWriter writer)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Writers/Go/GoConventionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,6 @@ internal void WriteDeprecation(IDeprecableElement element, LanguageWriter writer
var versionComment = string.IsNullOrEmpty(element.Deprecation.Version) ? string.Empty : $" as of {element.Deprecation.Version}";
var dateComment = element.Deprecation.Date is null ? string.Empty : $" on {element.Deprecation.Date.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}";
var removalComment = element.Deprecation.RemovalDate is null ? string.Empty : $" and will be removed {element.Deprecation.RemovalDate.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}";
WriteDescriptionItem($"Deprecated: {element.Deprecation.DescriptionTemplate}{versionComment}{dateComment}{removalComment}", writer);
WriteDescriptionItem($"Deprecated: {element.Deprecation.GetDescription(type => GetTypeString(type, (element as CodeElement)!).TrimStart('*'))}{versionComment}{dateComment}{removalComment}", writer);
}
}
2 changes: 1 addition & 1 deletion src/Kiota.Builder/Writers/Java/JavaConventionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ private string[] GetDeprecationInformationForDocumentationComment(IDeprecableEle
var removalComment = element.Deprecation.RemovalDate is null ? string.Empty : $" and will be removed {element.Deprecation.RemovalDate.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}";
return [
$"@deprecated",
$"{element.Deprecation.DescriptionTemplate}{versionComment}{dateComment}{removalComment}"
$"{element.Deprecation.GetDescription(type => GetTypeString(type, (element as CodeElement)!))}{versionComment}{dateComment}{removalComment}"
];
}
internal void WriteDeprecatedAnnotation(CodeElement element, LanguageWriter writer)
Expand Down
4 changes: 2 additions & 2 deletions src/Kiota.Builder/Writers/Python/PythonConventionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,14 @@ public void WriteInLineDescription(IDocumentedElement element, LanguageWriter wr
writer.WriteLine($"{InLineCommentPrefix}{description}");
}

private static string GetDeprecationInformation(IDeprecableElement element)
private string GetDeprecationInformation(IDeprecableElement element)
{
if (element.Deprecation is null || !element.Deprecation.IsDeprecated) return string.Empty;

var versionComment = string.IsNullOrEmpty(element.Deprecation.Version) ? string.Empty : $" as of {element.Deprecation.Version}";
var dateComment = element.Deprecation.Date is null ? string.Empty : $" on {element.Deprecation.Date.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}";
var removalComment = element.Deprecation.RemovalDate is null ? string.Empty : $" and will be removed {element.Deprecation.RemovalDate.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}";
return $"{element.Deprecation.DescriptionTemplate}{versionComment}{dateComment}{removalComment}";
return $"{element.Deprecation.GetDescription(type => GetTypeString(type, (element as CodeElement)!))}{versionComment}{dateComment}{removalComment}";
}
internal void WriteDeprecationWarning(IDeprecableElement element, LanguageWriter writer)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,6 @@ private string GetDeprecationComment(IDeprecableElement element)
var versionComment = string.IsNullOrEmpty(element.Deprecation.Version) ? string.Empty : $" as of {element.Deprecation.Version}";
var dateComment = element.Deprecation.Date is null ? string.Empty : $" on {element.Deprecation.Date.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}";
var removalComment = element.Deprecation.RemovalDate is null ? string.Empty : $" and will be removed {element.Deprecation.RemovalDate.Value.Date.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}";
return $"@deprecated {element.Deprecation.DescriptionTemplate}{versionComment}{dateComment}{removalComment}";
return $"@deprecated {element.Deprecation.GetDescription(type => GetTypeString(type, (element as CodeElement)!))}{versionComment}{dateComment}{removalComment}";
}
}
12 changes: 12 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/CSharp/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1838,4 +1838,16 @@ public void WritesDeprecationInformation()
Assert.Contains("v2.0", result);
Assert.Contains("[Obsolete", result);
}

[Fact]
public void WritesDeprecationInformationFromBuilder()
{
setup();
var newMethod = method.Clone() as CodeMethod;
newMethod.Name = "NewAwesomeMethod";// new method replacement
method.Deprecation = new("This method is obsolete. Use {TypeName} instead.", IsDeprecated: true, TypeReferences: new() { { "TypeName", new CodeType { TypeDefinition = newMethod, IsExternal = false } } });
writer.Write(method);
var result = tw.ToString();
Assert.Contains("This method is obsolete. Use NewAwesomeMethod instead.", result);
}
}
11 changes: 11 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/Go/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2177,6 +2177,17 @@ public void WritesDeprecationInformation()
Assert.Contains("// Deprecated:", result);
}
[Fact]
public void WritesDeprecationInformationFromBuilder()
{
setup();
var newMethod = method.Clone() as CodeMethod;
newMethod.Name = "NewAwesomeMethod";// new method replacement
method.Deprecation = new("This method is obsolete. Use {TypeName} instead.", IsDeprecated: true, TypeReferences: new() { { "TypeName", new CodeType { TypeDefinition = newMethod, IsExternal = false } } });
writer.Write(method);
var result = tw.ToString();
Assert.Contains("This method is obsolete. Use NewAwesomeMethod instead.", result);
}
[Fact]
public void WritesComposedMarker()
{
setup();
Expand Down
11 changes: 11 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/Java/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,17 @@ public void WritesDeprecationInformation()
Assert.Contains("@Deprecated", result);
}
[Fact]
public void WritesDeprecationInformationFromBuilder()
{
setup();
var newMethod = method.Clone() as CodeMethod;
newMethod.Name = "NewAwesomeMethod";// new method replacement
method.Deprecation = new("This method is obsolete. Use NewAwesomeMethod instead.", IsDeprecated: true, TypeReferences: new() { { "TypeName", new CodeType { TypeDefinition = newMethod, IsExternal = false } } });
writer.Write(method);
var result = tw.ToString();
Assert.Contains("This method is obsolete. Use NewAwesomeMethod instead.", result);
}
[Fact]
public void WritesMessageOverrideOnPrimary()
{
// Given
Expand Down
11 changes: 11 additions & 0 deletions tests/Kiota.Builder.Tests/Writers/Python/CodeMethodWriterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2191,4 +2191,15 @@ public void WritesDeprecationInformation()
Assert.Contains("v2.0", result);
Assert.Contains("warn(", result);
}
[Fact]
public void WritesDeprecationInformationFromBuilder()
{
setup();
var newMethod = method.Clone() as CodeMethod;
newMethod.Name = "NewAwesomeMethod";// new method replacement
method.Deprecation = new("This method is obsolete. Use NewAwesomeMethod instead.", IsDeprecated: true, TypeReferences: new() { { "TypeName", new CodeType { TypeDefinition = newMethod, IsExternal = false } } });
writer.Write(method);
var result = tw.ToString();
Assert.Contains("This method is obsolete. Use NewAwesomeMethod instead.", result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,25 @@ public void WritesDeprecationInformation()
Assert.Contains("v2.0", result);
Assert.Contains("@deprecated", result);
}
[Fact]
public void WritesDeprecationInformationFromBuilder()
{
var parentClass = root.AddClass(new CodeClass
{
Name = "ODataError",
Kind = CodeClassKind.Model,
}).First();
var method = TestHelper.CreateMethod(parentClass, MethodName, ReturnTypeName);
method.Kind = CodeMethodKind.Factory;
method.IsStatic = true;
method.Name = "NewAwesomeMethod";// new method replacement
method.Deprecation = new("This method is obsolete. Use {TypeName} instead.", IsDeprecated: true, TypeReferences: new() { { "TypeName", new CodeType { TypeDefinition = method, IsExternal = false } } });
var function = new CodeFunction(method);
root.TryAddCodeFile("foo", function);
writer.Write(function);
var result = tw.ToString();
Assert.Contains("This method is obsolete. Use NewAwesomeMethod instead.", result);
}
private const string MethodDescription = "some description";
private const string ParamDescription = "some parameter description";
private const string ParamName = "paramName";
Expand Down

0 comments on commit 4d4bec5

Please sign in to comment.