Skip to content

Commit

Permalink
fix(openapi): typing issue with openapiContext in #[ApiProperty] (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SebLevDev authored Jan 16, 2025
1 parent 9b07380 commit ab03b55
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function create(string $resourceClass, string $property, array $options =
// never override the following keys if at least one is already set or if there's a custom openapi context
if ([] === $types
|| ($propertySchema['type'] ?? $propertySchema['$ref'] ?? $propertySchema['anyOf'] ?? $propertySchema['allOf'] ?? $propertySchema['oneOf'] ?? false)
|| ($propertyMetadata->getOpenapiContext() ?? false)
|| \array_key_exists('type', $propertyMetadata->getOpenapiContext() ?? [])
) {
return $propertyMetadata->withSchema($propertySchema);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,10 @@ class DummyWithCustomOpenApiContext
{
#[ApiProperty(openapiContext: ['type' => 'object', 'properties' => ['alpha' => ['type' => 'integer']]])]
public $acme;

#[ApiProperty(openapiContext: ['description' => 'My description'])]
public bool $foo;

#[ApiProperty(openapiContext: ['iris' => 'https://schema.org/Date'])]
public \DateTimeImmutable $bar;
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,33 @@ public function testWithCustomOpenApiContext(): void
$apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'acme');
$this->assertEquals([], $apiProperty->getSchema());
}

public function testWithCustomOpenApiContextWithoutTypeDefinition(): void
{
$resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
$apiProperty = new ApiProperty(
openapiContext: ['description' => 'My description'],
builtinTypes: [new Type(builtinType: 'bool')],
);
$decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
$decorated->expects($this->once())->method('create')->with(DummyWithCustomOpenApiContext::class, 'foo')->willReturn($apiProperty);
$schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
$apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'foo');
$this->assertEquals([
'type' => 'boolean',
], $apiProperty->getSchema());

$apiProperty = new ApiProperty(
openapiContext: ['iris' => 'https://schema.org/Date'],
builtinTypes: [new Type(builtinType: 'object', class: \DateTimeImmutable::class)],
);
$decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
$decorated->expects($this->once())->method('create')->with(DummyWithCustomOpenApiContext::class, 'bar')->willReturn($apiProperty);
$schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
$apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'bar');
$this->assertEquals([
'type' => 'string',
'format' => 'date-time',
], $apiProperty->getSchema());
}
}

0 comments on commit ab03b55

Please sign in to comment.