cebe / php-openapi

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

readFromJsonFile not working? #29

Closed githubuser441 closed 5 years ago

githubuser441 commented 5 years ago

I used the code from readme file but it's not working. code: `require DIR . '/vendor/autoload.php'; use cebe\openapi\Reader;

// realpath is needed for resolving references with relative Paths or URLs $openapi = Reader::readFromJsonFile(realpath('shootproof-studio-openapi.json'));` Using this json file: https://developer.shootproof.com/oas/studio.json

cebe commented 5 years ago

Thanks for reporting, I will look into this.

Just to confirm, when I try to load the JSON file you specify I get that error:

Fatal error: Uncaught TypeError: Argument 1 passed to cebe\openapi\ReferenceContext::resolveRelativeUri() must be of the type string, null given, called in /tmp/test/vendor/cebe/php-openapi/src/spec/Reference.php on line 139 and defined in /tmp/test/vendor/cebe/php-openapi/src/ReferenceContext.php on line 77

TypeError: Argument 1 passed to cebe\openapi\ReferenceContext::resolveRelativeUri() must be of the type string, null given, called in /tmp/test/vendor/cebe/php-openapi/src/spec/Reference.php on line 139 in /tmp/test/vendor/cebe/php-openapi/src/ReferenceContext.php on line 77

Call Stack:
    0.0001     360784   1. {main}() /tmp/test/test.php:0
    0.0009     453776   2. cebe\openapi\Reader::readFromJsonFile() /tmp/test/test.php:7
    0.9087    4320888   3. cebe\openapi\spec\OpenApi->resolveReferences() /tmp/test/vendor/cebe/php-openapi/src/Reader.php:72
    0.9088    4320888   4. cebe\openapi\spec\Paths->resolveReferences() /tmp/test/vendor/cebe/php-openapi/src/SpecBaseObject.php:295
    0.9256    4183648   5. cebe\openapi\spec\PathItem->resolveReferences() /tmp/test/vendor/cebe/php-openapi/src/spec/Paths.php:228
    0.9256    4183648   6. cebe\openapi\spec\Operation->resolveReferences() /tmp/test/vendor/cebe/php-openapi/src/SpecBaseObject.php:295
    0.9256    4183648   7. cebe\openapi\spec\RequestBody->resolveReferences() /tmp/test/vendor/cebe/php-openapi/src/SpecBaseObject.php:295
    0.9256    4183648   8. cebe\openapi\spec\MediaType->resolveReferences() /tmp/test/vendor/cebe/php-openapi/src/SpecBaseObject.php:301
    0.9256    4183648   9. cebe\openapi\spec\Reference->resolve() /tmp/test/vendor/cebe/php-openapi/src/SpecBaseObject.php:293
    0.9256    4183648  10. cebe\openapi\ReferenceContext->resolveRelativeUri() /tmp/test/vendor/cebe/php-openapi/src/spec/Reference.php:139

is this the same error you are seeing?

githubuser441 commented 5 years ago

Yes, also I just noticed a typo in the readme: $openapi = Reader::readFromYamlFile(realpath('openapi.json')); You see it's loading json with yaml function.

cebe commented 5 years ago

seems their spec has some interesting constructs.

  1. references that have additional properties
  2. recursive schema definitions

both cause multiple issues when loading and validating the spec.

cebe commented 5 years ago

This should be fixed by https://github.com/cebe/php-openapi/pull/30, thanks for reporting! The spec you linked showed off a lot of issues that existed in the code. Should work much better now.