From 33db981027a0c58fce7e9b4445d6410d06ded8dd Mon Sep 17 00:00:00 2001 From: Nicky Gerritsen Date: Tue, 3 Oct 2023 22:22:45 +0200 Subject: [PATCH 1/3] If an API returns an empty response, do not try to JSON decode it. This happens when the API returns a 204. --- webapp/src/Service/DOMJudgeService.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/webapp/src/Service/DOMJudgeService.php b/webapp/src/Service/DOMJudgeService.php index c9da8e411c..ca2ad891c3 100644 --- a/webapp/src/Service/DOMJudgeService.php +++ b/webapp/src/Service/DOMJudgeService.php @@ -654,7 +654,13 @@ public function internalApiRequest(string $url, string $method = Request::METHOD return null; } - return $this->jsonDecode($response->getContent()); + $content = $response->getContent(); + + if ($content === '') { + return null; + } + + return $this->jsonDecode($content); } public function getDomjudgeEtcDir(): string From ad71f93b8dda816533698e86a8402a487cc85e16 Mon Sep 17 00:00:00 2001 From: Nicky Gerritsen Date: Tue, 3 Oct 2023 22:23:16 +0200 Subject: [PATCH 2/3] Only import images for entities that actually exist. --- misc-tools/import-contest.in | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/misc-tools/import-contest.in b/misc-tools/import-contest.in index a1cc8f9bf1..1f6226bbad 100755 --- a/misc-tools/import-contest.in +++ b/misc-tools/import-contest.in @@ -68,7 +68,10 @@ def import_images(entity: str, property: str, filename_regexes: List[str]): if not os.path.isdir(entity): return images_per_entity = {} - for entity_id in listdir(entity): + with open(f'{entity}.json') as entityFile: + entities = json.load(entityFile) + entity_ids = [entity['id'] for entity in entities] + for entity_id in entity_ids: entity_dir = f'{entity}/{entity_id}' if not os.path.isdir(entity_dir): continue From 18c91a60a2a2bd81fd2c3d10b09cc67b239ca644 Mon Sep 17 00:00:00 2001 From: Nicky Gerritsen Date: Tue, 3 Oct 2023 22:23:48 +0200 Subject: [PATCH 3/3] Fix organization logo and team photo properties in API when requesting without a contest. --- .../Controller/API/OrganizationController.php | 2 +- webapp/src/Controller/API/TeamController.php | 2 +- .../src/Serializer/TeamAffiliationVisitor.php | 18 +++++++++++------- webapp/src/Serializer/TeamVisitor.php | 18 +++++++++++------- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/webapp/src/Controller/API/OrganizationController.php b/webapp/src/Controller/API/OrganizationController.php index 0584351f79..0d2ef6b8bf 100644 --- a/webapp/src/Controller/API/OrganizationController.php +++ b/webapp/src/Controller/API/OrganizationController.php @@ -100,7 +100,7 @@ public function singleAction(Request $request, string $id): Response * Get the logo for the given organization. */ #[Rest\Get('contests/{cid}/organizations/{id}/logo', name: 'organization_logo')] - #[Rest\Get('organizations/{id}/logo')] + #[Rest\Get('organizations/{id}/logo', name: 'no_contest_organization_logo')] #[OA\Response( response: 200, description: 'Returns the given organization logo in PNG, JPG or SVG format', diff --git a/webapp/src/Controller/API/TeamController.php b/webapp/src/Controller/API/TeamController.php index e32ab1ca7a..dbc8ec7fdd 100644 --- a/webapp/src/Controller/API/TeamController.php +++ b/webapp/src/Controller/API/TeamController.php @@ -113,7 +113,7 @@ public function singleAction(Request $request, string $id): Response * Get the photo for the given team. */ #[Rest\Get('contests/{cid}/teams/{id}/photo', name: 'team_photo')] - #[Rest\Get('teams/{id}/photo')] + #[Rest\Get('teams/{id}/photo', name: 'no_contest_team_photo')] #[OA\Response( response: 200, description: 'Returns the given team photo in PNG, JPG or SVG format', diff --git a/webapp/src/Serializer/TeamAffiliationVisitor.php b/webapp/src/Serializer/TeamAffiliationVisitor.php index 39b643d7eb..b55b078427 100644 --- a/webapp/src/Serializer/TeamAffiliationVisitor.php +++ b/webapp/src/Serializer/TeamAffiliationVisitor.php @@ -84,13 +84,17 @@ public function onPostSerialize(ObjectEvent $event): void $parts = explode('.', $affiliationLogo); $extension = $parts[count($parts) - 1]; - $route = $this->dj->apiRelativeUrl( - 'v4_organization_logo', - [ - 'cid' => $this->requestStack->getCurrentRequest()->attributes->get('cid'), - 'id' => $id, - ] - ); + if ($cid = $this->requestStack->getCurrentRequest()->attributes->get('cid')) { + $route = $this->dj->apiRelativeUrl( + 'v4_organization_logo', + [ + 'cid' => $cid, + 'id' => $id, + ] + ); + } else { + $route = $this->dj->apiRelativeUrl('v4_no_contest_organization_logo', ['id' => $id]); + } $property = new StaticPropertyMetadata( TeamAffiliation::class, 'logo', diff --git a/webapp/src/Serializer/TeamVisitor.php b/webapp/src/Serializer/TeamVisitor.php index f37319e751..bb713a3529 100644 --- a/webapp/src/Serializer/TeamVisitor.php +++ b/webapp/src/Serializer/TeamVisitor.php @@ -62,13 +62,17 @@ public function onPostSerialize(ObjectEvent $event): void $imageSize = Utils::getImageSize($teamPhoto); - $route = $this->dj->apiRelativeUrl( - 'v4_team_photo', - [ - 'cid' => $this->requestStack->getCurrentRequest()->attributes->get('cid'), - 'id' => $id, - ] - ); + if ($cid = $this->requestStack->getCurrentRequest()->attributes->get('cid')) { + $route = $this->dj->apiRelativeUrl( + 'v4_team_photo', + [ + 'cid' => $cid, + 'id' => $id, + ] + ); + } else { + $route = $this->dj->apiRelativeUrl('v4_no_contest_team_photo', ['id' => $id,]); + } $property = new StaticPropertyMetadata( Team::class, 'photo',