@@ -37,7 +37,6 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
3737 use SchemaUriPrefixTrait;
3838
3939 private const ITEM_BASE_SCHEMA_NAME = 'HydraItemBaseSchema ' ;
40- private const ITEM_BASE_SCHEMA_OUTPUT_NAME = 'HydraOutputBaseSchema ' ;
4140 private const COLLECTION_BASE_SCHEMA_NAME = 'HydraCollectionBaseSchema ' ;
4241 private const BASE_PROP = [
4342 'type ' => 'string ' ,
@@ -69,11 +68,8 @@ final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareI
6968 ],
7069 ],
7170 ] + self ::BASE_PROPS ,
72- ];
73-
74- private const ITEM_BASE_SCHEMA_OUTPUT = [
7571 'required ' => ['@id ' , '@type ' ],
76- ] + self :: ITEM_BASE_SCHEMA ;
72+ ];
7773
7874 /**
7975 * @var array<string, true>
@@ -104,7 +100,15 @@ public function __construct(
104100 */
105101 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
106102 {
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 ) {
108112 return $ this ->schemaFactory ->buildSchema ($ className , $ format , $ type , $ operation , $ schema , $ serializerContext , $ forceCollection );
109113 }
110114 if (!$ this ->isResourceClass ($ className )) {
@@ -140,10 +144,10 @@ public function buildSchema(string $className, string $format = 'jsonld', string
140144 return $ schema ;
141145 }
142146
143- $ baseName = Schema:: TYPE_OUTPUT === $ type ? self ::ITEM_BASE_SCHEMA_NAME : self :: ITEM_BASE_SCHEMA_OUTPUT_NAME ;
147+ $ baseName = self ::ITEM_BASE_SCHEMA_NAME ;
144148
145149 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 ;
147151 }
148152
149153 $ allOf = new \ArrayObject (['allOf ' => [
0 commit comments