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

Source generated models should not scream on CS0618 #5229

Merged
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 @@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Expand properties types with null type for Typescript. [#4993](https://github.com/microsoft/kiota-typescript/issues/1188)
- Added Collection, HashMap, Map, Objects, InputStream, BigDecimal to the list of reserved names for Java generation. [#5135](https://github.com/microsoft/kiota/issues/5135)
- C# refiner now fixes data types for indexers. [#5201](https://github.com/microsoft/kiota/issues/5201)
- C# do not report CS0618 in the generated code. [#5229](https://github.com/microsoft/kiota/issues/5229)

## [1.17.0] - 2024-08-09

Expand Down
13 changes: 13 additions & 0 deletions src/Kiota.Builder/Writers/CSharp/CSharpConventionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class CSharpConventionService : CommonLanguageConventionService
public const string NullableEnableDirective = "#nullable enable";
public const string NullableRestoreDirective = "#nullable restore";

public const string CS0618 = "CS0618";
public const string CS1591 = "CS1591";

public static void WriteNullableOpening(LanguageWriter writer)
{
ArgumentNullException.ThrowIfNull(writer);
Expand All @@ -38,6 +41,16 @@ public static void WriteNullableClosing(LanguageWriter writer)
ArgumentNullException.ThrowIfNull(writer);
writer.WriteLine("#endif", false);
}
public void WritePragmaDisable(LanguageWriter writer, string code)
{
ArgumentNullException.ThrowIfNull(writer);
writer.WriteLine($"#pragma warning disable {code}");
}
public void WritePragmaRestore(LanguageWriter writer, string code)
{
ArgumentNullException.ThrowIfNull(writer);
writer.WriteLine($"#pragma warning restore {code}");
}
private const string ReferenceTypePrefix = "<see cref=\"";
private const string ReferenceTypeSuffix = "\"/>";
#pragma warning disable S1006 // Method overrides should not change parameter defaults
Expand Down
1 change: 1 addition & 0 deletions src/Kiota.Builder/Writers/CSharp/CodeBlockEndWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public override void WriteCodeElement(BlockEnd codeElement, LanguageWriter write
if (codeElement?.Parent is CodeClass codeClass && codeClass.Parent is CodeNamespace)
{
writer.CloseBlock();
conventions.WritePragmaRestore(writer, CSharpConventionService.CS0618);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit
if (codeElement.Parent?.Parent is CodeNamespace)
{
writer.WriteLine(AutoGenerationHeader);
conventions.WritePragmaDisable(writer, CSharpConventionService.CS0618);
codeElement.Usings
.Where(x => (x.Declaration?.IsExternal ?? true) || !x.Declaration.Name.Equals(codeElement.Name, StringComparison.OrdinalIgnoreCase)) // needed for circular requests patterns like message folder
.Select(static x => x.Declaration?.IsExternal ?? false ?
Expand All @@ -39,9 +40,9 @@ public override void WriteCodeElement(ClassDeclaration codeElement, LanguageWrit
bool hasDescription = conventions.WriteLongDescription(parentClass, writer);
conventions.WriteDeprecationAttribute(parentClass, writer);
writer.WriteLine(GeneratedCodeAttribute);
if (!hasDescription) writer.WriteLine("#pragma warning disable CS1591");
if (!hasDescription) conventions.WritePragmaDisable(writer, CSharpConventionService.CS1591);
writer.WriteLine($"public partial class {codeElement.Name.ToFirstCharacterUpperCase()} {derivation}");
if (!hasDescription) writer.WriteLine("#pragma warning restore CS1591");
if (!hasDescription) conventions.WritePragmaRestore(writer, CSharpConventionService.CS1591);
writer.StartBlock();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ public void WritesSimpleDeclaration()
Assert.Contains("public partial class", result);
}

[Fact]
public void WritesWarningDisableCs0618()
{
codeElementWriter.WriteCodeElement(parentClass.StartBlock, writer);
var result = tw.ToString();
Assert.Contains("#pragma warning disable CS0618", result);
}

[Fact]
public void WritesImplementation()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ public void ClosesNestedClasses()
codeElementWriter.WriteCodeElement(child.EndBlock, writer);
var result = tw.ToString();
Assert.Equal(1, result.Count(x => x == '}'));
Assert.DoesNotContain("#pragma warning restore CS0618", result);
}
[Fact]
public void WritesWarningRestoreCs0618()
{
codeElementWriter.WriteCodeElement(parentClass.EndBlock, writer);
var result = tw.ToString();
Assert.Contains("#pragma warning restore CS0618", result);
}
[Fact]
public void ClosesNonNestedClasses()
Expand Down
Loading