Skip to content

feat(Storage) : Implement Object Contexts with filter and validation#2

Open
salilg-eng wants to merge 64 commits intomainfrom
feat/object-context
Open

feat(Storage) : Implement Object Contexts with filter and validation#2
salilg-eng wants to merge 64 commits intomainfrom
feat/object-context

Conversation

@salilg-eng
Copy link
Copy Markdown
Owner

@salilg-eng salilg-eng commented Apr 14, 2026

  • Implemented validateContexts() to enforce strict type checks. It now throws InvalidArgumentException if custom contexts are not arrays or if values do not follow alphanumeric naming constraints (no double quotes or forward slashes
  • Ensured logic validating context structures before initiating API requests.
  • Add comprehensive unit and system tests covering CRUD, server-side
    operations, and complex filtering scenarios.

@salilg-eng
Copy link
Copy Markdown
Owner Author

/Gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for object contexts in Cloud Storage, enabling the attachment of key-payload pairs to objects for identification and filtering. The changes include updates to the service definition, connection handling, and the addition of validation logic for custom contexts. Feedback highlights the need for consistent validation across all object modification methods—such as update, copy, and rewrite—and suggests refining validation regex and error messages. Additionally, corrections were requested for a misleading internal comment and the incorrect placement of contexts within a unit test's metadata array.

Comment thread Storage/src/Connection/Rest.php Outdated
Comment on lines +517 to +518
// during object creation context properties go into metadata
// but not into request body
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This comment is misleading. In the Cloud Storage JSON API, contexts is a property of the Object resource. When calling insertObject, the metadata provided in $args['metadata'] represents the object resource and constitutes the request body. Thus, it is indeed part of the request body.

            // during object creation context properties are part of the object resource
            // and should be included in the request body.

Repository owner deleted a comment from gemini-code-assist bot Apr 14, 2026
Repository owner deleted a comment from gemini-code-assist bot Apr 14, 2026
Repository owner deleted a comment from gemini-code-assist bot Apr 14, 2026
Repository owner deleted a comment from gemini-code-assist bot Apr 14, 2026
@salilg-eng salilg-eng marked this pull request as ready for review April 14, 2026 18:17
@salilg-eng salilg-eng self-assigned this Apr 14, 2026
Copy link
Copy Markdown
Collaborator

@thiyaguk09 thiyaguk09 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add the missing tests?

@@ -215,12 +217,284 @@ public function testObjectRetentionUnlockedMode()
$this->assertFalse($object->exists());
}

public function testObjectExists()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not remove the existing test testObjectExists.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my bad I added

$this->assertFalse($object->exists());
}

public function testGetContextAndServerGenratedTimes()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits: typo

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced with this testGetContextsWithServerTime

{
$initialContexts = [
'custom' => [
'tag' => ['value' => 'orignal'],
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits: typo

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

$inherited = $object->rewrite(self::$bucket, ['name' => 'inherit-' . uniqid()]);
$info = $inherited->info();

$this->assertEquals('orignal', $info['contexts']['custom']['tag']['value']);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits: typo

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed

],
];

$object = $this->createObjectWithContexts($initialContexts);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure the object has context first.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I ensure that and according to added


const CONTEXT_OBJECT_KEY = 'insert-key';
const CONTEXT_OBJECT_VALUE = 'insert-val';
const CONTEXT_OBJECT_PREFIX = 'object-contexts-';
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nits: If this is not being reused, go with a static value, similar to how it is added in other tests.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this constant and directly used static

}
}

public function removeAndClearAllContextsDataProvider()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These scenarios are fundamentally wrong; you are performing the same operation twice with the same key.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove the expected value from dataProvider and compare directly in the method testRemoveAndClearAllObjectContexts.

Comment thread Storage/tests/Unit/BucketTest.php Outdated
);
}

public function testGetFiltersByPresenceOfKeyValuePair()
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is a list operation? Why is it named "get" here? Please correct this.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

