diff --git a/Google.Api.Generator.Rest.Tests/NamingTest.cs b/Google.Api.Generator.Rest.Tests/NamingTest.cs new file mode 100644 index 00000000..9d825a7f --- /dev/null +++ b/Google.Api.Generator.Rest.Tests/NamingTest.cs @@ -0,0 +1,35 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using Google.Api.Generator.Rest.Models; +using Google.Api.Generator.Utils.Roslyn; +using Xunit; + +namespace Google.Api.Generator.Rest.Tests +{ + public class NamingTest + { + [Fact] + public void GoogleKeywordIsEscaped() + { + var name = "Google"; + var escapedName = Naming.ToMemberName(name); + + Assert.True(Keywords.IsReservedName(name)); + Assert.NotEqual(escapedName, name, StringComparer.OrdinalIgnoreCase); + Assert.False(Keywords.IsReservedName(escapedName)); + } + } +} diff --git a/Google.Api.Generator.Rest/Models/Naming.cs b/Google.Api.Generator.Rest/Models/Naming.cs index dc98370a..7f08a40a 100644 --- a/Google.Api.Generator.Rest/Models/Naming.cs +++ b/Google.Api.Generator.Rest/Models/Naming.cs @@ -37,7 +37,7 @@ internal static string ToMemberName(this string name, bool addUnderscoresToEscap // We don't really need to escape keywords given that we've upper-cased it, // but this is what the Python code does. - if (addUnderscoresToEscape && Keywords.IsKeyword(upper.ToLowerInvariant())) + if (addUnderscoresToEscape && (Keywords.IsKeyword(upper.ToLowerInvariant()) || Keywords.IsReservedName(upper))) { upper += "__"; } diff --git a/Google.Api.Generator.Utils/Roslyn/Keywords.cs b/Google.Api.Generator.Utils/Roslyn/Keywords.cs index b93d6aa2..94fc4515 100644 --- a/Google.Api.Generator.Utils/Roslyn/Keywords.cs +++ b/Google.Api.Generator.Utils/Roslyn/Keywords.cs @@ -100,8 +100,14 @@ public static class Keywords "while" ); + private static readonly IImmutableSet s_reservedName = ImmutableHashSet.Create( + "Google" // reserved for the root namespace + ); + public static string PrependAtIfKeyword(string word) => IsKeyword(word) ? $"@{word}" : word; public static bool IsKeyword(string word) => s_keywords.Contains(word); + + public static bool IsReservedName(string word) => s_reservedName.Contains(word); } }