Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
724 views
in Technique[技术] by (71.8m points)

symfony - OpenApiFactory 2.6.x decorating context

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...