|
73 | 73 | use Prophecy\Argument; |
74 | 74 | use Prophecy\PhpUnit\ProphecyTrait; |
75 | 75 | use Psr\Container\ContainerInterface; |
| 76 | +use Symfony\Component\Routing\RouterInterface; |
76 | 77 | use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter; |
77 | 78 | use Symfony\Component\TypeInfo\Type; |
78 | 79 |
|
@@ -286,7 +287,7 @@ public function testInvoke(): void |
286 | 287 |
|
287 | 288 | $baseOperation = (new HttpOperation())->withTypes(['http://schema.example.com/Dummy'])->withInputFormats(self::OPERATION_FORMATS['input_formats'])->withOutputFormats(self::OPERATION_FORMATS['output_formats'])->withClass(Dummy::class)->withShortName('Parameter')->withDescription('This is a dummy'); |
288 | 289 | $parameterResource = (new ApiResource())->withOperations(new Operations([ |
289 | | - 'uriVariableSchema' => (new Get(uriTemplate: '/uri_variable_uuid', uriVariables: ['id' => new Link(schema: ['type' => 'string', 'format' => 'uuid'], description: 'hello', required: true, openApi: new Parameter('id', 'path'))]))->withOperation($baseOperation), |
| 290 | + 'uriVariableSchema' => (new Get(uriTemplate: '/uri_variable_uuid/{id}', uriVariables: ['id' => new Link(schema: ['type' => 'string', 'format' => 'uuid'], description: 'hello', required: true, openApi: new Parameter('id', 'path'))]))->withOperation($baseOperation), |
290 | 291 | 'parameters' => (new Put(uriTemplate: '/parameters', parameters: [ |
291 | 292 | 'foo' => new HeaderParameter(description: 'hi', schema: ['type' => 'string', 'format' => 'uuid']), |
292 | 293 | ]))->withOperation($baseOperation), |
@@ -1333,7 +1334,7 @@ public function testInvoke(): void |
1333 | 1334 | ) |
1334 | 1335 | ), $emptyResponsePath->getPost()); |
1335 | 1336 |
|
1336 | | - $parameter = $paths->getPath('/uri_variable_uuid')->getGet()->getParameters()[0]; |
| 1337 | + $parameter = $paths->getPath('/uri_variable_uuid/{id}')->getGet()->getParameters()[0]; |
1337 | 1338 | $this->assertEquals(['type' => 'string', 'format' => 'uuid'], $parameter->getSchema()); |
1338 | 1339 |
|
1339 | 1340 | $parameter = $paths->getPath('/parameters')->getPut()->getParameters()[0]; |
@@ -1949,6 +1950,67 @@ public function testDeleteOperationWithDeserializeDocumentsRequestBody(): void |
1949 | 1950 | $this->assertCount(0, $plainInputSchema->getDefinitions()); |
1950 | 1951 | } |
1951 | 1952 |
|
| 1953 | + public function testUriVariableAbsentFromPathIsSkipped(): void |
| 1954 | + { |
| 1955 | + $openApi = $this->createOpenApiForOperations([ |
| 1956 | + (new Get()) |
| 1957 | + ->withUriTemplate('/things/fixed/{id}') |
| 1958 | + ->withUriVariables([ |
| 1959 | + 'kind' => new Link(fromClass: Dummy::class, identifiers: ['kind']), |
| 1960 | + 'id' => new Link(fromClass: Dummy::class, identifiers: ['id']), |
| 1961 | + ]) |
| 1962 | + ->withShortName('Dummy') |
| 1963 | + ->withName('things_fixed_get'), |
| 1964 | + ]); |
| 1965 | + |
| 1966 | + $parameters = $openApi->getPaths()->getPath('/things/fixed/{id}')->getGet()->getParameters(); |
| 1967 | + |
| 1968 | + $this->assertSame(['id'], array_map(static fn (Parameter $parameter): string => $parameter->getName(), array_values(array_filter($parameters, static fn (Parameter $parameter): bool => 'path' === $parameter->getIn())))); |
| 1969 | + } |
| 1970 | + |
| 1971 | + /** |
| 1972 | + * @param HttpOperation[] $operations |
| 1973 | + */ |
| 1974 | + private function createOpenApiForOperations(array $operations, ?RouterInterface $router = null): OpenApi |
| 1975 | + { |
| 1976 | + $resourceNameCollectionFactory = $this->createStub(ResourceNameCollectionFactoryInterface::class); |
| 1977 | + $resourceCollectionMetadataFactory = $this->createStub(ResourceMetadataCollectionFactoryInterface::class); |
| 1978 | + $propertyNameCollectionFactory = $this->createStub(PropertyNameCollectionFactoryInterface::class); |
| 1979 | + $propertyMetadataFactory = $this->createStub(PropertyMetadataFactoryInterface::class); |
| 1980 | + |
| 1981 | + $resourceCollectionMetadata = new ResourceMetadataCollection(Dummy::class, [(new ApiResource(operations: $operations))->withClass(Dummy::class)]); |
| 1982 | + |
| 1983 | + $resourceCollectionMetadataFactory |
| 1984 | + ->method('create') |
| 1985 | + ->willReturnCallback(static fn (string $resourceClass): ResourceMetadataCollection => Dummy::class === $resourceClass ? $resourceCollectionMetadata : new ResourceMetadataCollection($resourceClass, [])); |
| 1986 | + $resourceNameCollectionFactory->method('create')->willReturn(new ResourceNameCollection([Dummy::class])); |
| 1987 | + $propertyNameCollectionFactory->method('create')->willReturn(new PropertyNameCollection([])); |
| 1988 | + |
| 1989 | + $schemaFactory = new SchemaFactory( |
| 1990 | + resourceMetadataFactory: $resourceCollectionMetadataFactory, |
| 1991 | + propertyNameCollectionFactory: $propertyNameCollectionFactory, |
| 1992 | + propertyMetadataFactory: $propertyMetadataFactory, |
| 1993 | + nameConverter: new CamelCaseToSnakeCaseNameConverter(), |
| 1994 | + definitionNameFactory: new DefinitionNameFactory(), |
| 1995 | + ); |
| 1996 | + |
| 1997 | + $factory = new OpenApiFactory( |
| 1998 | + $resourceNameCollectionFactory, |
| 1999 | + $resourceCollectionMetadataFactory, |
| 2000 | + $propertyNameCollectionFactory, |
| 2001 | + $propertyMetadataFactory, |
| 2002 | + $schemaFactory, |
| 2003 | + null, |
| 2004 | + [], |
| 2005 | + new Options('Test API', 'This is a test API.', '1.2.3'), |
| 2006 | + new PaginationOptions(), |
| 2007 | + $router, |
| 2008 | + ['json' => ['application/problem+json']] |
| 2009 | + ); |
| 2010 | + |
| 2011 | + return $factory->__invoke(); |
| 2012 | + } |
| 2013 | + |
1952 | 2014 | /** |
1953 | 2015 | * @param Parameter[] $parameters |
1954 | 2016 | */ |
|
0 commit comments