@@ -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 ' );
@@ -253,12 +253,8 @@ public function hasRequiredProperties(): bool
253253 */
254254 public function requireAll (): static
255255 {
256- foreach ($ this ->properties as $ property ) {
257- $ title = $ property ->getTitle ();
258-
259- if ($ title !== null ) {
260- $ this ->requiredProperties [] = $ title ;
261- }
256+ foreach ($ this ->properties as $ name => $ property ) {
257+ $ this ->requiredProperties [] = $ name ;
262258 }
263259
264260 return $ this ;
@@ -277,7 +273,15 @@ protected function addPropertiesToSchema(array $schema): array
277273 $ schema ['properties ' ] = [];
278274
279275 foreach ($ this ->properties as $ name => $ prop ) {
280- $ schema ['properties ' ][$ name ] = $ prop ->toArray (includeSchemaRef: false , includeTitle: false );
276+ $ propertySchema = $ prop ->toArray (includeSchemaRef: false , includeTitle: true );
277+
278+ // If the property schema has a title and it matches the name,
279+ // then we don't need to include it in the schema
280+ if (array_key_exists ('title ' , $ propertySchema ) && $ propertySchema ['title ' ] === $ name ) {
281+ unset($ propertySchema ['title ' ]);
282+ }
283+
284+ $ schema ['properties ' ][$ name ] = $ propertySchema ;
281285 }
282286 }
283287
@@ -331,6 +335,14 @@ protected function addPropertiesToSchema(array $schema): array
331335 return $ schema ;
332336 }
333337
338+ /**
339+ * Resolve the property title from a schema instance.
340+ */
341+ protected function resolvePropertyTitle (JsonSchema $ property ): ?string
342+ {
343+ return $ property ->getInitialTitle () ?? $ property ->getTitle ();
344+ }
345+
334346 /**
335347 * Get unevaluated properties features used by this schema.
336348 *
0 commit comments