From 61a1f014ac005b930997fcb108986e556e15fc90 Mon Sep 17 00:00:00 2001 From: leomunsa Date: Wed, 2 Jul 2025 16:01:04 -0300 Subject: [PATCH 1/3] Insert xml logic with body requests --- composer.json | 3 ++- src/AbstractRequester.php | 35 ++++++++++++++++++----------- tests/AbstractRequesterTest.php | 39 +++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 064e2d3..4a80a07 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,8 @@ "require": { "php": ">=8.1 <8.4", "ext-json": "*", - "byjg/webrequest": "^5.0" + "byjg/webrequest": "^5.0", + "byjg/xmlutil": "^5.0" }, "require-dev": { "phpunit/phpunit": "^9.6", diff --git a/src/AbstractRequester.php b/src/AbstractRequester.php index 18ad4ad..422c19a 100644 --- a/src/AbstractRequester.php +++ b/src/AbstractRequester.php @@ -17,6 +17,8 @@ use ByJG\WebRequest\Psr7\Request; use ByJG\Util\Uri; use ByJG\WebRequest\Psr7\MemoryStream; +use ByJG\XmlUtil\XmlDocument; +use ByJG\XmlUtil\XmlNode; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -230,21 +232,28 @@ public function send(): ResponseInterface $this->psr7Request = $this->psr7Request->withUri($uri); // Prepare Body to Match Against Specification - $requestBody = $this->psr7Request->getBody()->getContents(); - if (!empty($requestBody)) { - $contentType = $this->psr7Request->getHeaderLine("content-type"); - if (empty($contentType) || str_contains($contentType, "application/json")) { - $requestBody = json_decode($requestBody, true); - } elseif (str_contains($contentType, "multipart/")) { - $requestBody = $this->parseMultiPartForm($contentType, $requestBody); - } else { - throw new InvalidRequestException("Cannot handle Content Type '$contentType'"); + $rawBody = $this->psr7Request->getBody()->getContents(); + $contentType = $this->psr7Request->getHeaderLine("content-type"); + + if (str_contains($contentType, 'application/xml') || str_contains($contentType, 'text/xml')) { + if (!empty($rawBody)) { + new XmlDocument($rawBody); + } + } else { + $requestBody = null; + if (!empty($rawBody)) { + if (empty($contentType) || str_contains($contentType, "application/json")) { + $requestBody = json_decode($rawBody, true); + } elseif (str_contains($contentType, "multipart/")) { + $requestBody = $this->parseMultiPartForm($contentType, $rawBody); + } else { + throw new InvalidRequestException("Cannot handle Content Type '$contentType'"); + } } - } - // Check if the body is the expected before request - $bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod()); - $bodyRequestDef->match($requestBody); + $bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod()); + $bodyRequestDef->match($requestBody); + } // Handle Request $response = $this->handleRequest($this->psr7Request); diff --git a/tests/AbstractRequesterTest.php b/tests/AbstractRequesterTest.php index 0c4a1db..35203a2 100644 --- a/tests/AbstractRequesterTest.php +++ b/tests/AbstractRequesterTest.php @@ -380,4 +380,43 @@ public function testValidateAssertBodyNotContains() $this->assertRequest($request); } + + /** + * @throws DefinitionNotFoundException + * @throws GenericSwaggerException + * @throws HttpMethodNotFoundException + * @throws InvalidDefinitionException + * @throws InvalidRequestException + * @throws NotMatchedException + * @throws PathNotFoundException + * @throws RequiredArgumentNotFound + * @throws StatusCodeNotMatchedException + * @throws MessageException + * @throws RequestException + */ + public function testPostPetWithXmlBody() + { + $xmlBody = + '' . + 'Garfield XML' . + 'available' . + '' . + 'http://example.com/garfield.png' . + '' . + '' . + 'Cats' . + '' . + ''; + + $expectedResponse = Response::getInstance(200); + + $request = new MockRequester($expectedResponse); + $request + ->withMethod('POST') + ->withPath('/pet') + ->withRequestHeader(['Content-Type' => 'application/xml']) + ->withRequestBody($xmlBody); + + $this->assertRequest($request); + } } From 5173d91a42195589f2741c91a467e126a213dbc2 Mon Sep 17 00:00:00 2001 From: leomunsa Date: Wed, 2 Jul 2025 16:27:13 -0300 Subject: [PATCH 2/3] Fix --- src/AbstractRequester.php | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/AbstractRequester.php b/src/AbstractRequester.php index 422c19a..0495200 100644 --- a/src/AbstractRequester.php +++ b/src/AbstractRequester.php @@ -233,26 +233,24 @@ public function send(): ResponseInterface // Prepare Body to Match Against Specification $rawBody = $this->psr7Request->getBody()->getContents(); + $requestBody = null; $contentType = $this->psr7Request->getHeaderLine("content-type"); - - if (str_contains($contentType, 'application/xml') || str_contains($contentType, 'text/xml')) { - if (!empty($rawBody)) { + if (!empty($rawBody)) { + if (str_contains($contentType, 'application/xml') || str_contains($contentType, 'text/xml')) { new XmlDocument($rawBody); - } - } else { - $requestBody = null; - if (!empty($rawBody)) { - if (empty($contentType) || str_contains($contentType, "application/json")) { - $requestBody = json_decode($rawBody, true); - } elseif (str_contains($contentType, "multipart/")) { - $requestBody = $this->parseMultiPartForm($contentType, $rawBody); - } else { - throw new InvalidRequestException("Cannot handle Content Type '$contentType'"); - } + } elseif (empty($contentType) || str_contains($contentType, "application/json")) { + $requestBody = json_decode($rawBody, true); + } elseif (str_contains($contentType, "multipart/")) { + $requestBody = $this->parseMultiPartForm($contentType, $rawBody); + } else { + throw new InvalidRequestException("Cannot handle Content Type '$contentType'"); } - $bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod()); - $bodyRequestDef->match($requestBody); + // Check if the body is the expected before request + if (!is_null($requestBody)) { + $bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod()); + $bodyRequestDef->match($requestBody); + } } // Handle Request From 64231f07f0b6808ce733140e615ff82eef947753 Mon Sep 17 00:00:00 2001 From: Joao Gilberto Magalhaes Date: Wed, 2 Jul 2025 16:44:30 -0500 Subject: [PATCH 3/3] Refactor body content handling to fix XML and JSON validation logic. --- src/AbstractRequester.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/AbstractRequester.php b/src/AbstractRequester.php index 0495200..c166871 100644 --- a/src/AbstractRequester.php +++ b/src/AbstractRequester.php @@ -12,13 +12,12 @@ use ByJG\ApiTools\Exception\PathNotFoundException; use ByJG\ApiTools\Exception\RequiredArgumentNotFound; use ByJG\ApiTools\Exception\StatusCodeNotMatchedException; +use ByJG\Util\Uri; use ByJG\WebRequest\Exception\MessageException; use ByJG\WebRequest\Exception\RequestException; -use ByJG\WebRequest\Psr7\Request; -use ByJG\Util\Uri; use ByJG\WebRequest\Psr7\MemoryStream; +use ByJG\WebRequest\Psr7\Request; use ByJG\XmlUtil\XmlDocument; -use ByJG\XmlUtil\XmlNode; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -233,11 +232,12 @@ public function send(): ResponseInterface // Prepare Body to Match Against Specification $rawBody = $this->psr7Request->getBody()->getContents(); + $isXmlBody = false; $requestBody = null; $contentType = $this->psr7Request->getHeaderLine("content-type"); if (!empty($rawBody)) { if (str_contains($contentType, 'application/xml') || str_contains($contentType, 'text/xml')) { - new XmlDocument($rawBody); + $isXmlBody = new XmlDocument($rawBody); } elseif (empty($contentType) || str_contains($contentType, "application/json")) { $requestBody = json_decode($rawBody, true); } elseif (str_contains($contentType, "multipart/")) { @@ -246,11 +246,12 @@ public function send(): ResponseInterface throw new InvalidRequestException("Cannot handle Content Type '$contentType'"); } - // Check if the body is the expected before request - if (!is_null($requestBody)) { - $bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod()); - $bodyRequestDef->match($requestBody); - } + } + + // Check if the body is the expected before request + if ($isXmlBody === false) { + $bodyRequestDef = $this->schema->getRequestParameters($this->psr7Request->getUri()->getPath(), $this->psr7Request->getMethod()); + $bodyRequestDef->match($requestBody); } // Handle Request