Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions WebFiori/Http/WebService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
}
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
30 changes: 5 additions & 25 deletions tests/WebFiori/Tests/Http/RestControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]));
}
Expand All @@ -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'
]));
}
Expand Down Expand Up @@ -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)));
}
Expand Down
9 changes: 4 additions & 5 deletions tests/WebFiori/Tests/Http/TestServices/AnnotatedService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.'!';
}
Expand All @@ -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;
}
}