Comment thread Storage/tests/Unit/BucketTest.php Outdated
$iterator = $bucket->objects([
'filter' => $filter
]);
$iterator->current();
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Schemes test without assert!

Comment thread Storage/tests/Unit/BucketTest.php Outdated
'metadata' => [
'contexts' => [
'custom' => [
'dept' => ['value' => 'eng', 'createTime' => '...']
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use DateProvider instead of '...', and include all values in the get method since the update time is currently missing.

@salilg-eng
Copy link
Copy Markdown
Owner Author

/Gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for object contexts in Google Cloud Storage, enabling users to attach and filter by key-payload pairs. The implementation includes metadata handling in the REST connection, validation logic for context keys and values during upload, and updates to the service definition for context-based filtering. Extensive unit and system tests were added to verify CRUD operations and filtering. Feedback suggests refactoring the validation logic into a shared trait to ensure consistency across other operations like update and copy, and improving the granularity of error messages for context value validation.

Comment thread Storage/src/Bucket.php
Comment on lines +373 to +375
if ($val !== '' && !preg_match('/^[a-zA-Z0-9][^"\/]*$/', $val)) {
throw new \InvalidArgumentException('Object context value must start with an alphanumeric.');
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

For consistency with the key validation logic and to provide more specific error messages, consider splitting the value validation into separate checks. The current error message is also incomplete as it doesn't mention the restrictions on double quotes and forward slashes.

            if ($val !== '') {
                if (!preg_match('/^[a-zA-Z0-9]/', $val)) {
                    throw new \InvalidArgumentException('Object context value must start with an alphanumeric.');
                }
                if (preg_match('/["\/]/', $val)) {
                    throw new \InvalidArgumentException('Object context value cannot contain double quotes or forward slashes.');
                }
            }

Repository owner deleted a comment from gemini-code-assist bot Apr 17, 2026
Repository owner deleted a comment from gemini-code-assist bot Apr 17, 2026
@salilg-eng
Copy link
Copy Markdown
Owner Author

/Gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for object contexts in Google Cloud Storage, enabling users to attach, update, and filter objects based on custom key-value pairs. The changes span the service definition, REST connection handling, and core library classes, accompanied by extensive system and unit tests. Feedback focuses on improving the reusability of validation logic across different object operations, ensuring that null values are correctly handled to allow clearing contexts, and refining error messages and documentation for better consistency and accuracy.

Comment thread Storage/src/Bucket.php
Comment on lines +373 to +375
if ($val !== '' && !preg_match('/^[a-zA-Z0-9][^"\/]*$/', $val)) {
throw new \InvalidArgumentException('Object context value must start with an alphanumeric.');
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The error message is misleading. If the value starts with an alphanumeric character but contains a forward slash or a double quote, the regex will fail, but the exception message will only mention the alphanumeric requirement. It should be updated to reflect all constraints.

            if ($val !== '' && !preg_match('/^[a-zA-Z0-9][^"\/]*$/', $val)) {
                throw new \InvalidArgumentException('Object context value must start with an alphanumeric and cannot contain double quotes or forward slashes.');
            }

Comment on lines +239 to +241
* @type string $contexts.custom.{key}.value The value associated with the context.
* Must start with an alphanumeric character and cannot contain double quotes (`"`)
* or forward slashes (`/`).
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The documentation for the context value here is inconsistent with the one in Bucket.php. It is missing the 'If not empty' qualifier, which is important because the validation logic allows empty strings.

     *     @type string $contexts.custom.{key}.value The value associated with the context.
     *           If not empty, must start with an alphanumeric character and cannot contain double quotes (`"`)
     *           or forward slashes (`/`).

@salilg-eng salilg-eng requested a review from thiyaguk09 April 17, 2026 06:06
Repository owner deleted a comment from gemini-code-assist bot Apr 17, 2026
Repository owner deleted a comment from gemini-code-assist bot Apr 17, 2026
@salilg-eng salilg-eng requested a review from thiyaguk09 April 17, 2026 08:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants