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

ReferenceContext::reduceDots shows `Undefined array key 7` error when `..` repeatedly appeared in path #94

Closed il-m-yamagishi closed 3 years ago

il-m-yamagishi commented 3 years ago

Thank you for the great library.

I found to occur ErrorException when .. repeatedly appeared in path at ReferenceContext::reduceDots. PHP 8.0.0.

ErrorException {#587
  #message: "Undefined array key 7"
  #code: 0
  #file: "./vendor/cebe/php-openapi/src/ReferenceContext.php"
  #line: 123
  #severity: E_WARNING
  trace: {
    ./vendor/cebe/php-openapi/src/ReferenceContext.php:123 { …}
    ./vendor/cebe/php-openapi/src/ReferenceContext.php:195 { …}
    ./vendor/cebe/php-openapi/src/spec/Reference.php:307 { …}
    ./vendor/cebe/php-openapi/src/spec/Reference.php:315 { …}
    ./vendor/cebe/php-openapi/src/spec/Reference.php:315 { …}
    ./vendor/cebe/php-openapi/src/spec/Reference.php:315 { …}
    ./vendor/cebe/php-openapi/src/spec/Reference.php:229 { …}
    ./vendor/cebe/php-openapi/src/spec/PathItem.php:156 { …}
    ./vendor/cebe/php-openapi/src/spec/Paths.php:254 { …}
    ./vendor/cebe/php-openapi/src/SpecBaseObject.php:387 { …}
    ./vendor/cebe/php-openapi/src/Reader.php:134 { …}
    ...
$path = /var/www/app/contracts/api/paths/account/../../../responses/InternalServerError.yml
$parts = array:10 [
  0 => "var"
  1 => "www"
  2 => "app"
  3 => "contracts"
  4 => "api"
  5 => "paths"
  8 => ".."
  9 => ".."
  10 => "responses"
  11 => "InternalServerError.yml"
]
# /var/www/app/contracts/api/openapi.yml
openapi: 3.0.3
paths:
  /account/create:
    $ref: 'paths/account/createPaths.yml'
# /var/www/app/contracts/api/paths/account/createPaths.yml
post:
  requestBody:
    $ref: createRequest.yml
  responses:
    default:
      $ref: '../../../responses/InternalServerError.yml'
    '200':
      $ref: createResponse.yml
# /var/www/app/contracts/responses/InternalServerError.yml
content:
  application/json:
    schema:
      type: object
      properties:
        error:
          type: string
amnuts commented 3 years ago

I'm hoping this does make its was into the main branch sometime soon. I'm trying to do local references like:

        "parameters": [
            {
                "$ref": "../../parameters.json#/EmailAddress"
            },
            {
                "$ref": "../../parameters.json#/LaunchpadCodeInQuery"
            }
        ],

and without this fix is fails to resolve the path correctly, but with this PR is works perfectly. Thanks for the fix @il-m-yamagishi

il-m-yamagishi commented 3 years ago

This is workaround until the PR will be merged.

$ composer require cweagans/composer-patches

And add to composer.json

{
    "extra": {
        "patches": {
            "cebe/php-openapi": {
                "Workaround cebe/php-openapi#94": "https://patch-diff.githubusercontent.com/raw/cebe/php-openapi/pull/99.patch"
            }
        }
    }
}

So patch will be executed when use composer install.

$ composer install
...
Gathering patches for root package.
Gathering patches for dependencies. This might take a minute.
  - Installing cebe/php-openapi (1.5.0): Extracting archive
  - Applying patches for cebe/php-openapi
    https://patch-diff.githubusercontent.com/raw/cebe/php-openapi/pull/99.patch (Workaround cebe/php-openapi#94)
...

https://github.com/vaimo/composer-patches has not compatible with composer v2 so I use cweagans/composer-patches instead.

cebe commented 3 years ago

I just release version 1.5.1 which includes this fix.