FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

refactor(grpc): improve Parser::parseResponse return value format by huangdijia · Pull Request #7653 · hyperf/hyperf · GitHub

/ hyperf Public

refactor(grpc): improve Parser::parseResponse return value format - #7653

Merged
limingxinleo merged 26 commits into
hyperf:3.2from
huangdijia:refactor/grpc-parser-response-format
Dec 2, 2025
Merged

refactor(grpc): improve Parser::parseResponse return value format#7653
limingxinleo merged 26 commits into
hyperf:3.2from
huangdijia:refactor/grpc-parser-response-format

Conversation

huangdijia commented Dec 1, 2025
edited
Loading

Copy link
Copy Markdown
Member

Summary

  • Refactored the Parser::parseResponse method to use a more standardized approach
  • Replaced Grpc\StringifyAble dependency with stdClass for better compatibility
  • Updated return type annotation to be more explicit about the return structure
  • Standardized all return paths to use a consistent stdClass status object

Changes Made

  • Import Change: Replaced use Grpc\StringifyAble; with use stdClass;
  • Return Type: Updated PHPDoc to clearly define the return structure as array{0:null|Message,1:stdClass{code:int,details:string,metadata:null|Http2Response}}
  • Code Structure: All return scenarios now use a consistent stdClass status object with:
    • code: Error/status code
    • details: Descriptive message
    • metadata: Response metadata

Test Plan

  • Verify existing gRPC functionality continues to work
  • Test error handling scenarios (no response, invalid status, grpc errors)
  • Test successful response parsing
  • Ensure compatibility with existing code that uses this method

huangdijia marked this pull request as draft December 1, 2025 14:41
huangdijia requested review from Copilot and removed request for limingxinleo December 1, 2025 14:50
huangdijia added this to the v3.2 milestone Dec 1, 2025

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Pull request overview

This PR refactors the Parser::parseResponse method to return a more standardized format using stdClass instead of a mixed-type array. The change replaces the old three-element array format [message, code, response] with a two-element array [message, statusObject] where the status object contains code, details, and metadata fields.

Key Changes:

  • Replaced Grpc\StringifyAble import with stdClass
  • Updated return type annotation to specify structured stdClass status object
  • Standardized all return paths to use consistent stdClass format with code, details, and metadata fields

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/grpc/src/Parser.php Refactored parseResponse method to return structured stdClass status object instead of mixed array elements
CHANGELOG-3.2.md Added changelog entry documenting the improvement

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/grpc/src/Parser.php Outdated
Comment thread src/grpc/src/Parser.php Outdated
Comment thread src/grpc/src/Parser.php Outdated

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread CHANGELOG-3.2.md Outdated
Comment thread src/grpc/src/Parser.php Outdated
Comment thread src/grpc/src/Parser.php Outdated
Comment on lines +64 to +68
return [
self::deserializeMessage($deserialize, $response->data ?? ''),
self::statusFromResponse($response),
$response,
];

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Critical error handling logic has been removed without replacement. The old implementation checked for:

  1. Invalid HTTP status codes (via isInvalidStatus()) - returning error when statusCode was not 0, 200, or 400
  2. Non-zero gRPC status codes (grpc-status !== 0) - returning the gRPC error message

The new implementation only calls statusFromResponse() which only extracts grpc-status-details-bin header. This means:

  • Invalid HTTP status codes are no longer detected or reported
  • gRPC error status codes (from grpc-status header) are not being checked or returned
  • Error messages from grpc-message header are lost

The method now deserializes the response data unconditionally, even when there are errors, which could lead to deserialization failures or incorrect behavior. The error handling logic should be restored to properly validate the response before attempting deserialization.

Copilot uses AI. Check for mistakes.
huangdijia and others added 17 commits December 2, 2025 15:45
- Replace Grpc\StringifyAble with stdClass for better standardization
- Update return type annotation to be more explicit about the structure
- Standardize all return paths to use consistent stdClass status object
- Improve code readability and maintainability
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
huangdijia force-pushed the refactor/grpc-parser-response-format branch from 7386f76 to 2a23528 Compare December 2, 2025 07:45

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Pull request overview

