1313
1414namespace ApiPlatform \Metadata \Property \Factory ;
1515
16+ use ApiPlatform \JsonSchema \Metadata \Property \Factory \SchemaPropertyMetadataFactory ;
1617use ApiPlatform \Metadata \ApiProperty ;
1718use ApiPlatform \Metadata \Exception \PropertyNotFoundException ;
1819use ApiPlatform \Metadata \Extractor \PropertyExtractorInterface ;
@@ -52,7 +53,7 @@ public function create(string $resourceClass, string $property, array $options =
5253 }
5354
5455 if ($ parentPropertyMetadata ) {
55- return $ this ->update ($ parentPropertyMetadata , $ propertyMetadata );
56+ return $ this ->handleUserDefinedSchema ( $ this -> update ($ parentPropertyMetadata , $ propertyMetadata) );
5657 }
5758
5859 $ apiProperty = new ApiProperty ();
@@ -69,7 +70,7 @@ public function create(string $resourceClass, string $property, array $options =
6970 }
7071 }
7172
72- return $ apiProperty ;
73+ return $ this -> handleUserDefinedSchema ( $ apiProperty) ;
7374 }
7475
7576 /**
@@ -91,39 +92,22 @@ private function handleNotFound(?ApiProperty $parentPropertyMetadata, string $re
9192 */
9293 private function update (ApiProperty $ propertyMetadata , array $ metadata ): ApiProperty
9394 {
94- $ metadataAccessors = [
95- 'description ' => 'get ' ,
96- 'readable ' => 'is ' ,
97- 'writable ' => 'is ' ,
98- 'writableLink ' => 'is ' ,
99- 'readableLink ' => 'is ' ,
100- 'required ' => 'is ' ,
101- 'identifier ' => 'is ' ,
102- 'default ' => 'get ' ,
103- 'example ' => 'get ' ,
104- 'deprecationReason ' => 'get ' ,
105- 'fetchable ' => 'is ' ,
106- 'fetchEager ' => 'get ' ,
107- 'jsonldContext ' => 'get ' ,
108- 'openapiContext ' => 'get ' ,
109- 'jsonSchemaContext ' => 'get ' ,
110- 'push ' => 'get ' ,
111- 'security ' => 'get ' ,
112- 'securityPostDenormalize ' => 'get ' ,
113- 'types ' => 'get ' ,
114- 'builtinTypes ' => 'get ' ,
115- 'schema ' => 'get ' ,
116- 'initializable ' => 'is ' ,
117- 'genId ' => 'get ' ,
118- 'extraProperties ' => 'get ' ,
119- ];
120-
121- foreach ($ metadataAccessors as $ metadataKey => $ accessorPrefix ) {
122- if (null === $ metadata [$ metadataKey ]) {
123- continue ;
95+ foreach (get_class_methods (ApiProperty::class) as $ method ) {
96+ if (preg_match ('/^(?:get|is)(.*)/ ' , (string ) $ method , $ matches ) && null !== $ val = $ metadata [lcfirst ($ matches [1 ])]) {
97+ $ propertyMetadata = $ propertyMetadata ->{"with {$ matches [1 ]}" }($ val );
12498 }
99+ }
125100
126- $ propertyMetadata = $ propertyMetadata ->{'with ' .ucfirst ($ metadataKey )}($ metadata [$ metadataKey ]);
101+ return $ propertyMetadata ;
102+ }
103+
104+ private function handleUserDefinedSchema (ApiProperty $ propertyMetadata ): ApiProperty
105+ {
106+ // can't know later if the schema has been defined by the user or by API Platform
107+ // store extra key to make this difference
108+ if (null !== $ propertyMetadata ->getSchema ()) {
109+ $ extraProperties = $ propertyMetadata ->getExtraProperties () ?? [];
110+ $ propertyMetadata = $ propertyMetadata ->withExtraProperties ([SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED => true ] + $ extraProperties );
127111 }
128112
129113 return $ propertyMetadata ;
0 commit comments