diff --git a/lib/Controller/EndpointsController.php b/lib/Controller/EndpointsController.php index c54776ad3..6d40dd468 100644 --- a/lib/Controller/EndpointsController.php +++ b/lib/Controller/EndpointsController.php @@ -240,6 +240,14 @@ public function handlePath(string $_path): Response ); } + // If no matching endpoints found, return 404 + if (count($matchingEndpoints) > 1) { + return new JSONResponse( + data: ['error' => 'Multiple endpoints found for path and method: ' . $_path . ' ' . $this->request->getMethod()], + statusCode: 409 + ); + } + // Get the first matching endpoint since we have already filtered by method $endpoint = reset($matchingEndpoints); diff --git a/lib/Service/EndpointService.php b/lib/Service/EndpointService.php index 7523ecd94..e492ef49d 100644 --- a/lib/Service/EndpointService.php +++ b/lib/Service/EndpointService.php @@ -541,24 +541,49 @@ private function handleSchemaRequest(Endpoint $endpoint, IRequest $request, stri // Route to appropriate ObjectService method based on HTTP method try { - return match ($method) { - 'GET' => new JSONResponse( - $this->getObjects(mapper: $mapper, parameters: $parameters, pathParams: $pathParams, status: $status), statusCode: $status, headers: $headers - ), - 'POST' => new JSONResponse( - $this->replaceInternalReferences(mapper: $mapper, serializedObject: $mapper->createFromArray(object: $parameters)) - ), - 'PUT' => new JSONResponse( - $this->replaceInternalReferences(mapper: $mapper, serializedObject: $mapper->updateFromArray($parameters['id'], $request->getParams(), true, false)) - ), - 'PATCH' => new JSONResponse( - $this->replaceInternalReferences(mapper: $mapper, serializedObject: $mapper->updateFromArray($parameters['id'], $request->getParams(), true, true)) - ), - 'DELETE' => new JSONResponse( - $mapper->delete($request->getParams()) - ), - default => throw new Exception('Unsupported HTTP method') - }; + switch ($method) { + case 'GET': + return new JSONResponse( + $this->getObjects(mapper: $mapper, parameters: $parameters, pathParams: $pathParams, status: $status), + statusCode: $status, + headers: $headers + ); + case 'POST': + return new JSONResponse( + $this->replaceInternalReferences( + mapper: $mapper, + serializedObject: $mapper->createFromArray(object: $parameters) + ) + ); + case 'PUT': + return new JSONResponse( + $this->replaceInternalReferences( + mapper: $mapper, + serializedObject: $mapper->updateFromArray($parameters['id'], $request->getParams(), true, false) + ) + ); + case 'PATCH': + return new JSONResponse( + $this->replaceInternalReferences( + mapper: $mapper, + serializedObject: $mapper->updateFromArray($parameters['id'], $request->getParams(), true, true) + ) + ); + case 'DELETE': + if (isset($parameters['id']) === false) { + return new JSONResponse(data: ['error' => 'No id given to delete'], statusCode: 400); + } + + if ($mapper->delete(['id' => $parameters['id']]) !== true) { + return new JSONResponse(data: ['error' => sprintf('Something went wrong deleting object: %s', $parameters['id'])], statusCode: 500); + } + + return new JSONResponse(statusCode: 200); + + default: + throw new Exception('Unsupported HTTP method'); + } + } catch (Exception $exception) { if (in_array(get_class($exception), ['OCA\OpenRegister\Exception\ValidationException', 'OCA\OpenRegister\Exception\CustomValidationException']) === true) { return $mapper->handleValidationException(exception: $exception); diff --git a/lib/Service/SynchronizationService.php b/lib/Service/SynchronizationService.php index 5926f4263..17aa7e6f0 100644 --- a/lib/Service/SynchronizationService.php +++ b/lib/Service/SynchronizationService.php @@ -728,13 +728,13 @@ private function updateTargetOpenRegister(SynchronizationContract $synchronizati // Save the object to the target switch ($action) { case 'save': - $target = $objectService->saveObject($register, $schema, $targetObject); + $target = $objectService->saveObject(register: $register, schema: $schema, object: $targetObject); // Get the id form the target object $synchronizationContract->setTargetId($target->getUuid()); // Handle sub-objects synchronization if sourceConfig is defined if (isset($sourceConfig['subObjects']) === true) { - $targetObject = $objectService->extendEntity($target->jsonSerialize(), ['all']); + $targetObject = $objectService->renderEntity($target->jsonSerialize(), ['all']); $this->updateContractsForSubObjects(subObjectsConfig: $sourceConfig['subObjects'], synchronizationId: $synchronization->getId(), targetObject: $targetObject); }