Skip to content

Commit 9164496

Browse files
authored
chore: Merge pull request #94 from WebFiori/dev
refactor: Changed How Response is Handled
2 parents 44868b3 + baea2d5 commit 9164496

4 files changed

Lines changed: 37 additions & 37 deletions

File tree

WebFiori/Http/WebService.php

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1331,12 +1331,24 @@ protected function handleMethodResponse(mixed $result, string $methodName): void
13311331
}
13321332

13331333
$responseBody = $responseBodyAttrs[0]->newInstance();
1334+
$contentType = $responseBody->contentType;
13341335

13351336
// Handle custom content types
1336-
if ($responseBody->contentType !== 'application/json') {
1337+
if ($contentType !== 'application/json') {
13371338
// For non-JSON content types, send raw result
1338-
$content = is_string($result) ? $result : (is_array($result) || is_object($result) ? json_encode($result) : (string)$result);
1339-
$this->send($responseBody->contentType, $content, $responseBody->status);
1339+
if (is_array($result)) {
1340+
$content = new Json();
1341+
$content->addArray('data', $result);
1342+
$contentType = 'application/json';
1343+
} else if (is_object($result)) {
1344+
$content = new Json();
1345+
$content->addObject('data', $result);
1346+
$contentType = 'application/json';
1347+
} else {
1348+
$content = (string)$result;
1349+
}
1350+
1351+
$this->send($contentType, $content, $responseBody->status);
13401352

13411353
return;
13421354
}
@@ -1345,12 +1357,20 @@ protected function handleMethodResponse(mixed $result, string $methodName): void
13451357
if ($result === null) {
13461358
// Null return = empty response with configured status
13471359
$this->sendResponse('', $responseBody->status, $responseBody->type);
1348-
} elseif (is_array($result) || is_object($result)) {
1360+
} else if (is_array($result) || is_object($result)) {
13491361
// Array/object = JSON response
1350-
$this->sendResponse('Success', $responseBody->status, $responseBody->type, $result);
1362+
if ($result instanceof Json) {
1363+
$json = $result;
1364+
} else if ($result instanceof JsonI) {
1365+
$json = $result->toJSON();
1366+
} else {
1367+
$json = new Json();
1368+
$json->add('data', $result);
1369+
}
1370+
$this->send($responseBody->contentType, $json, $responseBody->status);
13511371
} else {
13521372
// String/scalar = plain response
1353-
$this->sendResponse($result, $responseBody->status, $responseBody->type);
1373+
$this->send($responseBody->contentType, $result, $responseBody->status);
13541374
}
13551375
}
13561376
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
},
3333
"scripts": {
3434
"test": "phpunit --configuration tests/phpunit.xml",
35-
"test10": "phpunit --configuration tests/phpunit10.xml"
35+
"test10": "phpunit --configuration tests/phpunit10.xml",
36+
"fix-cs": "php-cs-fixer fix --config=php_cs.php.dist"
3637
},
3738
"require-dev": {
3839
"phpunit/phpunit": "^10.0",

tests/WebFiori/Tests/Http/RestControllerTest.php

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,9 @@ public function testAnnotatedServiceWithManager() {
5555
. ' "http-code":405'.self::NL
5656
. '}', $this->postRequest($manager, 'annotated-service'));
5757

58-
$this->assertEquals('{'.self::NL
59-
. ' "message":"Hi user!",'.self::NL
60-
. ' "type":"success",'.self::NL
61-
. ' "http-code":200'.self::NL
62-
. '}', $this->getRequest($manager, 'annotated-service'));
58+
$this->assertEquals('Hi user!', $this->getRequest($manager, 'annotated-service'));
6359

64-
$this->assertEquals('{'.self::NL
65-
. ' "message":"Hi Ibrahim!",'.self::NL
66-
. ' "type":"success",'.self::NL
67-
. ' "http-code":200'.self::NL
68-
. '}', $this->getRequest($manager, 'annotated-service', [
60+
$this->assertEquals('Hi Ibrahim!', $this->getRequest($manager, 'annotated-service', [
6961
'name' => 'Ibrahim'
7062
]));
7163
}
@@ -90,17 +82,9 @@ public function testAnnotatedGet() {
9082
$this->assertNotNull($retrievedService);
9183
$this->assertEquals('A service configured via annotations', $retrievedService->getDescription());
9284

93-
$this->assertEquals('{'.self::NL
94-
. ' "message":"Hi user!",'.self::NL
95-
. ' "type":"success",'.self::NL
96-
. ' "http-code":200'.self::NL
97-
. '}', $this->getRequest($manager, 'annotated-service'));
85+
$this->assertEquals('Hi user!', $this->getRequest($manager, 'annotated-service'));
9886

99-
$this->assertEquals('{'.self::NL
100-
. ' "message":"Hi Ibrahim!",'.self::NL
101-
. ' "type":"success",'.self::NL
102-
. ' "http-code":200'.self::NL
103-
. '}', $this->getRequest($manager, 'annotated-service', [
87+
$this->assertEquals('Hi Ibrahim!', $this->getRequest($manager, 'annotated-service', [
10488
'name' => 'Ibrahim'
10589
]));
10690
}
@@ -152,11 +136,7 @@ public function testAnnotatedDelete() {
152136
'id' => 1
153137
], [], new TestUser(1, ['ADMIN'], ['USER_DELETE'], false)));
154138
//valid user
155-
$this->assertEquals('{'.self::NL
156-
. ' "message":"Delete user with ID: 1",'.self::NL
157-
. ' "type":"success",'.self::NL
158-
. ' "http-code":200'.self::NL
159-
. '}', $this->deleteRequest($manager, 'annotated-service', [
139+
$this->assertEquals('Delete user with ID: 1', $this->deleteRequest($manager, 'annotated-service', [
160140
'id' => 1
161141
], [], new TestUser(1, ['ADMIN'], ['USER_DELETE'], true)));
162142
}

tests/WebFiori/Tests/Http/TestServices/AnnotatedService.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use WebFiori\Http\Annotations\AllowAnonymous;
55
use WebFiori\Http\Annotations\DeleteMapping;
66
use WebFiori\Http\Annotations\GetMapping;
7+
use WebFiori\Http\Annotations\MapEntity;
78
use WebFiori\Http\Annotations\PreAuthorize;
89
use WebFiori\Http\Annotations\RequestParam;
910
use WebFiori\Http\Annotations\ResponseBody;
@@ -17,9 +18,8 @@ class AnnotatedService extends WebService {
1718
#[ResponseBody]
1819
#[AllowAnonymous]
1920
#[RequestParam('name', ParamType::STRING, true)]
20-
public function sayHi() {
21-
$name = $this->getParamVal('name');
22-
21+
public function sayHi(?string $name) {
22+
2323
if ($name !== null) {
2424
return "Hi ".$name.'!';
2525
}
@@ -29,8 +29,7 @@ public function sayHi() {
2929
#[ResponseBody]
3030
#[RequestParam('id', ParamType::INT)]
3131
#[PreAuthorize("isAuthenticated() && hasRole('ADMIN') && hasAuthority('USER_DELETE')")]
32-
public function delete() {
33-
$id = $this->getParamVal('id');
32+
public function delete(int $id) {
3433
return "Delete user with ID: ".$id;
3534
}
3635
}

0 commit comments

Comments
 (0)