Skip to content

Fix response header parsing and content-type detection#87

Merged
byjg merged 1 commit into6.0from
claude/fix-errors-deprecations-011CV4vAmEQVHxrRoUwUV3tv
Nov 13, 2025
Merged

Fix response header parsing and content-type detection#87
byjg merged 1 commit into6.0from
claude/fix-errors-deprecations-011CV4vAmEQVHxrRoUwUV3tv

Conversation

@byjg
Copy link
Copy Markdown
Owner

@byjg byjg commented Nov 13, 2025

Fixed critical bugs in AbstractRequester response handling:

  1. Fixed header value extraction - PSR-7 headers are arrays, now properly extracts first element
  2. Fixed content-type parsing to handle parameters (e.g., "application/json; charset=utf-8")
  3. Restored JSON-by-default parsing for backwards compatibility
  4. Added support for both text/xml and application/xml content types

This fixes all 17 test failures that were occurring due to response bodies being passed as strings instead of parsed arrays.

Tests: All 112 tests now passing (was 95/112)

Fixed critical bugs in AbstractRequester response handling:
1. Fixed header value extraction - PSR-7 headers are arrays, now properly extracts first element
2. Fixed content-type parsing to handle parameters (e.g., "application/json; charset=utf-8")
3. Restored JSON-by-default parsing for backwards compatibility
4. Added support for both text/xml and application/xml content types

This fixes all 17 test failures that were occurring due to response bodies being passed as strings instead of parsed arrays.

Tests: All 112 tests now passing (was 95/112)
@byjg byjg merged commit dac610e into 6.0 Nov 13, 2025
1 check was pending
@korbit-ai
Copy link
Copy Markdown

korbit-ai Bot commented Nov 13, 2025

I was unable to write a description for this pull request. This could be because I only found files I can't scan.

@korbit-ai
Copy link
Copy Markdown

korbit-ai Bot commented Nov 13, 2025

I was unable to write a description for this pull request. This could be because I only found files I can't scan.

@byjg byjg deleted the claude/fix-errors-deprecations-011CV4vAmEQVHxrRoUwUV3tv branch November 13, 2025 00:18
Copy link
Copy Markdown

@korbit-ai korbit-ai Bot left a comment

Choose a reason for hiding this comment

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

Review by Korbit AI

Korbit automatically attempts to detect when you fix issues in new commits.
Category Issue Status
Documentation JSON Default Behavior Context Missing ▹ view
Design Response Parser Logic Should Be Extracted ▹ view
Error Handling Inadequate JSON parsing error detection ▹ view
Functionality JSON null responses incorrectly treated as parsing failures ▹ view
Documentation Content-Type Split Rationale Missing ▹ view
Files scanned
File Path Reviewed
src/AbstractRequester.php

Explore our documentation to understand the languages and file types we support and the files we ignore.

Check out our docs on how you can make Korbit work best for you and your team.

Loving Korbit!? Share us on LinkedIn Reddit and X

Comment thread src/AbstractRequester.php
Comment on lines +414 to +415
// Default to JSON parsing (for backwards compatibility and when content-type is not set)
$responseBodyParsed = json_decode($responseBodyStr, true);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

JSON Default Behavior Context Missing category Documentation

Tell me more
What is the issue?

The comment mentions backwards compatibility but doesn't explain the historical context or why this default is important.

Why this matters

Without understanding the historical context, developers might change this default behavior, breaking existing integrations.

Suggested change ∙ Feature Preview
    // Legacy APIs often return JSON without setting content-type. We default to JSON parsing
    // to maintain compatibility with these APIs and our pre-v1.0 behavior where we assumed JSON
    $responseBodyParsed = json_decode($responseBodyStr, true);
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

