From a60ccec6feadd3da6df874e0d493612d33816ef7 Mon Sep 17 00:00:00 2001 From: Robert Voinescu Date: Wed, 5 Aug 2026 21:12:53 +0000 Subject: [PATCH] fix: suppress warnings on enumeration of deprecated fields --- Google.Api.Generator.Utils/Roslyn/RoslynExtensions.cs | 6 ++++++ Google.Api.Generator/Generation/MethodDetails.cs | 2 ++ Google.Api.Generator/Generation/ServiceCodeGenerator.cs | 2 +- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Google.Api.Generator.Utils/Roslyn/RoslynExtensions.cs b/Google.Api.Generator.Utils/Roslyn/RoslynExtensions.cs index f97c4fdc..198f733f 100644 --- a/Google.Api.Generator.Utils/Roslyn/RoslynExtensions.cs +++ b/Google.Api.Generator.Utils/Roslyn/RoslynExtensions.cs @@ -416,9 +416,15 @@ public static SwitchStatementSyntax WithDefault(this SwitchStatementSyntax @swit public static SimpleNameSyntax MaybeWithPragmaDisableObsoleteWarning(this SimpleNameSyntax syntax, bool obsolete) => obsolete ? syntax.WithPragmaWarning(PragmaWarnings.Obsolete) : syntax; + public static PropertyDeclarationSyntax MaybeWithPragmaDisableObsoleteWarning(this PropertyDeclarationSyntax syntax, bool obsolete) => + obsolete ? syntax.WithPragmaWarning(PragmaWarnings.Obsolete) : syntax; + public static SimpleNameSyntax WithPragmaWarning(this SimpleNameSyntax syntax, string errorCode) => syntax.WithIdentifier(syntax.Identifier.WithPragmaWarning(errorCode)); + public static PropertyDeclarationSyntax WithPragmaWarning(this PropertyDeclarationSyntax syntax, string errorCode) => + syntax.WithIdentifier(syntax.Identifier.WithPragmaWarning(errorCode)); + public static SyntaxToken WithPragmaWarning(this SyntaxToken token, string errorCode) => token.WithAdditionalAnnotations(new SyntaxAnnotation(PragmaWarnings.AnnotationKind, errorCode)); diff --git a/Google.Api.Generator/Generation/MethodDetails.cs b/Google.Api.Generator/Generation/MethodDetails.cs index 747bb493..f3ebf3e5 100644 --- a/Google.Api.Generator/Generation/MethodDetails.cs +++ b/Google.Api.Generator/Generation/MethodDetails.cs @@ -80,6 +80,7 @@ public Paginated(ServiceDetails svc, MethodDescriptor desc, SyncGrpcType = Typ.Generic(typeof(GrpcPagedEnumerable<,,>), RequestTyp, ResponseTyp, ResourceTyp); AsyncGrpcType = Typ.Generic(typeof(GrpcPagedAsyncEnumerable<,,>), RequestTyp, ResponseTyp, ResourceTyp); ResourcesFieldName = responseResourceField.CSharpPropertyName(); + ResponseResourceFieldIsDeprecated = responseResourceField.IsDeprecated(); PageSizeFieldNumber = pageSizeFieldNumber; PageTokenFieldNumber = pageTokenFieldNumber; } @@ -96,6 +97,7 @@ public Paginated(ServiceDetails svc, MethodDescriptor desc, public Typ SyncGrpcType { get; } public Typ AsyncGrpcType { get; } public string ResourcesFieldName { get; } + public bool ResponseResourceFieldIsDeprecated { get; } public int PageSizeFieldNumber { get; } public int PageTokenFieldNumber { get; } } diff --git a/Google.Api.Generator/Generation/ServiceCodeGenerator.cs b/Google.Api.Generator/Generation/ServiceCodeGenerator.cs index 4137c6db..a53eacbe 100644 --- a/Google.Api.Generator/Generation/ServiceCodeGenerator.cs +++ b/Google.Api.Generator/Generation/ServiceCodeGenerator.cs @@ -71,7 +71,7 @@ private static IEnumerable PaginatedPartialClasses(Sour { var propertyName = method.ResourcesFieldName; var genericGetEnumerator = Method(Public, ctx.Type(Typ.Generic(typeof(IEnumerator<>), method.ResourceTyp)), "GetEnumerator")() - .WithBody(Property(Public, ctx.TypeDontCare, propertyName).Call(nameof(IEnumerable.GetEnumerator))()) + .WithBody(Property(Public, ctx.TypeDontCare, propertyName).MaybeWithPragmaDisableObsoleteWarning(method.ResponseResourceFieldIsDeprecated).Call(nameof(IEnumerable.GetEnumerator))()) .WithXmlDoc(XmlDoc.Summary("Returns an enumerator that iterates through the resources in this response.")); var getEnumerator = Method(None, ctx.Type(), "GetEnumerator")() .WithExplicitInterfaceSpecifier(ctx.Type())