swaggest/swagger2-schema

Swagger 2.0 schema PHP mappings

v0.2.6 2021-06-17 22:37 UTC

This package is auto-updated.

Last update: 2024-11-18 05:57:35 UTC


README

Build Status codecov

Access and validate OpenAPI 3.0 and Swagger 2.0 schemas from PHP.

Installation

composer require swaggest/swagger2-schema

Usage

OpenAPI 3.0

// Load schema
$json = json_decode(file_get_contents(__DIR__ . '/../../../spec/petstore-openapi3.json'));

// Import and validate
$schema = OpenAPI3Schema::import($json);

// Access data through PHP classes
$this->assertSame('Swagger Petstore', $schema->info->title);
$ops = $schema->paths['/pets']->getGetPutPostDeleteOptionsHeadPatchTraceValues();
$this->assertSame('List all pets', $ops['get']->summary);

$responseSchema = $ops['get']->responses[200]->content['application/json']->schema;
$this->assertSame('array', $responseSchema->type);

Swagger 2.0

// Load schema
$json = json_decode(file_get_contents(__DIR__ . '/../../spec/petstore-swagger.json'));

// Import and validate
$schema = SwaggerSchema::import($json);

// Access data through PHP classes
$this->assertSame('Swagger Petstore', $schema->info->title);