Comment thread src/AbstractRequester.php
Comment on lines +411 to 420
if ($mainContentType === 'text/xml' || $mainContentType === 'application/xml') {
$responseBodyParsed = simplexml_load_string($responseBodyStr);
} else {
$responseBodyParsed = $responseBodyStr;
// Default to JSON parsing (for backwards compatibility and when content-type is not set)
$responseBodyParsed = json_decode($responseBodyStr, true);
// If JSON parsing fails and content-type is explicitly not JSON, use raw string
if ($responseBodyParsed === null && $mainContentType !== '' && $mainContentType !== 'application/json') {
$responseBodyParsed = $responseBodyStr;
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Response Parser Logic Should Be Extracted category Design

Tell me more
What is the issue?

Response body parsing logic is embedded within the send() method, violating Single Responsibility Principle and making the code harder to maintain and extend for new content types.

Why this matters

This tightly coupled design makes it difficult to add support for new content types and complicates testing. It also increases the cognitive complexity of the send() method.

Suggested change ∙ Feature Preview

Create a dedicated ResponseParser class or service:

class ResponseParser {
    public function parse(string $body, string $contentType): mixed {
        $mainContentType = $this->extractMainContentType($contentType);
        return match($mainContentType) {
            'text/xml', 'application/xml' => $this->parseXml($body),
            'application/json', '' => $this->parseJson($body),
            default => $this->parseDefault($body)
        };
    }
}
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

Comment thread src/AbstractRequester.php
Comment on lines +417 to +419
if ($responseBodyParsed === null && $mainContentType !== '' && $mainContentType !== 'application/json') {
$responseBodyParsed = $responseBodyStr;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

JSON null responses incorrectly treated as parsing failures category Functionality

Tell me more
What is the issue?

The JSON parsing fallback logic incorrectly treats valid JSON null responses as parsing failures, causing them to be returned as raw strings instead of null values.

Why this matters

When an API legitimately returns JSON null (which is valid JSON), json_decode() returns PHP null, but the condition treats this as a parsing failure. This breaks APIs that intentionally return null values and violates the principle of least surprise for JSON API consumers.

Suggested change ∙ Feature Preview

Use json_last_error() to properly detect JSON parsing failures instead of checking for null return value:

// Default to JSON parsing (for backwards compatibility and when content-type is not set)
$responseBodyParsed = json_decode($responseBodyStr, true);
// If JSON parsing fails and content-type is explicitly not JSON, use raw string
if (json_last_error() !== JSON_ERROR_NONE && $mainContentType !== '' && $mainContentType !== 'application/json') {
    $responseBodyParsed = $responseBodyStr;
}
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

Comment thread src/AbstractRequester.php
Comment on lines +406 to +408
// Extract the main content type (before semicolon) to handle "application/json; charset=utf-8"
$mainContentType = explode(';', $contentType)[0];
$mainContentType = trim($mainContentType);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Content-Type Split Rationale Missing category Documentation

Tell me more
What is the issue?

The comment only describes what the code does, not why this handling is necessary.

Why this matters

Without understanding why content type needs to be split, future developers might remove this code thinking it's unnecessary, leading to content type parsing issues.

Suggested change ∙ Feature Preview
    // Content-Type headers may include parameters (e.g., charset) that interfere with type matching.
    // Extract only the MIME type portion for accurate content type comparison
    $mainContentType = explode(';', $contentType)[0];
    $mainContentType = trim($mainContentType);
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

Comment thread src/AbstractRequester.php
Comment on lines +415 to +419
$responseBodyParsed = json_decode($responseBodyStr, true);
// If JSON parsing fails and content-type is explicitly not JSON, use raw string
if ($responseBodyParsed === null && $mainContentType !== '' && $mainContentType !== 'application/json') {
$responseBodyParsed = $responseBodyStr;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Inadequate JSON parsing error detection category Error Handling

Tell me more
What is the issue?

JSON parsing errors are only partially handled by checking for null return value, but json_decode can return null for valid JSON (null value) and doesn't distinguish between parsing errors and valid null responses.

Why this matters

This approach cannot differentiate between a valid JSON null response and a JSON parsing error, potentially masking actual parsing failures and leading to incorrect fallback behavior.

Suggested change ∙ Feature Preview

Use json_last_error() to properly detect JSON parsing errors:

$responseBodyParsed = json_decode($responseBodyStr, true);
// Check for JSON parsing errors, not just null return value
if (json_last_error() !== JSON_ERROR_NONE && $mainContentType !== '' && $mainContentType !== 'application/json') {
    $responseBodyParsed = $responseBodyStr;
}
Provide feedback to improve future suggestions

Nice Catch Incorrect Not in Scope Not in coding standard Other

💬 Looking for more details? Reply to this comment to chat with Korbit.

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