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

Inline references in referenced files are not resolved #97

Closed damiendiels closed 3 years ago

damiendiels commented 3 years ago

It seems that since version 1.5 , references in referenced files are not resolved. An error like "Cannot find component with name ..." is throwed. It worked fine in previous version.

vdelau commented 3 years ago

I'm not sure I understand the original issue correctly, but I think I'm running into the same issue here. I'm trying to load a file from this repository (and branch): https://forge.etsi.org/rep/3GPP/5G_APIs/tree/REL-16

This is the simple snippet I'm using to do this, where ../specs/5G_APIs is a clone of the above repository:

<?php
require_once __DIR__ . '/../vendor/autoload.php';

use cebe\openapi\Reader;

$specfile = realpath(__DIR__ . '/../specs/5G_APIs/TS32291_Nchf_OfflineOnlyCharging.yaml');
$openapi = Reader::readFromYamlFile($specfile);

This fails with the following exception:

D:\Development\opt\php-8.0.0\php.exe D:\Development\PHP\5g_api\bin\generator.php
PHP Fatal error:  Uncaught cebe\openapi\json\NonexistentJsonPointerReferenceException: Failed to evaluate pointer '/components/schemas/Tai'. Array has no member Tai at path '/components/schemas'. in D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\json\JsonPointer.php:116
Stack trace:
#0 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\spec\Reference.php(199): cebe\openapi\json\JsonPointer->evaluate(Object(cebe\openapi\spec\OpenApi))
#1 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\SpecBaseObject.php(391): cebe\openapi\spec\Reference->resolve(Object(cebe\openapi\ReferenceContext))
#2 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\SpecBaseObject.php(397): cebe\openapi\SpecBaseObject->resolveReferences(NULL)
#3 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\SpecBaseObject.php(394): cebe\openapi\SpecBaseObject->resolveReferences()
#4 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\SpecBaseObject.php(394): cebe\openapi\SpecBaseObject->resolveReferences()
#5 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\SpecBaseObject.php(384): cebe\openapi\SpecBaseObject->resolveReferences()
#6 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\SpecBaseObject.php(397): cebe\openapi\SpecBaseObject->resolveReferences(NULL)
#7 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\SpecBaseObject.php(384): cebe\openapi\SpecBaseObject->resolveReferences()
#8 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\SpecBaseObject.php(397): cebe\openapi\SpecBaseObject->resolveReferences(NULL)
#9 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\SpecBaseObject.php(384): cebe\openapi\SpecBaseObject->resolveReferences()
#10 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\SpecBaseObject.php(397): cebe\openapi\SpecBaseObject->resolveReferences(NULL)
#11 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\SpecBaseObject.php(387): cebe\openapi\SpecBaseObject->resolveReferences(NULL)
#12 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\SpecBaseObject.php(387): cebe\openapi\SpecBaseObject->resolveReferences(NULL)
#13 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\spec\PathItem.php(194): cebe\openapi\SpecBaseObject->resolveReferences(NULL)
#14 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\spec\Paths.php(254): cebe\openapi\spec\PathItem->resolveReferences(NULL)
#15 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\SpecBaseObject.php(387): cebe\openapi\spec\Paths->resolveReferences(NULL)
#16 D:\Development\PHP\5g_api\vendor\cebe\php-openapi\src\Reader.php(134): cebe\openapi\SpecBaseObject->resolveReferences()
#17 D:\Development\PHP\5g_api\bin\generator.php(7): cebe\openapi\Reader::readFromYamlFile('D:\\Development\\...')
#18 {main}

Based on the issue, I've downgraded to 1.4.2 and that executes without a problem.

samizdam commented 3 years ago

After upgrade to 1.5.1 I'm get cebe\openapi\exceptions\UnresolvableReferenceException on reference in referenced file: https://github.com/FreeElephants/json-api-php-toolkit/runs/2539120065?check_suite_focus=true#step:9:15

marcelthole commented 3 years ago

i tried the example from @vdelau and can't reproduce the error with the latest dev-master version.

Repro:

  1. composer require cebe/php-openapi
  2. use 5G Apis yaml files (in my example from https://github.com/WhyBingo/5GC_APIs)
  3. use the provided snipped.php

-> I got the same error

Fix:

  1. composer require cebe/php-openapi:dev-master -> No errors anymore.

In the second example from @samizdam i have the same result.

  1. git clone https://github.com/FreeElephants/json-api-php-toolkit.git
  2. git checkout a9e501736a6da9fbc9a6c82cbfec60cd7d9442ad
  3. composer install
  4. vendor/bin/phpunit -> I got the same error

if i require "cebe/php-openapi": "dev-master" and run the phpunit tests i got no errors anymore.

In that case a new tag for cebe/php-openapi 1.5.2 should fix the errors in this example.

Each package maintainer that requires cebe/php-openapi could add the following in the composer.json

    "conflict": {
        "cebe/php-openapi": "1.5.0 || 1.5.1"
    }
cebe commented 3 years ago

Looks like this is fixed in dev-master, so it will be fixed by upgrading to 1.5.2, which I'll release today.

Thanks @marcelthole for checking.