I passed on 2.6.x ApiPlatform recently and I have a SwaggerDecorator service to generate some custom documentation.
As I could see now we have to decorate api_platform.openapi.factory
in order to customize our documentation.
Problem : as before it was a normalizer, we had the format and context passed to the custom decorator :
public function normalize($object, $format = null, array $context = []): array
and each normalize/denormalize context was perform to generate the documentation.
I try to find a way to reproduce that with decorating the new OpenAPiFactory
Questions :
- how can I get the format now ?
- how can I get my custom documentation working for all output context ?
relative documentation links just have a single example :
[https://api-platform.com/docs/core/openapi/]
actual stack
- PHP 7.4.8
- Symfony 5.2.x
- ApiPlatform 2.6.x
actual try code :
public function __invoke(array $context = []): OpenApi
{
$result = $this->cache->get('openapi_documentation', function (ItemInterface $item) use ($context) {
// Build default doc
$this->docs = $this->decorated->__invoke($context);
if (!$this->docs instanceof OpenApi) {
return $this->docs;
}
// Complete API docudmentation if files exist in schemas configuration directory
$schemas = $this->docs->getComponents()->getSchemas();
foreach ($schemas ?? [] as $k => &$schema) {
$schemaNameExploded = explode(':', $k);
$objectName = strstr($schemaNameExploded[0], '-', true) ?: $schemaNameExploded[0];
$docFile = $this->confDir.'/packages/api_platform/schemas/'.$objectName.'.yaml';
$this->completeDoc($schema, $docFile);
}
});
return $result;
}
/**
* Complete the openapi documentation of $schema with $docFile.
*
* @param array|ArrayObject $schema
*
* @return mixed
*/
private function completeDoc(&$schema, string $docFile)
{
if (!file_exists($docFile)) {
return $schema;
} else {
$docFileParsed = (array) Yaml::parseFile($docFile);
}
// Treatment of the description
if (isset($docFileParsed['description'])) {
$schema['description'] = $docFileParsed['description'];
}
// Treatment of properties
if (isset($schema['properties'])) {
$newProperties = $docFileParsed['properties'] ?? [];
foreach ($schema['properties'] as $k => &$currentProperty) {
if (!empty($newProperties[$k])) {
$currentPropertyArray = $currentProperty instanceof ArrayObject ? $currentProperty->getArrayCopy() : $currentProperty;
$currentProperty = new ArrayObject(array_replace($currentPropertyArray, $newProperties[$k]));
}
}
}
}
thx
question from:
https://stackoverflow.com/questions/65922371/openapifactory-2-6-x-decorating-context 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…