Skip to content

Commit 8697d66

Browse files
authored
fix(openapi): boolean "true" value in HttpOperation::openapi (#7003)
fixes #6993
1 parent b1e0c88 commit 8697d66

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
174174
}
175175

176176
foreach ($resource->getOperations() as $operationName => $operation) {
177-
$resourceShortName = $operation->getShortName();
177+
$resourceShortName = $operation->getShortName() ?? $operation;
178178
// No path to return
179179
if (null === $operation->getUriTemplate() && null === $operation->getRouteName()) {
180180
continue;
@@ -187,7 +187,8 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
187187
continue;
188188
}
189189

190-
$operationTag = ($openapiAttribute?->getExtensionProperties()[self::API_PLATFORM_TAG] ?? []);
190+
// See https://github.com/api-platform/core/issues/6993 we would like to allow only `false` but as we typed `bool` we have this check
191+
$operationTag = !\is_object($openapiAttribute) ? [] : ($openapiAttribute->getExtensionProperties()[self::API_PLATFORM_TAG] ?? []);
191192
if (!\is_array($operationTag)) {
192193
$operationTag = [$operationTag];
193194
}
@@ -206,7 +207,7 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
206207
if ($this->routeCollection && $routeName && $route = $this->routeCollection->get($routeName)) {
207208
$path = $route->getPath();
208209
} else {
209-
$path = rtrim($operation->getRoutePrefix() ?? '', '/').'/'.ltrim($operation->getUriTemplate(), '/');
210+
$path = rtrim($operation->getRoutePrefix() ?? '', '/').'/'.ltrim($operation->getUriTemplate() ?? '', '/');
210211
}
211212

212213
$path = $this->getPath($path);

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,4 +1296,54 @@ public function testInvoke(): void
12961296
$this->assertNotNull($diamondPutOperation);
12971297
$this->assertArrayNotHasKey('403', $diamondPutResponses);
12981298
}
1299+
1300+
public function testGetExtensionPropertiesWithFalseValue(): void
1301+
{
1302+
$resourceNameCollectionFactory = $this->createMock(ResourceNameCollectionFactoryInterface::class);
1303+
$resourceCollectionMetadataFactory = $this->createMock(ResourceMetadataCollectionFactoryInterface::class);
1304+
$propertyNameCollectionFactory = $this->createMock(PropertyNameCollectionFactoryInterface::class);
1305+
$propertyMetadataFactory = $this->createMock(PropertyMetadataFactoryInterface::class);
1306+
$definitionNameFactory = new DefinitionNameFactory([]);
1307+
1308+
$resourceCollectionMetadata = new ResourceMetadataCollection(Dummy::class, [(new ApiResource(operations: [
1309+
(new Get())->withOpenapi(true)->withShortName('Dummy')->withName('api_dummies_get_collection')->withRouteName('api_dummies_get_collection'),
1310+
]))->withClass(Dummy::class)]);
1311+
1312+
$resourceCollectionMetadataFactory
1313+
->method('create')
1314+
->willReturnCallback(fn (string $resourceClass): ResourceMetadataCollection => match ($resourceClass) {
1315+
default => new ResourceMetadataCollection($resourceClass, []),
1316+
Dummy::class => $resourceCollectionMetadata,
1317+
});
1318+
1319+
$resourceNameCollectionFactory->expects($this->once())
1320+
->method('create')
1321+
->willReturn(new ResourceNameCollection([Dummy::class]));
1322+
1323+
$propertyNameCollectionFactory->method('create')->willReturn(new PropertyNameCollection([]));
1324+
1325+
$schemaFactory = new SchemaFactory(
1326+
resourceMetadataFactory: $resourceCollectionMetadataFactory,
1327+
propertyNameCollectionFactory: $propertyNameCollectionFactory,
1328+
propertyMetadataFactory: $propertyMetadataFactory,
1329+
nameConverter: new CamelCaseToSnakeCaseNameConverter(),
1330+
definitionNameFactory: $definitionNameFactory,
1331+
);
1332+
1333+
$factory = new OpenApiFactory(
1334+
$resourceNameCollectionFactory,
1335+
$resourceCollectionMetadataFactory,
1336+
$propertyNameCollectionFactory,
1337+
$propertyMetadataFactory,
1338+
$schemaFactory,
1339+
null,
1340+
[],
1341+
new Options('Test API', 'This is a test API.', '1.2.3'),
1342+
new PaginationOptions(),
1343+
null,
1344+
['json' => ['application/problem+json']]
1345+
);
1346+
1347+
$openApi = $factory->__invoke();
1348+
}
12991349
}

0 commit comments

Comments
 (0)