Copilot reviewed 6 out of 6 changed files in this pull request and generated 8 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/grpc/src/Parser.php
if (in_array($lowerKey, ['content-type', 'content-length', 'te'])) {
continue;
}
// 处理-bin结尾 metadata

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

[nitpick] The comment contains Chinese characters which may not be clear to all contributors. Consider translating to English: "Handle -bin suffixed metadata"

Suggested change
// 处理-bin结尾 metadata
// Handle -bin suffixed metadata

Copilot uses AI. Check for mistakes.
$status = new stdClass();
$status->code = 0;
$status->details = 'OK';
$status->metadata = [];

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The metadata is initialized as an empty array on line 57, but should be initialized to match the documented return type. According to the PHPDoc annotations, metadata should be typed as null|Http2Response in the return type (lines 23, 35, 50), not as an array. Either change line 57 to $status->metadata = null; or update the PHPDoc to reflect that metadata is an array of parsed headers.

Suggested change
$status->metadata = [];
$status->metadata = null;

Copilot uses AI. Check for mistakes.
Comment thread src/grpc/src/Parser.php
if (str_starts_with($lowerKey, 'grpc-') && $lowerKey !== 'grpc-status-details-bin') {
continue;
}
// 忽略http2预留伪头

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

[nitpick] The comment contains Chinese characters which may not be clear to all contributors. Consider translating to English: "Ignore HTTP/2 reserved pseudo-headers"

Suggested change
// 忽略http2预留伪头
// Ignore HTTP/2 reserved pseudo-headers

Copilot uses AI. Check for mistakes.
Comment thread src/grpc/src/Parser.php
if (str_starts_with($lowerKey, ':')) {
continue;
}
// 忽略 HTTP/2 传输层头部

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

[nitpick] The comment contains Chinese characters which may not be clear to all contributors. Consider translating to English: "Ignore HTTP/2 transport layer headers"

Suggested change
// 忽略 HTTP/2 传输层头部
// Ignore HTTP/2 transport layer headers

Copilot uses AI. Check for mistakes.
Comment thread src/grpc/src/Parser.php Outdated
}, $this->options['retry_interval'] ?? 100);
return Parser::parseResponse($this->recv($streamId), $deserialize);

return new UnaryCall($this, $streamId, $deserialize);

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This change breaks existing tests. The method now returns a UnaryCall object instead of directly parsing and returning the response. However, BaseClientTest.php expects _simpleRequest to return a numeric value (stream ID) in tests like testGrpcClientReconnect() which calls $this->assertGreaterThan(0, $client->sayHello()). Since sayHello() returns the result of _simpleRequest, these tests will fail. The tests need to be updated to call ->wait() on the returned UnaryCall object, or test coverage should be added for the new return behavior.

Copilot uses AI. Check for mistakes.
$status->code = 0;
$status->details = 'OK';
$status->metadata = [];
$status->rawResponse = $response;

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

The rawResponse property is being added to the status object but is not documented in the return type annotation. The PHPDoc at line 35 and 50 should include rawResponse in the stdClass structure definition: stdClass{code:int,details:string,metadata:array,rawResponse:null|Http2Response}

Copilot uses AI. Check for mistakes.
Comment thread src/grpc/src/Parser.php

foreach ($response->headers as $key => $value) {
$lowerKey = strtolower($key);
// 忽略grpc官方预留,将grpc-status-details-bin保留,可解析为Google\Rpc\Status

Copilot AI Dec 2, 2025

Copy link

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

[nitpick] The comment contains Chinese characters which may not be clear to all contributors. Consider translating to English: "Ignore official gRPC reserved headers, keep grpc-status-details-bin which can be parsed as Google\Rpc\Status"

Suggested change
// 忽略grpc官方预留,将grpc-status-details-bin保留,可解析为Google\Rpc\Status
// Ignore official gRPC reserved headers, keep grpc-status-details-bin which can be parsed as Google\Rpc\Status

Copilot uses AI. Check for mistakes.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
limingxinleo merged commit 5b71af8 into hyperf:3.2 Dec 2, 2025
huangdijia deleted the refactor/grpc-parser-response-format branch December 2, 2025 11:49
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

3 participants


Back | FazBrowse Home | New Git URL