Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OpenAPI schema validation issues #18161

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 15 additions & 2 deletions src/Glpi/Api/HL/Controller/AssetController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,7 @@ public function deleteCartridgeItems(Request $request): Response
}

#[Route(path: '/Cartridge/{cartridgeitems_id}/{id}', methods: ['GET'], requirements: [
'cartridgeitems_id' => '\d+',
'id' => '\d+'
], tags: ['Assets'], middlewares: [ResultFormatterMiddleware::class])]
#[RouteVersion(introduced: '2.0')]
Expand All @@ -1694,7 +1695,9 @@ public function getCartridge(Request $request): Response
return Search::getOneBySchema($this->getKnownSchema('Cartridge', $this->getAPIVersion($request)), $request->getAttributes(), $request->getParameters());
}

#[Route(path: '/Cartridge/{cartridgeitems_id}', methods: ['POST'], tags: ['Assets'])]
#[Route(path: '/Cartridge/{cartridgeitems_id}', methods: ['POST'], tags: ['Assets'], requirements: [
'cartridgeitems_id' => '\d+'
])]
#[RouteVersion(introduced: '2.0')]
#[Doc\Route(
description: 'Create a cartridge',
Expand All @@ -1712,6 +1715,7 @@ public function createCartridges(Request $request): Response
}

#[Route(path: '/Cartridge/{cartridgeitems_id}/{id}', methods: ['PATCH'], requirements: [
'cartridgeitems_id' => '\d+',
'id' => '\d+'
], tags: ['Assets'])]
#[RouteVersion(introduced: '2.0')]
Expand All @@ -1731,6 +1735,7 @@ public function updateCartridges(Request $request): Response
}

#[Route(path: '/Cartridge/{cartridgeitems_id}/{id}', methods: ['DELETE'], requirements: [
'cartridgeitems_id' => '\d+',
'id' => '\d+'
], tags: ['Assets'])]
#[RouteVersion(introduced: '2.0')]
Expand Down Expand Up @@ -1820,6 +1825,7 @@ public function deleteConsumableItems(Request $request): Response
}

#[Route(path: '/Consumable/{consumableitems_id}/{id}', methods: ['GET'], requirements: [
'consumableitems_id' => '\d+',
'id' => '\d+'
], tags: ['Assets'], middlewares: [ResultFormatterMiddleware::class])]
#[RouteVersion(introduced: '2.0')]
Expand All @@ -1834,7 +1840,9 @@ public function getConsumable(Request $request): Response
return Search::getOneBySchema($this->getKnownSchema('Consumable', $this->getAPIVersion($request)), $request->getAttributes(), $request->getParameters());
}

#[Route(path: '/Consumable/{consumableitems_id}', methods: ['POST'], tags: ['Assets'])]
#[Route(path: '/Consumable/{consumableitems_id}', methods: ['POST'], requirements: [
'consumableitems_id' => '\d+'
], tags: ['Assets'])]
#[RouteVersion(introduced: '2.0')]
#[Doc\Route(
description: 'Create a consumable',
Expand All @@ -1852,6 +1860,7 @@ public function createConsumables(Request $request): Response
}

#[Route(path: '/Consumable/{consumableitems_id}/{id}', methods: ['PATCH'], requirements: [
'consumableitems_id' => '\d+',
'id' => '\d+'
], tags: ['Assets'])]
#[RouteVersion(introduced: '2.0')]
Expand All @@ -1871,6 +1880,7 @@ public function updateConsumable(Request $request): Response
}

#[Route(path: '/Consumable/{consumableitems_id}/{id}', methods: ['DELETE'], requirements: [
'consumableitems_id' => '\d+',
'id' => '\d+'
], tags: ['Assets'])]
#[RouteVersion(introduced: '2.0')]
Expand Down Expand Up @@ -2546,6 +2556,7 @@ public function searchSoftwareVersions(Request $request): Response
}

#[Route(path: '/Software/{software_id}/Version/{id}', methods: ['GET'], requirements: [
'software_id' => '\d+',
'id' => '\d+'
], tags: ['Assets'], middlewares: [ResultFormatterMiddleware::class])]
#[RouteVersion(introduced: '2.0')]
Expand Down Expand Up @@ -2590,6 +2601,7 @@ public function createSoftwareVersion(Request $request): Response
}

#[Route(path: '/Software/{software_id}/Version/{id}', methods: ['PATCH'], requirements: [
'software_id' => '\d+',
'id' => '\d+'
], tags: ['Assets'])]
#[RouteVersion(introduced: '2.0')]
Expand All @@ -2609,6 +2621,7 @@ public function updateSoftwareVersion(Request $request): Response
}

