All Projects → cebe → Php Openapi

cebe / Php Openapi

Licence: mit
Read and write OpenAPI yaml/json files and make the content accessible in PHP objects.

Projects that are alternatives of or similar to Php Openapi

Apispec
A pluggable API specification generator. Currently supports the OpenAPI Specification (f.k.a. the Swagger specification)..
Stars: ✭ 831 (+210.07%)
Mutual labels:  api, openapi, openapi3, hacktoberfest
Mockoon
Mockoon is the easiest and quickest way to run mock APIs locally. No remote deployment, no account required, open source.
Stars: ✭ 3,448 (+1186.57%)
Mutual labels:  api, openapi, openapi3, hacktoberfest
Openapi Generator
OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
Stars: ✭ 10,634 (+3867.91%)
Mutual labels:  api, openapi, openapi3, hacktoberfest
openapi-schema-validator
OpenAPI schema validator for Python
Stars: ✭ 35 (-86.94%)
Mutual labels:  schema, openapi, openapi3
Compojure Api
Sweet web apis with Compojure & Swagger
Stars: ✭ 1,056 (+294.03%)
Mutual labels:  api, openapi, schema
Vue Openapi
OpenAPI viewer component for VueJS
Stars: ✭ 66 (-75.37%)
Mutual labels:  api, openapi, openapi3
Kin Openapi
OpenAPI 3.0 implementation for Go (parsing, converting, validation, and more)
Stars: ✭ 776 (+189.55%)
Mutual labels:  api, openapi, openapi3
Tcases
A model-based test case generator
Stars: ✭ 103 (-61.57%)
Mutual labels:  api, openapi, openapi3
Openapi Viewer
Browse and test a REST API described with the OpenAPI 3.0 Specification
Stars: ✭ 82 (-69.4%)
Mutual labels:  api, openapi, openapi3
thema
A CUE-based framework for portable, evolvable schema
Stars: ✭ 41 (-84.7%)
Mutual labels:  schema, openapi, openapi3
Openapi Directory
🌐 Wikipedia for Web APIs. Directory of REST API definitions in OpenAPI 2.0/3.x format
Stars: ✭ 2,635 (+883.21%)
Mutual labels:  api, openapi, openapi3
Fastapi
FastAPI framework, high performance, easy to learn, fast to code, ready for production
Stars: ✭ 39,588 (+14671.64%)
Mutual labels:  api, openapi, openapi3
Widdershins
OpenAPI / Swagger, AsyncAPI & Semoasa definitions to (re)Slate compatible markdown
Stars: ✭ 856 (+219.4%)
Mutual labels:  api, openapi, openapi3
Flama
🔥 Fire up your API with this flamethrower
Stars: ✭ 161 (-39.93%)
Mutual labels:  api, openapi, schema
Openapi Diff
Utility for comparing two OpenAPI specifications.
Stars: ✭ 208 (-22.39%)
Mutual labels:  api, openapi, openapi3
Swagger Combine
Combines multiple Swagger schemas into one dereferenced schema.
Stars: ✭ 102 (-61.94%)
Mutual labels:  api, openapi, hacktoberfest
Awesome Openapi3
😎 A list of awesome projects related to OpenAPI 3.0.x, curated by the community
Stars: ✭ 469 (+75%)
Mutual labels:  api, openapi, openapi3
Oas Kit
Convert Swagger 2.0 definitions to OpenAPI 3.0 and resolve/validate/lint
Stars: ✭ 516 (+92.54%)
Mutual labels:  api, openapi, openapi3
Angular Swagger Ui
An angularJS implementation of Swagger UI
Stars: ✭ 131 (-51.12%)
Mutual labels:  api, openapi, openapi3
Openapi Codegen
OpenAPI 3.0 CodeGen plus Node.js minus the Java and emojis
Stars: ✭ 224 (-16.42%)
Mutual labels:  api, openapi, openapi3

php-openapi

Read and write OpenAPI 3.0.x YAML and JSON files and make the content accessible in PHP objects.

It also provides a CLI tool for validating and converting OpenAPI 3.0.x Description files.

Latest Stable Version Total Downloads Build Status License

Install

composer require cebe/php-openapi

Requirements

  • PHP 7.1 or higher (works fine with PHP 8)

Used by

This library provides a low level API for reading and writing OpenAPI files. It is used by higher level tools to do awesome work:

Usage

CLI Tool

$ vendor/bin/php-openapi help
PHP OpenAPI 3 tool
------------------
by Carsten Brandt <[email protected]>

Usage:
  php-openapi <command> [<options>] [input.yml|input.json] [output.yml|output.json]

  The following commands are available:

    validate   Validate the API Description in the specified input file against the OpenAPI v3.0 schema.
               Note: the validation is performed in two steps. The results are composed of
                (1) structural errors found while reading the API Description file, and
                (2) violations of the OpenAPI v3.0 schema.

               If no input file is specified input will be read from STDIN.
               The tool will try to auto-detect the content type of the input, but may fail
               to do so. You may specify --read-yaml or --read-json to force the file type.

               Exits with code 2 on validation errors, 1 on other errors and 0 on success.

    convert    Convert a JSON or YAML input file to JSON or YAML output file.

               If no input file is specified input will be read from STDIN.
               If no output file is specified output will be written to STDOUT.
               The tool will try to auto-detect the content type of the input and output file, but may fail
               to do so. You may specify --read-yaml or --read-json to force the input file type.
               and --write-yaml or --write-json to force the output file type.

               By default all references are resolved (replaced with the object referred to). You can control
               handling of references with the following arguments:

               --resolve-none      Do not resolve references.
               --resolve-external  Only resolve references that point to external files.
                                   This process is often referred to as "inlining".
               --resolve-all       Resolve all references (default).
                                   Recursive pointers will stay references.

    inline     Convert a JSON or YAML input file to JSON or YAML output file and
               resolve all external references. The output will be a single API Description file.
               This is a shortcut for calling convert --resolve-external.

    help       Shows this usage information.

  Options:

    --read-json   force reading input as JSON. Auto-detect if not specified.
    --read-yaml   force reading input as YAML. Auto-detect if not specified.
    --write-json  force writing output as JSON. Auto-detect if not specified.
    --write-yaml  force writing output as YAML. Auto-detect if not specified.
    -s, --silent  silent mode. Will hide all success/information messages and only print errors.

