@@ -37,7 +37,6 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
37
37
use SchemaUriPrefixTrait;
38
38
39
39
private const ITEM_BASE_SCHEMA_NAME = 'HydraItemBaseSchema ' ;
40
- private const ITEM_BASE_SCHEMA_OUTPUT_NAME = 'HydraOutputBaseSchema ' ;
41
40
private const COLLECTION_BASE_SCHEMA_NAME = 'HydraCollectionBaseSchema ' ;
42
41
private const BASE_PROP = [
43
42
'type ' => 'string ' ,
@@ -69,11 +68,8 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
69
68
],
70
69
],
71
70
] + self ::BASE_PROPS ,
72
- ];
73
-
74
- private const ITEM_BASE_SCHEMA_OUTPUT = [
75
71
'required ' => ['@id ' , '@type ' ],
76
- ] + self :: ITEM_BASE_SCHEMA ;
72
+ ];
77
73
78
74
/**
79
75
* @var array<string, true>
@@ -104,7 +100,15 @@ public function __construct(
104
100
*/
105
101
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
102
{
107
- if ('jsonld ' !== $ format || 'input ' === $ type ) {
103
+ // The input schema must not include `@id` or `@type` as required fields, so it should be a pure JSON schema.
104
+ // Strictly speaking, it is possible to include `@id` or `@context` in the input,
105
+ // but the generated JSON Schema does not include `"additionalProperties": false` by default,
106
+ // so it is possible to include `@id` or `@context` in the input even if the input schema is a JSON schema.
107
+ if (Schema::TYPE_INPUT === $ type ) {
108
+ $ format = 'json ' ;
109
+ }
110
+
111
+ if ('jsonld ' !== $ format ) {
108
112
return $ this ->schemaFactory ->buildSchema ($ className , $ format , $ type , $ operation , $ schema , $ serializerContext , $ forceCollection );
109
113
}
110
114
if (!$ this ->isResourceClass ($ className )) {
@@ -140,10 +144,10 @@ public function buildSchema(string $className, string $format = 'jsonld', string
140
144
return $ schema ;
141
145
}
142
146
143
- $ baseName = Schema:: TYPE_OUTPUT === $ type ? self ::ITEM_BASE_SCHEMA_NAME : self :: ITEM_BASE_SCHEMA_OUTPUT_NAME ;
147
+ $ baseName = self ::ITEM_BASE_SCHEMA_NAME ;
144
148
145
149
if (!isset ($ definitions [$ baseName ])) {
146
- $ definitions [$ baseName ] = Schema:: TYPE_OUTPUT === $ type ? self :: ITEM_BASE_SCHEMA_OUTPUT : self ::ITEM_BASE_SCHEMA ;
150
+ $ definitions [$ baseName ] = self ::ITEM_BASE_SCHEMA ;
147
151
}
148
152
149
153
$ allOf = new \ArrayObject (['allOf ' => [
0 commit comments