#[Route(path: '/Software/{software_id}/Version/{id}', methods: ['DELETE'], requirements: [
'software_id' => '\d+',
'id' => '\d+'
], tags: ['Assets'])]
#[RouteVersion(introduced: '2.0')]
Expand Down
49 changes: 19 additions & 30 deletions src/Glpi/Api/HL/Controller/CoreController.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ public static function getRawKnownSchemas(): array
'entity' => ['type' => Doc\Schema::TYPE_INTEGER],
'options' => ['type' => Doc\Schema::TYPE_OBJECT],
]
],
'APIInformation' => [
'x-version-introduced' => '2.0',
'type' => Doc\Schema::TYPE_OBJECT,
'properties' => [
'message' => ['type' => Doc\Schema::TYPE_STRING],
'api_versions' => [
'type' => Doc\Schema::TYPE_ARRAY,
'items' => [
'type' => Doc\Schema::TYPE_OBJECT,
'properties' => [
'api_version' => ['type' => Doc\Schema::TYPE_STRING],
'version' => ['type' => Doc\Schema::TYPE_STRING],
'endpoint' => ['type' => Doc\Schema::TYPE_STRING],
]
]
]
]
]
];
}
Expand All @@ -111,34 +129,7 @@ public static function getRawKnownSchemas(): array
responses: [
'200' => [
'description' => 'API information',
'schema' => [
'type' => Doc\Schema::TYPE_OBJECT,
'properties' => [
'message' => ['type' => Doc\Schema::TYPE_STRING],
'api_versions' => [
'type' => Doc\Schema::TYPE_ARRAY,
'items' => [
'type' => Doc\Schema::TYPE_OBJECT,
'properties' => [
'api_version' => ['type' => Doc\Schema::TYPE_STRING],
'version' => ['type' => Doc\Schema::TYPE_STRING],
'endpoint' => ['type' => Doc\Schema::TYPE_STRING],
]
]
],
'links' => [
'type' => Doc\Schema::TYPE_ARRAY,
'items' => [
'type' => Doc\Schema::TYPE_OBJECT,
'properties' => [
'href' => ['type' => Doc\Schema::TYPE_STRING],
'methods' => ['type' => Doc\Schema::TYPE_ARRAY, 'items' => ['type' => Doc\Schema::TYPE_STRING]],
'requirements' => ['type' => Doc\Schema::TYPE_OBJECT, 'properties' => []],
]
]
]
]
]
'schema' => 'APIInformation'
]
]
)]
Expand All @@ -149,8 +140,6 @@ public function index(Request $request): Response
'api_versions' => Router::getAPIVersions()
];

$data['links'] = Router::getInstance()->getAllRoutePaths();

return new JSONResponse($data);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Glpi/Api/HL/Controller/ITILController.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ public static function getRawKnownSchemas(): array

$schemas[$itil_type]['properties']['team'] = [
'type' => Doc\Schema::TYPE_ARRAY,
'x-full-schema' => 'TeamMember',
'items' => [
'x-mapped-from' => 'id',
'x-mapper' => function ($v) use ($itil_type) {
Expand All @@ -221,7 +220,8 @@ public static function getRawKnownSchemas(): array
}
return [];
},
'ref' => 'TeamMember'
'type' => Doc\Schema::TYPE_OBJECT,
'x-full-schema' => 'TeamMember',
]
];
}
Expand Down
10 changes: 0 additions & 10 deletions src/Glpi/Api/HL/Controller/ManagementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,11 @@ public static function getManagementTypes(bool $schema_names_only = true): array

if ($management_types === null) {
$management_types = [
Appliance::class => [
'schema_name' => 'Appliance',
'label' => Appliance::getTypeName(1),
'version_introduced' => '2.0'
],
Budget::class => [
'schema_name' => 'Budget',
'label' => Budget::getTypeName(1),
'version_introduced' => '2.0'
],
Certificate::class => [
'schema_name' => 'Certificate',
'label' => Certificate::getTypeName(1),
'version_introduced' => '2.0'
],
Cluster::class => [
'schema_name' => 'Cluster',
'label' => Cluster::getTypeName(1),
Expand Down
21 changes: 5 additions & 16 deletions src/Glpi/Api/HL/OpenAPIGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Glpi\Api\HL\Doc\Response;
use Glpi\Api\HL\Doc\Schema;
use Glpi\Api\HL\Doc\SchemaReference;
use Glpi\Api\HL\Middleware\ResultFormatterMiddleware;
use Glpi\OAuth\Server;

/**
Expand Down Expand Up @@ -556,21 +557,11 @@ private function getPathParameterSchema(Doc\Parameter $route_param): array
*/
private function getPathSecuritySchema(RoutePath $route_path, string $route_method): array
{
$schemas = [
return [
[
'oauth' => []
]
];
// Handle special Session case
if ($route_path->getRouteSecurityLevel() !== Route::SECURITY_NONE) {
$schemas = array_merge($schemas, [
[
'sessionTokenAuth' => []
]
]);
}

return $schemas;
}

private function getPathResponseSchemas(RoutePath $route_path, string $method): array
Expand All @@ -596,7 +587,7 @@ private function getPathResponseSchemas(RoutePath $route_path, string $method):
]
],
];
if ($response_media_type === 'application/json') {
if ($response_media_type === 'application/json' && $route_path->hasMiddleware(ResultFormatterMiddleware::class)) {
// add csv and xml
$response_schema['content']['text/csv'] = [
'schema' => $resolved_schema
Expand Down Expand Up @@ -696,8 +687,6 @@ private function getPathSchemas(RoutePath $route_path): array
if (isset($info['content'])) {
$path_schema['responses'][$code]['content'] = $info['content'];
}
} else {
$path_schema['responses'][$code]['produces'] = array_keys($response_schema[(int) $code]['content']);
}
}

Expand Down Expand Up @@ -733,7 +722,7 @@ private function getPathSchemas(RoutePath $route_path): array
$param = [
'name' => $name,
'in' => 'path',
'required' => 'true',
'required' => true,
'schema' => [
'type' => 'integer',
'pattern' => $requirement
Expand All @@ -743,7 +732,7 @@ private function getPathSchemas(RoutePath $route_path): array
$param = [
'name' => $name,
'in' => 'path',
'required' => 'true',
'required' => true,
'schema' => [
'type' => 'string',
'pattern' => $requirement
Expand Down
9 changes: 9 additions & 0 deletions src/Glpi/Api/HL/RoutePath.php
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,15 @@ public function getMiddlewares(): array
return $this->getRoute()->middlewares;
}

/**
* Check if a middleware is present in the route
* @param class-string<AbstractMiddleware> $middleware
*/
public function hasMiddleware(string $middleware): bool
{
return in_array($middleware, $this->getMiddlewares(), true);
}

/**
* Combine data from the class Route attribute (if present) with the method's own attribute
*
Expand Down
Loading