Skip to content

Commit 89d96fe

Browse files
authored
fix(SecurityCenter): rename class using reserved PHP "object" keyword (#9194)
* fix: manually rename class using reserved PHP "object" keyword * add descriptor fix * fix descriptor fix for protobuf extension * revert changes to bigquerystorage descriptor fix
1 parent 0e291fb commit 89d96fe

8 files changed

Lines changed: 91 additions & 12 deletions

File tree

SecurityCenter/composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"psr-4": {
88
"Google\\Cloud\\SecurityCenter\\": "src",
99
"GPBMetadata\\Google\\Cloud\\Securitycenter\\": "metadata"
10-
}
10+
},
11+
"files": [
12+
"metadata/descriptor_fix.php"
13+
]
1114
},
1215
"autoload-dev": {
1316
"psr-4": {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/*
3+
* Copyright 2026 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* IMPORTANT: This file prevents an exception when `PBObject` is used.
20+
* The `Object` class from `google/cloud/securitycenter/v1/kubernetes.proto` was manually
21+
* renamed to `PBObject` in PHP to avoid a conflict with a PHP reserved keyword.
22+
* This file ensures that the `PBObject` class is correctly registered with the
23+
* Protobuf descriptor pool under its original `Object` descriptor.
24+
*
25+
* This file can be removed once the protobuf library introduces a fix for
26+
* descriptor pool issues when classes are renamed due to reserved keywords.
27+
* @see https://github.com/protocolbuffers/protobuf/pull/27456
28+
*/
29+
namespace GPBMetadata\Google\Cloud\Securitycenter\V1 {
30+
31+
class DescriptorFix
32+
{
33+
public static $is_initialized = false;
34+
35+
public static function initOnce() {
36+
if (static::$is_initialized == true) {
37+
return;
38+
}
39+
40+
$pool = \Google\Protobuf\Internal\DescriptorPool::getGeneratedPool();
41+
if ($pool instanceof \Google\Protobuf\Internal\DescriptorPool) {
42+
$pool->addMessage(
43+
'google.cloud.securitycenter.v1.Kubernetes.Object',
44+
\Google\Cloud\SecurityCenter\V1\Kubernetes\PBObject::class
45+
)->finalizeToPool();
46+
}
47+
48+
static::$is_initialized = true;
49+
}
50+
}
51+
52+
DescriptorFix::initOnce();
53+
}
54+

SecurityCenter/owlbot.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"""This script is used to synthesize generated parts of this library."""
1616

1717
import logging
18+
import os
1819
from pathlib import Path
1920
import subprocess
2021

@@ -32,6 +33,26 @@
3233

3334
php.owlbot_main(src=src, dest=dest)
3435

36+
# Rename "Object" to "PBObject" because "Object" is a reserved word in PHP
37+
for version in ["V1", "V2"]:
38+
path = f'src/{version}/Kubernetes/Object.php'
39+
if Path(path).exists():
40+
s.replace(
41+
path,
42+
r'class Object',
43+
r'class PBObject'
44+
)
45+
s.move(path, f'src/{version}/Kubernetes/PBObject.php')
46+
if Path(path).exists():
47+
os.remove(path)
48+
49+
# Update typehints
50+
s.replace(
51+
'src/**/*.php',
52+
r'Kubernetes\\Object',
53+
r'Kubernetes\\PBObject'
54+
)
55+
3556
# format generated clients
3657
subprocess.run([
3758
'npm',

SecurityCenter/src/V1/Kubernetes.php

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SecurityCenter/src/V1/Kubernetes/Object.php renamed to SecurityCenter/src/V1/Kubernetes/PBObject.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SecurityCenter/src/V2/Kubernetes.php

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

SecurityCenter/src/V2/Kubernetes/Object.php renamed to SecurityCenter/src/V2/Kubernetes/PBObject.php

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,7 +797,8 @@
797797
"Grafeas\\": "Grafeas/src"
798798
},
799799
"files": [
800-
"BigQueryStorage/metadata/descriptor_fix.php"
800+
"BigQueryStorage/metadata/descriptor_fix.php",
801+
"SecurityCenter/metadata/descriptor_fix.php"
801802
]
802803
},
803804
"autoload-dev": {

0 commit comments

Comments
 (0)