diff --git a/WebFiori/Http/WebService.php b/WebFiori/Http/WebService.php index b999fbc..1090897 100644 --- a/WebFiori/Http/WebService.php +++ b/WebFiori/Http/WebService.php @@ -1331,12 +1331,24 @@ protected function handleMethodResponse(mixed $result, string $methodName): void } $responseBody = $responseBodyAttrs[0]->newInstance(); + $contentType = $responseBody->contentType; // Handle custom content types - if ($responseBody->contentType !== 'application/json') { + if ($contentType !== 'application/json') { // For non-JSON content types, send raw result - $content = is_string($result) ? $result : (is_array($result) || is_object($result) ? json_encode($result) : (string)$result); - $this->send($responseBody->contentType, $content, $responseBody->status); + if (is_array($result)) { + $content = new Json(); + $content->addArray('data', $result); + $contentType = 'application/json'; + } else if (is_object($result)) { + $content = new Json(); + $content->addObject('data', $result); + $contentType = 'application/json'; + } else { + $content = (string)$result; + } + + $this->send($contentType, $content, $responseBody->status); return; } @@ -1345,12 +1357,20 @@ protected function handleMethodResponse(mixed $result, string $methodName): void if ($result === null) { // Null return = empty response with configured status $this->sendResponse('', $responseBody->status, $responseBody->type); - } elseif (is_array($result) || is_object($result)) { + } else if (is_array($result) || is_object($result)) { // Array/object = JSON response - $this->sendResponse('Success', $responseBody->status, $responseBody->type, $result); + if ($result instanceof Json) { + $json = $result; + } else if ($result instanceof JsonI) { + $json = $result->toJSON(); + } else { + $json = new Json(); + $json->add('data', $result); + } + $this->send($responseBody->contentType, $json, $responseBody->status); } else { // String/scalar = plain response - $this->sendResponse($result, $responseBody->status, $responseBody->type); + $this->send($responseBody->contentType, $result, $responseBody->status); } } } diff --git a/composer.json b/composer.json index 8b41c3c..2443cca 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,8 @@ }, "scripts": { "test": "phpunit --configuration tests/phpunit.xml", - "test10": "phpunit --configuration tests/phpunit10.xml" + "test10": "phpunit --configuration tests/phpunit10.xml", + "fix-cs": "php-cs-fixer fix --config=php_cs.php.dist" }, "require-dev": { "phpunit/phpunit": "^10.0", diff --git a/tests/WebFiori/Tests/Http/RestControllerTest.php b/tests/WebFiori/Tests/Http/RestControllerTest.php index 9ffcc7a..2399975 100644 --- a/tests/WebFiori/Tests/Http/RestControllerTest.php +++ b/tests/WebFiori/Tests/Http/RestControllerTest.php @@ -55,17 +55,9 @@ public function testAnnotatedServiceWithManager() { . ' "http-code":405'.self::NL . '}', $this->postRequest($manager, 'annotated-service')); - $this->assertEquals('{'.self::NL - . ' "message":"Hi user!",'.self::NL - . ' "type":"success",'.self::NL - . ' "http-code":200'.self::NL - . '}', $this->getRequest($manager, 'annotated-service')); + $this->assertEquals('Hi user!', $this->getRequest($manager, 'annotated-service')); - $this->assertEquals('{'.self::NL - . ' "message":"Hi Ibrahim!",'.self::NL - . ' "type":"success",'.self::NL - . ' "http-code":200'.self::NL - . '}', $this->getRequest($manager, 'annotated-service', [ + $this->assertEquals('Hi Ibrahim!', $this->getRequest($manager, 'annotated-service', [ 'name' => 'Ibrahim' ])); } @@ -90,17 +82,9 @@ public function testAnnotatedGet() { $this->assertNotNull($retrievedService); $this->assertEquals('A service configured via annotations', $retrievedService->getDescription()); - $this->assertEquals('{'.self::NL - . ' "message":"Hi user!",'.self::NL - . ' "type":"success",'.self::NL - . ' "http-code":200'.self::NL - . '}', $this->getRequest($manager, 'annotated-service')); + $this->assertEquals('Hi user!', $this->getRequest($manager, 'annotated-service')); - $this->assertEquals('{'.self::NL - . ' "message":"Hi Ibrahim!",'.self::NL - . ' "type":"success",'.self::NL - . ' "http-code":200'.self::NL - . '}', $this->getRequest($manager, 'annotated-service', [ + $this->assertEquals('Hi Ibrahim!', $this->getRequest($manager, 'annotated-service', [ 'name' => 'Ibrahim' ])); } @@ -152,11 +136,7 @@ public function testAnnotatedDelete() { 'id' => 1 ], [], new TestUser(1, ['ADMIN'], ['USER_DELETE'], false))); //valid user - $this->assertEquals('{'.self::NL - . ' "message":"Delete user with ID: 1",'.self::NL - . ' "type":"success",'.self::NL - . ' "http-code":200'.self::NL - . '}', $this->deleteRequest($manager, 'annotated-service', [ + $this->assertEquals('Delete user with ID: 1', $this->deleteRequest($manager, 'annotated-service', [ 'id' => 1 ], [], new TestUser(1, ['ADMIN'], ['USER_DELETE'], true))); } diff --git a/tests/WebFiori/Tests/Http/TestServices/AnnotatedService.php b/tests/WebFiori/Tests/Http/TestServices/AnnotatedService.php index 098cec6..e2d68ce 100644 --- a/tests/WebFiori/Tests/Http/TestServices/AnnotatedService.php +++ b/tests/WebFiori/Tests/Http/TestServices/AnnotatedService.php @@ -4,6 +4,7 @@ use WebFiori\Http\Annotations\AllowAnonymous; use WebFiori\Http\Annotations\DeleteMapping; use WebFiori\Http\Annotations\GetMapping; +use WebFiori\Http\Annotations\MapEntity; use WebFiori\Http\Annotations\PreAuthorize; use WebFiori\Http\Annotations\RequestParam; use WebFiori\Http\Annotations\ResponseBody; @@ -17,9 +18,8 @@ class AnnotatedService extends WebService { #[ResponseBody] #[AllowAnonymous] #[RequestParam('name', ParamType::STRING, true)] - public function sayHi() { - $name = $this->getParamVal('name'); - + public function sayHi(?string $name) { + if ($name !== null) { return "Hi ".$name.'!'; } @@ -29,8 +29,7 @@ public function sayHi() { #[ResponseBody] #[RequestParam('id', ParamType::INT)] #[PreAuthorize("isAuthenticated() && hasRole('ADMIN') && hasAuthority('USER_DELETE')")] - public function delete() { - $id = $this->getParamVal('id'); + public function delete(int $id) { return "Delete user with ID: ".$id; } }