Using the following as an example of how to define an OpenAPI v3 data model.
code
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
use sixlabs\LaravelOpenAPI\Type;
use sixlabs\LaravelOpenAPI\PropertyType;
class UserResource extends JsonResource
{
public function toArray($request): array
{
return [
'name' => $this->name,
'email' => $this->email,
];
}
public function toSpec(): array
{
return Type::object()
->property('name', PropertyType::string())
->property('email', PropertyType::string())
->property('age', PropertyType::integer())
->property('is_active', PropertyType::boolean())
->property('rating', PropertyType::number())
->property('favorite_colors', PropertyType::array()->items(PropertyType::string()))
->property(
'profile',
Type::object()
->property('bio', PropertyType::string())
->property('profile_picture', PropertyType::string())
);
}
}
Can you provide a code example that showcases different aspects of defining OpenAPI v3 models. You should use PHP 8.1 features and take inspiration from Laravel conventions. Please only provide the toSpec method in your example. Please label each example.
Include ONLY: all distinct variations and attributes for data models in the Open API v3 spec.
Exclude: Enums, Dictionaries, Hashmaps, Associative Arrays, oneOf, anyOf, allOf, not, Inheritance and Polymorphism
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Certainly! Below are several examples showcasing different aspects of defining OpenAPI v3 models using PHP 8.1 features and inspired by Laravel conventions. Each example will focus on a specific aspect of the OpenAPI v3 specification.
Example 1: Basic Object Model with String Properties
php
public function toSpec(): array
{
return Type::object()
->property('id', PropertyType::string())
->property('name', PropertyType::string())
->property('description', PropertyType::string());
}
Example 2: Object Model with Nested Objects
php
public function toSpec(): array
{
return Type::object()
->property('id', PropertyType::string())
->property('name', PropertyType::string())
->property('address', Type::object()
->property('street', PropertyType::string())
->property('city', PropertyType::string())
->property('zip', PropertyType::string()));
}
Example 3: Object Model with Array Properties
php
public function toSpec(): array
{
return Type::object()
->property('id', PropertyType::string())
->property('tags', PropertyType::array()->items(PropertyType::string()));
}
Example 4: Object Model with Integer Properties
php
public function toSpec(): array
{
return Type::object()
->property('id', PropertyType::string())
->property('age', PropertyType::integer());
}
Example 5: Object Model with Boolean Properties
php
public function toSpec(): array
{
return Type::object()
->property('id', PropertyType::string())
->property('is_active', PropertyType::boolean());
}
Example 6: Object Model with Number Properties
php
public function toSpec(): array
{
return Type::object()
->property('id', PropertyType::string())
->property('rating', PropertyType::number());
}
Example 7: Object Model with Multiple Types
php
public function toSpec(): array
{
return Type::object()
->property('id', PropertyType::string())
->property('name', PropertyType::string())
->property('price', PropertyType::number())
->property('is_available', PropertyType::boolean());
}
public function toSpec(): array
{
return Type::object()
->property('id', PropertyType::string())
->property('name', PropertyType::string())
->property('created_at', PropertyType::string()->format(PropertyType::dateTime()));
}
Example 10: Object Model with Write-Only Properties
php
public function toSpec(): array
{
return Type::object()
->property('id', PropertyType::string())
->property('name', PropertyType::string())
->property('password', PropertyType::string()->format(PropertyType::password()));
}
Each example demonstrates a different aspect of defining OpenAPI v3 models, such as nested objects, arrays, optional properties, and read-only or write-only properties. These examples can be adapted and expanded upon to fit the specific needs of your API documentation.