Skip to content
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
3 changes: 2 additions & 1 deletion src/JsonSchema/SchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Symfony\Component\TypeInfo\Type\CollectionType;
use Symfony\Component\TypeInfo\Type\CompositeTypeInterface;
use Symfony\Component\TypeInfo\Type\GenericType;
use Symfony\Component\TypeInfo\Type\IntersectionType;
use Symfony\Component\TypeInfo\Type\ObjectType;
use Symfony\Component\TypeInfo\Type\UnionType;
use Symfony\Component\TypeInfo\TypeIdentifier;
Expand Down Expand Up @@ -361,7 +362,7 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str
}

if (($c = \count($refs)) > 1) {
$propertySchema['anyOf'] = $refs;
$propertySchema[$type instanceof IntersectionType ? 'allOf' : 'anyOf'] = $refs;
} elseif (1 === $c) {
$propertySchema['$ref'] = $refs[0]['$ref'];
}
Expand Down
16 changes: 16 additions & 0 deletions src/JsonSchema/Tests/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,22 @@ public function testBuildSchemaKeepsPropertyMetadataAlongsideAnIntersectionOfRef
->withDescription('The child.')
->withSchema(['description' => 'The child.', 'allOf' => [['type' => Schema::UNKNOWN_TYPE], ['type' => Schema::UNKNOWN_TYPE]]]));

$this->assertSame([
'description' => 'The child.',
'allOf' => [
['$ref' => '#/definitions/GenericChild'],
['$ref' => '#/definitions/Serializable'],
],
], $properties['child']->getArrayCopy());
}

public function testBuildSchemaUsesAnyOfForAUnionOfReferences(): void
{
$properties = $this->buildPropertiesWithReference('child', (new ApiProperty())
->withNativeType(Type::union(Type::object(GenericChild::class), Type::object(Serializable::class)))
->withDescription('The child.')
->withSchema(['description' => 'The child.', 'anyOf' => [['type' => Schema::UNKNOWN_TYPE], ['type' => Schema::UNKNOWN_TYPE]]]));

$this->assertSame([
'description' => 'The child.',
'anyOf' => [
Expand Down
4 changes: 2 additions & 2 deletions tests/Functional/JsonSchema/JsonSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ public function testResourceWithEnumPropertiesSchema(): void
public function testSchemaWithUnknownType(): void
{
$schema = $this->schemaFactory->buildSchema(Book::class, 'json', Schema::TYPE_OUTPUT, $this->operationMetadataFactory->create('_api_/issue-5452/books{._format}_get_collection'));
$this->assertContains(['$ref' => '#/definitions/ActivableInterface'], $schema['definitions']['Book']['properties']['library']['anyOf']);
$this->assertContains(['$ref' => '#/definitions/TimestampableInterface'], $schema['definitions']['Book']['properties']['library']['anyOf']);
$this->assertContains(['$ref' => '#/definitions/ActivableInterface'], $schema['definitions']['Book']['properties']['library']['allOf']);
$this->assertContains(['$ref' => '#/definitions/TimestampableInterface'], $schema['definitions']['Book']['properties']['library']['allOf']);
}

public function testReadOnlySchema(): void
Expand Down
Loading