Reading API Description Files

Read OpenAPI Description from JSON file:

use cebe\openapi\Reader;

// realpath is needed for resolving references with relative Paths or URLs
$openapi = Reader::readFromJsonFile(realpath('openapi.json'));

Read OpenAPI Description from YAML:

use cebe\openapi\Reader;

// realpath is needed for resolving references with relative Paths or URLs
$openapi = Reader::readFromYamlFile(realpath('openapi.yaml'));
// you may also specify the URL to your API Description file
$openapi = Reader::readFromYamlFile('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/3.0.2/examples/v3.0/petstore-expanded.yaml');

Access API Description data:

echo $openapi->openapi; // openAPI version, e.g. 3.0.0
echo $openapi->info->title; // API title
foreach($openapi->paths as $path => $definition) {
    // iterate path definitions
}

Object properties are exactly like in the OpenAPI Specification. You may also access additional properties added by specification extensions.

Writing API Description Files

use cebe\openapi\spec\OpenApi;
use cebe\openapi\spec\PathItem;

// create base API Description
$openapi = new OpenApi([
    'openapi' => '3.0.2',
    'info' => [
        'title' => 'Test API',
        'version' => '1.0.0',
    ],
    'paths' => [],
]);
// manipulate description as needed
$openapi->paths['/test'] = new PathItem([
    'description' => 'something'
]);
// ...

$json = \cebe\openapi\Writer::writeToJson($openapi);

results in the following JSON data:

{
    "openapi": "3.0.0",
    "info": {
        "title": "Test API",
        "version": "1.0.0"
    },
    "paths": {
        "/test": {
            "description": "something"
        }
    }
}

Writing API Description Files using prepared Objects

Since version 1.2.0, the above example can also be written like this (passing objects instead of arrays):

use cebe\openapi\spec\OpenApi;
use cebe\openapi\spec\PathItem;
use cebe\openapi\spec\Info;


// create base API Description
$openapi = new OpenApi([
    'openapi' => '3.0.2',
    'info' => new Info([
        'title' => 'Test API',
        'version' => '1.0.0',
    ]),
    'paths' => [
        '/test' => new PathItem([
            'description' => 'something'
        ]),
    ],
]);
$json = \cebe\openapi\Writer::writeToJson($openapi);

Reading API Description Files and Resolving References

In the above we have passed the raw JSON or YAML data to the Reader. In order to be able to resolve references to structures in external files, we must provide the full context.

use cebe\openapi\Reader;
// an absolute URL or file path is needed to allow resolving external references
$openapi = Reader::readFromJsonFile('https://www.example.com/api/openapi.json');
$openapi = Reader::readFromYamlFile('https://www.example.com/api/openapi.yaml');

If data has been loaded in a different way you can manually resolve references like this by giving a context:

$openapi->resolveReferences(
    new \cebe\openapi\ReferenceContext($openapi, 'https://www.example.com/api/openapi.yaml')
);

Validation

The library provides simple validation operations, that check basic OpenAPI spec requirements. This is the same as "structural errors found while reading the API Description file" from the CLI tool. This validation does not include checking against the OpenAPI v3.0 JSON schema, this is only implemented in the CLI.

// return `true` in case no errors have been found, `false` in case of errors.
$specValid = $openapi->validate();
// after validation getErrors() can be used to retrieve the list of errors found.
$errors = $openapi->getErrors();

Note: Validation is done on a very basic level and is not complete. So a failing validation will show some errors, but the list of errors given may not be complete. Also a passing validation does not necessarily indicate a completely valid spec.

Completeness

This library is currently work in progress, the following list tracks completeness:

  • [x] read OpenAPI 3.0 JSON
  • [x] read OpenAPI 3.0 YAML
  • [ ] OpenAPI 3.0 Schema
    • [x] OpenAPI Object
    • [x] Info Object
    • [x] Contact Object
    • [x] License Object
    • [x] Server Object
    • [x] Server Variable Object
    • [x] Components Object
    • [x] Paths Object
    • [x] Path Item Object
    • [x] Operation Object
    • [x] External Documentation Object
    • [x] Parameter Object
    • [x] Request Body Object
    • [x] Media Type Object
    • [x] Encoding Object
    • [x] Responses Object
    • [x] Response Object
    • [x] Callback Object
    • [x] Example Object
    • [x] Link Object
    • [x] Header Object
    • [x] Tag Object
    • [x] Reference Object
    • [x] Schema Object
      • [x] load/read
      • [ ] validation
    • [x] Discriminator Object
    • [x] XML Object
    • [x] Security Scheme Object
    • [x] OAuth Flows Object
    • [x] OAuth Flow Object
    • [x] Security Requirement Object

Support

Need help with your API project?

Professional support, consulting as well as software development services are available:

https://www.cebe.cc/en/contact

Development of this library is sponsored by cebe.☁️ "Your Professional Deployment Platform".

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].