cebe / php-openapi

Read and write OpenAPI yaml/json files and make the content accessible in PHP objects.
MIT License
466 stars 87 forks source link

Convert to inline spec without using the console command #122

Closed antonkomarev closed 2 years ago

antonkomarev commented 2 years ago

Hi @cebe! Thank you for the package, you've done a great job!

I need to fetch openapi files with refs and compile them to the single inline file. I saw there is a way to make it in console command, but I want to do the same just in my controllers.

This is how I tried to make it:

$file = $this->client->repositoryFiles()->getFile($projectId, $filePath, 'master');

$content = base64_decode($file['content']);
$openapi = Reader::readFromYaml($content);

$yaml = Writer::writeToYaml($openapi);
dd($yaml);

But I still have yaml file with refs. How can I fetch all the refs and convert them to the single file?

// Spec files are not public, so readFromYamlFile is returning an error.

cebe commented 2 years ago

You can inline external references like this:

$inputFile = 'file://tmp/yourfile.yaml'; // change this to a valid URI to your file.
$openApi = \cebe\openapi\Reader::readFromYamlFile($inputFile, \cebe\openapi\spec\OpenApi::class, ReferenceContext::RESOLVE_MODE_INLINE);
// use $openApi without external references here

// or write it to a file
\cebe\openapi\Writer::writeToYaml($openApi);
antonkomarev commented 2 years ago

Thank you