Skip to content
Open
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
97 changes: 97 additions & 0 deletions app/Http/Controllers/Apis/Marketplace/DriversApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php namespace App\Http\Controllers;
/**
* Copyright 2017 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\Models\Foundation\Marketplace\IDriverRepository;
use models\oauth2\IResourceServerContext;
use models\utils\IBaseRepository;
use ModelSerializers\SerializerRegistry;

/**
* Class DriversApiController
* @package App\Http\Controllers
*/
final class DriversApiController extends JsonController
{
use ParametrizedGetAll;

/**
* @var IDriverRepository
*/
private $repository;

/**
* @var IResourceServerContext
*/
private $resource_server_context;

/**
* DriversApiController constructor.
* @param IDriverRepository $repository
* @param IResourceServerContext $resource_server_context
*/
public function __construct(IDriverRepository $repository, IResourceServerContext $resource_server_context)
{
parent::__construct();
$this->repository = $repository;
$this->resource_server_context = $resource_server_context;
}

protected function getResourceServerContext(): IResourceServerContext
{
return $this->resource_server_context;
}

protected function getRepository(): IBaseRepository
{
return $this->repository;
}

/**
* @return mixed
*/
public function getAll()
{
return $this->_getAll(
function () {
return [
'name' => ['=@', '==', '@@'],
'project' => ['=@', '==', '@@'],
'vendor' => ['=@', '==', '@@'],
'release' => ['=@', '==', '@@'],
];
},
function () {
return [
'name' => 'sometimes|string',
'project' => 'sometimes|string',
'vendor' => 'sometimes|string',
'release' => 'sometimes|string',
];
},
function () {
return [
'id',
'name',
'project',
'vendor',
];
},
function ($filter) {
return $filter;
},
function () {
return SerializerRegistry::SerializerType_Public;
}
);
}
}
27 changes: 27 additions & 0 deletions app/ModelSerializers/Marketplace/DriverReleaseSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace App\ModelSerializers\Marketplace;
/**
* Copyright 2017 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use ModelSerializers\SilverStripeSerializer;

/**
* Class DriverReleaseSerializer
* @package App\ModelSerializers\Marketplace
*/
class DriverReleaseSerializer extends SilverStripeSerializer
{
protected static $array_mappings = [
'Name' => 'name:json_string',
'Url' => 'url:json_string',
'Active' => 'active:json_boolean',
];
}
68 changes: 68 additions & 0 deletions app/ModelSerializers/Marketplace/DriverSerializer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php namespace App\ModelSerializers\Marketplace;
/**
* Copyright 2017 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

use App\Models\Foundation\Marketplace\Driver;
use Libs\ModelSerializers\Many2OneExpandSerializer;
use ModelSerializers\SilverStripeSerializer;

/**
* Class DriverSerializer
* @package App\ModelSerializers\Marketplace
*/
class DriverSerializer extends SilverStripeSerializer
{
protected static $array_mappings = [
'Name' => 'name:json_string',
'Description' => 'description:json_string',
'Project' => 'project:json_string',
'Vendor' => 'vendor:json_string',
'Url' => 'url:json_string',
'Tested' => 'tested:json_boolean',
'Active' => 'active:json_boolean',
];

/**
* @param $expand
* @param array $fields
* @param array $relations
* @param array $params
* @return array
*/
public function serialize($expand = null, array $fields = [], array $relations = [], array $params = [])
{
$driver = $this->object;
if (!$driver instanceof Driver) return [];
$values = parent::serialize($expand, $fields, $relations, $params);

if (in_array('releases', $relations) && !isset($values['releases'])) {
$releases = [];
foreach ($driver->getReleases() as $r) {
$releases[] = $r->getId();
}
$values['releases'] = $releases;
}
return $values;
}

protected static $allowed_relations = [
'releases',
];

protected static $expand_mappings = [
'releases' => [
'type' => Many2OneExpandSerializer::class,
'getter' => 'getReleases',
],
];
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DriverSerializer declares releases in $allowed_relations, but unlike other serializers for collection relations (eg TrainingServiceSerializer), it doesn’t add a releases field containing related IDs when relations=releases is requested. As a result, clients asking for relation IDs may get no releases attribute unless they use expand=releases. Consider overriding serialize() to include the release IDs when releases is in $relations (mirroring the pattern used in TrainingServiceSerializer).

Suggested change
];
];
/**
* @param null|string $expand
* @param array $fields
* @param array $relations
* @param array $params
* @return array
*/
public function serialize($expand = null, array $fields = [], array $relations = [], array $params = [])
{
$values = parent::serialize($expand, $fields, $relations, $params);
$driver = $this->object;
if (is_null($driver)) {
return $values;
}
if (in_array('releases', $relations) && !isset($values['releases'])) {
$values['releases'] = [];
foreach ($driver->getReleases() as $release) {
$values['releases'][] = (int)$release->getId();
}
}
return $values;
}

Copilot uses AI. Check for mistakes.
}
4 changes: 4 additions & 0 deletions app/ModelSerializers/SerializerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
use App\ModelSerializers\Marketplace\TrainingCourseSerializer;
use App\ModelSerializers\Marketplace\TrainingCourseTypeSerializer;
use App\ModelSerializers\Marketplace\TrainingServiceSerializer;
use App\ModelSerializers\Marketplace\DriverSerializer;
use App\ModelSerializers\Marketplace\DriverReleaseSerializer;
use App\ModelSerializers\PushNotificationMessageSerializer;
use App\ModelSerializers\ResourceServer\ApiEndpointAuthzGroupSerializer;
use App\ModelSerializers\ResourceServer\ApiEndpointSerializer;
Expand Down Expand Up @@ -668,6 +670,8 @@ private function __construct()
$this->registry['RemoteCloudService'] = RemoteCloudServiceSerializer::class;
$this->registry['CloudServiceOffered'] = CloudServiceOfferedSerializer::class;
$this->registry['TrainingService'] = TrainingServiceSerializer::class;
$this->registry['Driver'] = DriverSerializer::class;
$this->registry['DriverRelease'] = DriverReleaseSerializer::class;
$this->registry['TrainingCourse'] = TrainingCourseSerializer::class;
$this->registry['TrainingCourseType'] = TrainingCourseTypeSerializer::class;
$this->registry['TrainingCourseLevel'] = TrainingCourseLevelSerializer::class;
Expand Down
Loading