Skip to content

Commit 79733f8

Browse files
author
Antoine Lelaisant
committed
fix: make EnumType, CollectionType and ObjectType implementing JsonSchemaInterface
1 parent 8de165d commit 79733f8

File tree

3 files changed

+50
-5
lines changed

3 files changed

+50
-5
lines changed

src/CollectionSchema.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
*
1212
* @extends JsonSchema<CollectionSchemaData>
1313
*/
14-
abstract class CollectionSchema extends JsonSchema
14+
abstract class CollectionSchema implements JsonSchemaInterface
1515
{
1616
/**
1717
* @param JsonSchema<I> $itemSchema
@@ -30,6 +30,11 @@ public function getTitle(): string
3030
return sprintf('Collection<%s>', $this->itemSchema->getTitle());
3131
}
3232

33+
public function getDescription(): string
34+
{
35+
return sprintf('Collection of %s', $this->itemSchema->getDescription());
36+
}
37+
3338
protected function getUniqueItems(): ?bool
3439
{
3540
return null;
@@ -71,4 +76,24 @@ public function getSchema(): array
7176

7277
return $schema;
7378
}
79+
80+
/**
81+
* {@inheritdoc}
82+
*/
83+
public function jsonSerialize(): array
84+
{
85+
$schema = $this->getSchema();
86+
87+
/**
88+
* @var array<string, mixed>&array{title: string, description: string, examples: array<T>}
89+
*/
90+
return array_merge(
91+
$schema,
92+
[
93+
'title' => $this->getTitle(),
94+
'description' => $this->getDescription(),
95+
'examples' => [...$this->getExamples()],
96+
],
97+
);
98+
}
7499
}

src/EnumSchema.php

+23-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88
* @template E
99
* @extends JsonSchema<E>
1010
*/
11-
abstract class EnumSchema extends JsonSchema
11+
abstract class EnumSchema implements JsonSchemaInterface
1212
{
13+
/**
14+
* @return iterable<int, E>
15+
*/
16+
abstract protected function getEnum(): iterable;
17+
1318
public function getExamples(): iterable
1419
{
1520
return $this->getEnum();
@@ -23,7 +28,22 @@ public function getSchema(): array
2328
}
2429

2530
/**
26-
* @return iterable<int, E>
31+
* {@inheritdoc}
2732
*/
28-
abstract protected function getEnum(): iterable;
33+
public function jsonSerialize(): array
34+
{
35+
$schema = $this->getSchema();
36+
37+
/**
38+
* @var array<string, mixed>&array{title: string, description: string, examples: array<T>}
39+
*/
40+
return array_merge(
41+
$schema,
42+
[
43+
'title' => $this->getTitle(),
44+
'description' => $this->getDescription(),
45+
'examples' => [...$this->getExamples()],
46+
],
47+
);
48+
}
2949
}

src/ObjectSchema.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* @template T of array<string, mixed>
99
* @extends JsonSchema<T>
1010
*/
11-
abstract class ObjectSchema extends JsonSchema
11+
abstract class ObjectSchema implements JsonSchemaInterface
1212
{
1313
/**
1414
* @var array<string, JsonSchema<mixed>>

0 commit comments

Comments
 (0)