Longhorn PHP 2021
Daniel Abernathy
Dripping Springs, TX
"A broadly adopted industry standard for describing modern APIs."
A guide for developers on how to build or consume an API.
An agreement between all stakeholders on how an API's requests and responses should be structured.
Practical summary:
An OpenAPI document is a machine-readable description of an API's endpoints and the structure of the data in its requests and responses.
vs. API format specifications like JSON:API
OpenAPI
JSON:API
Documentation
Mock Server
Testing
Validation
(... and more)
{
"openapi": "3.0.0",
"info": {
"title": "Todo MVC",
"version": "",
"description": "A backend API for TodoMVC"
},
"servers": [],
"paths": {},
"components": {}
}
"/widgets": {
"get": {
"description": "Get all widgets"
"responses": {
"200": {
"description": "OK"
}
}
}
}
Paths describe your individual endpoints
They primarily contain "operations", which correspond to HTTP verbs
"/widgets/{widgetId}": {
"parameters": [
{
"schema": {
"type": "integer"
},
"name": "widgetId",
"in": "path",
"required": true
}
]
}
Paths can also contain parameters
"patch": {
"description": "Update a widget",
"responses": {
"200": {
"description": "OK"
}
},
"requestBody": {
"content": {
"application/json": {
"schema": {
"type": "object"
}
}
}
}
}
The most important parts of an operation are the request body and the responses.
Data in OpenAPI is described as JSON Schema.... ish
OpenAPI uses an "extended subset" of an old draft of JSON Schema.
ಠ_ಠ
That is, this used to be the case...
"schema": {
"type": "object",
"required": [
"name"
],
"properties": {
"name": {
"type": "string"
},
"description": {
"type": "string",
"nullable": true
}
}
}
"post": {
"requestBody": {
"content": {
"application/json": {
"schema": {
}
}
}
}
}
"post": {
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
}
}
}
}
}
}
The root level components property lets you store data that you can reuse throughout document
{
"components": {
"schema": {},
"responses": {},
"parameters": {},
"requestBodies": {}
}
}
The $ref keyword lets you point to an item in the components section from elsewhere in the document.
{
"schema": {
"type": "array",
"items": {
"$ref": "#/components/schemas/Widget"
}
}
}
git clone https://github.com/dabernathy89/openapi-workshop-2021.git
cd openapi-workshop-2021
git checkout start
composer install
touch database/database.sqlite
cp .env.example .env
env file - delete all `DB_*` settings
env file: `DB_CONNECTION=sqlite`
php artisan key:generate
php artisan migrate:fresh
cd public
php -S localhost:3100