Skip to content

Commit ab03b55

Browse files
authored
fix(openapi): typing issue with openapiContext in #[ApiProperty] (#6910)
1 parent 9b07380 commit ab03b55

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/JsonSchema/Metadata/Property/Factory/SchemaPropertyMetadataFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function create(string $resourceClass, string $property, array $options =
106106
// never override the following keys if at least one is already set or if there's a custom openapi context
107107
if ([] === $types
108108
|| ($propertySchema['type'] ?? $propertySchema['$ref'] ?? $propertySchema['anyOf'] ?? $propertySchema['allOf'] ?? $propertySchema['oneOf'] ?? false)
109-
|| ($propertyMetadata->getOpenapiContext() ?? false)
109+
|| \array_key_exists('type', $propertyMetadata->getOpenapiContext() ?? [])
110110
) {
111111
return $propertyMetadata->withSchema($propertySchema);
112112
}

src/JsonSchema/Tests/Fixtures/DummyWithCustomOpenApiContext.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,10 @@ class DummyWithCustomOpenApiContext
3030
{
3131
#[ApiProperty(openapiContext: ['type' => 'object', 'properties' => ['alpha' => ['type' => 'integer']]])]
3232
public $acme;
33+
34+
#[ApiProperty(openapiContext: ['description' => 'My description'])]
35+
public bool $foo;
36+
37+
#[ApiProperty(openapiContext: ['iris' => 'https://schema.org/Date'])]
38+
public \DateTimeImmutable $bar;
3339
}

src/JsonSchema/Tests/Metadata/Property/Factory/SchemaPropertyMetadataFactoryTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,33 @@ public function testWithCustomOpenApiContext(): void
4949
$apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'acme');
5050
$this->assertEquals([], $apiProperty->getSchema());
5151
}
52+
53+
public function testWithCustomOpenApiContextWithoutTypeDefinition(): void
54+
{
55+
$resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
56+
$apiProperty = new ApiProperty(
57+
openapiContext: ['description' => 'My description'],
58+
builtinTypes: [new Type(builtinType: 'bool')],
59+
);
60+
$decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
61+
$decorated->expects($this->once())->method('create')->with(DummyWithCustomOpenApiContext::class, 'foo')->willReturn($apiProperty);
62+
$schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
63+
$apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'foo');
64+
$this->assertEquals([
65+
'type' => 'boolean',
66+
], $apiProperty->getSchema());
67+
68+
$apiProperty = new ApiProperty(
69+
openapiContext: ['iris' => 'https://schema.org/Date'],
70+
builtinTypes: [new Type(builtinType: 'object', class: \DateTimeImmutable::class)],
71+
);
72+
$decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
73+
$decorated->expects($this->once())->method('create')->with(DummyWithCustomOpenApiContext::class, 'bar')->willReturn($apiProperty);
74+
$schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
75+
$apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'bar');
76+
$this->assertEquals([
77+
'type' => 'string',
78+
'format' => 'date-time',
79+
], $apiProperty->getSchema());
80+
}
5281
}

0 commit comments

Comments
 (0)