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

Inline references are not resolved when creating schema from JSON\YAML string #116

Closed scaytrase closed 2 years ago

scaytrase commented 3 years ago

Original issue https://github.com/thephpleague/openapi-psr7-validator/issues/107

If a spec has header parameters with references, and references are not solved when creating the schema, findHeaderSpecs will fail with error Undefined property: cebe\openapi\spec\Reference::$in

Failing test case ```php paths->getPath('/products.create')->getOperations()['post']; foreach ($operation->parameters as $parameter) { if ($parameter->in !== 'header') { continue; } $this->addToAssertionCount(1); } } } ```

Fails with

Undefined property: cebe\openapi\spec\Reference::$in
HeaderParametersReferenceResolution.php:59
cebe commented 3 years ago

this is expected behavior, you have to call resolveReferences() manually when reading from a string, because in case of reading from string there is no context which can be used to resolve relative paths.

scaytrase commented 3 years ago

Can we resolve at least inline references without explicity calling resolveReferences() (i.e if string spec contains no external file references)

cebe commented 2 years ago

You can resolve references in this cases like that:

$schema = Reader::readFromYaml($yaml);
$schema->resolveReferences(new \cebe\openapi\ReferenceContext($schema, ''));

that might break if you refer to any files using relative paths though.

cebe commented 2 years ago

not sure if this really a use case that is worth implementing, you can call resolveReferences() as shown above if needed, won't implement this unless there is any demand for it.