@@ -36,17 +36,18 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
36
36
use ResourceMetadataTrait;
37
37
use SchemaUriPrefixTrait;
38
38
39
- private const ITEM_BASE_SCHEMA_NAME = 'HydraItemBaseSchema ' ;
40
- private const ITEM_BASE_SCHEMA_OUTPUT_NAME = 'HydraOutputBaseSchema ' ;
41
- private const COLLECTION_BASE_SCHEMA_NAME = 'HydraCollectionBaseSchema ' ;
39
+ private const INPUT_ITEM_SCHEMA_NAME = 'HydraInputItemSchema ' ;
40
+ private const OUTPUT_ITEM_SCHEMA_NAME = 'HydraOutputItemSchema ' ;
41
+ private const COLLECTION_SCHEMA_NAME = 'HydraCollectionSchema ' ;
42
42
private const BASE_PROP = [
43
43
'type ' => 'string ' ,
44
44
];
45
45
private const BASE_PROPS = [
46
46
'@id ' => self ::BASE_PROP ,
47
47
'@type ' => self ::BASE_PROP ,
48
48
];
49
- private const ITEM_BASE_SCHEMA = [
49
+ // This is the base for both input and output, and serves as the complete definition for an INPUT schema.
50
+ private const BASE_ITEM_SCHEMA = [
50
51
'type ' => 'object ' ,
51
52
'properties ' => [
52
53
'@context ' => [
@@ -70,10 +71,10 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
70
71
],
71
72
] + self ::BASE_PROPS ,
72
73
];
73
-
74
- private const ITEM_BASE_SCHEMA_OUTPUT = [
74
+ // The OUTPUT schema is the base schema with the addition of required fields.
75
+ private const OUTPUT_ITEM_SCHEMA = [
75
76
'required ' => ['@id ' , '@type ' ],
76
- ] + self ::ITEM_BASE_SCHEMA ;
77
+ ] + self ::BASE_ITEM_SCHEMA ;
77
78
78
79
/**
79
80
* @var array<string, true>
@@ -104,9 +105,10 @@ public function __construct(
104
105
*/
105
106
public function buildSchema (string $ className , string $ format = 'jsonld ' , string $ type = Schema::TYPE_OUTPUT , ?Operation $ operation = null , ?Schema $ schema = null , ?array $ serializerContext = null , bool $ forceCollection = false ): Schema
106
107
{
107
- if ('jsonld ' !== $ format || ' input ' === $ type ) {
108
+ if ('jsonld ' !== $ format ) {
108
109
return $ this ->schemaFactory ->buildSchema ($ className , $ format , $ type , $ operation , $ schema , $ serializerContext , $ forceCollection );
109
110
}
111
+
110
112
if (!$ this ->isResourceClass ($ className )) {
111
113
$ operation = null ;
112
114
$ inputOrOutputClass = null ;
@@ -140,10 +142,11 @@ public function buildSchema(string $className, string $format = 'jsonld', string
140
142
return $ schema ;
141
143
}
142
144
143
- $ baseName = Schema::TYPE_OUTPUT === $ type ? self ::ITEM_BASE_SCHEMA_NAME : self ::ITEM_BASE_SCHEMA_OUTPUT_NAME ;
145
+ $ isOutput = Schema::TYPE_OUTPUT === $ type ;
146
+ $ baseName = $ isOutput ? self ::OUTPUT_ITEM_SCHEMA_NAME : self ::INPUT_ITEM_SCHEMA_NAME ;
144
147
145
148
if (!isset ($ definitions [$ baseName ])) {
146
- $ definitions [$ baseName ] = Schema:: TYPE_OUTPUT === $ type ? self ::ITEM_BASE_SCHEMA_OUTPUT : self ::ITEM_BASE_SCHEMA ;
149
+ $ definitions [$ baseName ] = $ isOutput ? self ::OUTPUT_ITEM_SCHEMA : self ::BASE_ITEM_SCHEMA ;
147
150
}
148
151
149
152
$ allOf = new \ArrayObject (['allOf ' => [
@@ -171,7 +174,7 @@ public function buildSchema(string $className, string $format = 'jsonld', string
171
174
172
175
$ hydraPrefix = $ this ->getHydraPrefix ($ serializerContext + $ this ->defaultContext );
173
176
174
- if (!isset ($ definitions [self ::COLLECTION_BASE_SCHEMA_NAME ])) {
177
+ if (!isset ($ definitions [self ::COLLECTION_SCHEMA_NAME ])) {
175
178
switch ($ schema ->getVersion ()) {
176
179
// JSON Schema + OpenAPI 3.1
177
180
case Schema::VERSION_OPENAPI :
@@ -184,7 +187,7 @@ public function buildSchema(string $className, string $format = 'jsonld', string
184
187
break ;
185
188
}
186
189
187
- $ definitions [self ::COLLECTION_BASE_SCHEMA_NAME ] = [
190
+ $ definitions [self ::COLLECTION_SCHEMA_NAME ] = [
188
191
'type ' => 'object ' ,
189
192
'required ' => [
190
193
$ hydraPrefix .'member ' ,
@@ -261,7 +264,7 @@ public function buildSchema(string $className, string $format = 'jsonld', string
261
264
$ schema ['type ' ] = 'object ' ;
262
265
$ schema ['description ' ] = "$ definitionName collection. " ;
263
266
$ schema ['allOf ' ] = [
264
- ['$ref ' => $ prefix .self ::COLLECTION_BASE_SCHEMA_NAME ],
267
+ ['$ref ' => $ prefix .self ::COLLECTION_SCHEMA_NAME ],
265
268
[
266
269
'type ' => 'object ' ,
267
270
'properties ' => [
@@ -285,3 +288,4 @@ public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
285
288
}
286
289
}
287
290
}
291
+
0 commit comments