cebe / php-openapi

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

Should x-* references be parsed? #100

Closed amnuts closed 3 years ago

amnuts commented 3 years ago

I'm using x-* extensions in a number of places and wanted to split up my OAS doc over multiple files. If I have a x-subscription content inline in the main file at root level, like this:

    "x-subscriptions": [
        {
            "on": "some.event",
            "do": [
                {
                    //...
                }
            ]
        }

Then after I do $specs = Reader::readFromJsonFile(realpath($specPath)); I can access the subscriptions, eg var_dump($spec->{'x-subscriptions'}); gives me:

array(1) {
  [0]=>
  array(2) {
    ["on"]=>
    string(32) "some.event"
    ["do"]=>
    array(1) {
        ...

But if I have:

    "x-subscriptions": {
        "$ref": "./x-subscriptions/some-event.json"
    },

with the full array moved to another file, the same $spec calls returns:

array(1) {
  ["$ref"]=>
  string(46) "./x-subscriptions/some-event.json"
}

I've tried a variety of ways (using 'allOf', having the array be in the root and reference individual events, etc.) but the reference doesn't resolve.

So I guess my question is; should it?

Thanks!

cebe commented 3 years ago

have you called resolveReferences() method on the base object after reading the file?

eric-pfi commented 3 years ago

I'm having a similar issue. If I define a path that contains an x-* attribute without using a reference then:

$spec->paths['path']->{'x-*'}

contains the value of the x-* attribute. However, if the path only contains:

$ref: 'filename.yaml'

where filename.yaml has the x-attribute defined, then:

$spec->paths['path']->{'x-*'}

throws the exception: Exception: Getting unknown property: cebe\openapi\spec\PathItem::x-*

I've worked around it for the moment in an odd way, if I define the path so that the x- attribute is in the paths spec, followed by the $ref attribute, then I'm able to access the x- property as expected. More specifically this works:

paths:
  'path':
    x-*: 'value'
    $ref: 'filename.yaml'

and this doesn't:

paths:
  'path':
    $ref: 'filename.yaml'  # where x-* is a root element of filename.yaml

I'm unsure if this is clear enough, and if it is the same issue. If you need a working example, I can probably put it together.

cebe commented 3 years ago

Reproduced, added a failing test case in #111. If someone wants to dig into fixing this, I'd be happy to merge a fix. Have no time to fix it myself right now.