Skip to content

Commit

Permalink
fix: json 中有 & 时解析错误 (#687)
Browse files Browse the repository at this point in the history
  • Loading branch information
yansongda authored Sep 26, 2022
1 parent 0f6963a commit 6a730b7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## v3.2.6

### fixed

- fix: json 中有 `&` 时解析错误(#687)

## v3.2.5

### fixed
Expand Down
13 changes: 6 additions & 7 deletions src/Parser/ArrayParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,16 @@ public function parse(?ResponseInterface $response): array

$body = (string) $response->getBody();

if (Str::contains($body, '&')) {
return $this->query($body);
}

$result = json_decode($body, true);
if (JSON_ERROR_NONE === json_last_error()) {
return $result;
}

if (JSON_ERROR_NONE !== json_last_error()) {
throw new InvalidResponseException(Exception::UNPACK_RESPONSE_ERROR, 'Unpack Response Error', ['body' => $body, 'response' => $response]);
if (Str::contains($body, '&')) {
return $this->query($body);
}

return $result;
throw new InvalidResponseException(Exception::UNPACK_RESPONSE_ERROR, 'Unpack Response Error', ['body' => $body, 'response' => $response]);
}

protected function query(string $body): array
Expand Down
12 changes: 12 additions & 0 deletions tests/Parser/ArrayParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ public function testQueryBody()

self::assertEqualsCanonicalizing(['name' => 'yansongda', 'age' => '29'], $result);
}

public function testJsonWith()
{
$url = 'https://yansongda.cn?name=yansongda&age=29';

$response = new Response(200, [], json_encode(['h5_url' => $url]));

$parser = new ArrayParser();
$result = $parser->parse($response);

self::assertEquals('https://yansongda.cn?name=yansongda&age=29', $result['h5_url']);
}
}

0 comments on commit 6a730b7

Please sign in to comment.