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): typing issue with openapiContext in #[ApiProperty] #6910

Merged
merged 3 commits into from
Jan 16, 2025
Merged
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
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());
}
}
Loading