13
13
14
14
namespace ApiPlatform \Metadata \Property \Factory ;
15
15
16
+ use ApiPlatform \JsonSchema \Metadata \Property \Factory \SchemaPropertyMetadataFactory ;
16
17
use ApiPlatform \Metadata \ApiProperty ;
17
18
use ApiPlatform \Metadata \Exception \PropertyNotFoundException ;
18
19
use ApiPlatform \Metadata \Extractor \PropertyExtractorInterface ;
@@ -52,7 +53,7 @@ public function create(string $resourceClass, string $property, array $options =
52
53
}
53
54
54
55
if ($ parentPropertyMetadata ) {
55
- return $ this ->update ($ parentPropertyMetadata , $ propertyMetadata );
56
+ return $ this ->handleUserDefinedSchema ( $ this -> update ($ parentPropertyMetadata , $ propertyMetadata) );
56
57
}
57
58
58
59
$ apiProperty = new ApiProperty ();
@@ -69,7 +70,7 @@ public function create(string $resourceClass, string $property, array $options =
69
70
}
70
71
}
71
72
72
- return $ apiProperty ;
73
+ return $ this -> handleUserDefinedSchema ( $ apiProperty) ;
73
74
}
74
75
75
76
/**
@@ -91,39 +92,22 @@ private function handleNotFound(?ApiProperty $parentPropertyMetadata, string $re
91
92
*/
92
93
private function update (ApiProperty $ propertyMetadata , array $ metadata ): ApiProperty
93
94
{
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 );
124
98
}
99
+ }
125
100
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 );
127
111
}
128
112
129
113
return $ propertyMetadata ;
0 commit comments