@@ -55,7 +55,7 @@ trait HasProperties
5555 public function properties (JsonSchema ...$ properties ): static
5656 {
5757 foreach ($ properties as $ property ) {
58- $ title = $ property -> getTitle ( );
58+ $ title = $ this -> resolvePropertyTitle ( $ property );
5959
6060 if ($ title === null ) {
6161 throw new SchemaException ('Property must have a title ' );
@@ -254,7 +254,7 @@ public function hasRequiredProperties(): bool
254254 public function requireAll (): static
255255 {
256256 foreach ($ this ->properties as $ property ) {
257- $ title = $ property -> getTitle ( );
257+ $ title = $ this -> resolvePropertyTitle ( $ property );
258258
259259 if ($ title !== null ) {
260260 $ this ->requiredProperties [] = $ title ;
@@ -277,7 +277,15 @@ protected function addPropertiesToSchema(array $schema): array
277277 $ schema ['properties ' ] = [];
278278
279279 foreach ($ this ->properties as $ name => $ prop ) {
280- $ schema ['properties ' ][$ name ] = $ prop ->toArray (includeSchemaRef: false , includeTitle: false );
280+ $ propertySchema = $ prop ->toArray (includeSchemaRef: false , includeTitle: true );
281+
282+ // If the property schema has a title and it matches the name,
283+ // then we don't need to include it in the schema
284+ if (array_key_exists ('title ' , $ propertySchema ) && $ propertySchema ['title ' ] === $ name ) {
285+ unset($ propertySchema ['title ' ]);
286+ }
287+
288+ $ schema ['properties ' ][$ name ] = $ propertySchema ;
281289 }
282290 }
283291
@@ -331,6 +339,14 @@ protected function addPropertiesToSchema(array $schema): array
331339 return $ schema ;
332340 }
333341
342+ /**
343+ * Resolve the property title from a schema instance.
344+ */
345+ protected function resolvePropertyTitle (JsonSchema $ jsonSchema ): ?string
346+ {
347+ return $ jsonSchema ->getInitialTitle () ?? $ jsonSchema ->getTitle ();
348+ }
349+
334350 /**
335351 * Get unevaluated properties features used by this schema.
336352 *
0 commit comments