openApiPetStore static method
Creates an API definition from OpenAPI specification.
Implementation
static ApiDefinition openApiPetStore() {
// Sample OpenAPI spec for JSONPlaceholder API
final openApiSpec = {
"openapi": "3.0.0",
"info": {
"title": "JSONPlaceholder API",
"description": "A simple REST API for testing and prototyping (JSON format example)",
"version": "1.0.0"
},
"servers": [
{
"url": "https://jsonplaceholder.typicode.com"
}
],
"paths": {
"/posts": {
"get": {
"tags": ["posts", "read"],
"summary": "Get All Posts",
"description": "Retrieve all posts",
"operationId": "getAllPosts",
"parameters": [
{
"name": "userId",
"in": "query",
"description": "Filter by user ID",
"required": false,
"schema": {
"type": "integer"
}
},
{
"name": "_limit",
"in": "query",
"description": "Maximum number of posts to return",
"required": false,
"schema": {
"type": "integer"
}
},
{
"name": "_start",
"in": "query",
"description": "Number of results to skip",
"required": false,
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"userId": {"type": "integer"},
"id": {"type": "integer"},
"title": {"type": "string"},
"body": {"type": "string"}
}
}
}
}
}
}
}
},
"post": {
"tags": ["posts", "write"],
"summary": "Create Post",
"description": "Create a new post",
"operationId": "createPost",
"requestBody": {
"description": "Post object to be created",
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"title": {"type": "string"},
"body": {"type": "string"},
"userId": {"type": "integer"}
}
}
}
}
},
"responses": {
"201": {
"description": "Post created successfully",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"id": {"type": "integer"},
"title": {"type": "string"},
"body": {"type": "string"},
"userId": {"type": "integer"}
}
}
}
}
}
}
}
},
"/posts/{id}": {
"get": {
"tags": ["posts", "read"],
"summary": "Get Post by ID",
"description": "Retrieve a specific post",
"operationId": "getPostById",
"parameters": [
{
"name": "id",
"in": "path",
"description": "Post ID",
"required": true,
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"userId": {"type": "integer"},
"id": {"type": "integer"},
"title": {"type": "string"},
"body": {"type": "string"}
}
}
}
}
},
"404": {
"description": "Post not found"
}
}
},
"put": {
"tags": ["posts", "write"],
"summary": "Update Post",
"description": "Update an existing post",
"operationId": "updatePost",
"parameters": [
{
"name": "id",
"in": "path",
"description": "Post ID",
"required": true,
"schema": {
"type": "integer"
}
}
],
"requestBody": {
"description": "Post object to be updated",
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"title": {"type": "string"},
"body": {"type": "string"},
"userId": {"type": "integer"}
}
}
}
}
},
"responses": {
"200": {
"description": "Post updated successfully",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"userId": {"type": "integer"},
"id": {"type": "integer"},
"title": {"type": "string"},
"body": {"type": "string"}
}
}
}
}
}
}
},
"delete": {
"tags": ["posts", "write"],
"summary": "Delete Post",
"description": "Delete a post",
"operationId": "deletePost",
"parameters": [
{
"name": "id",
"in": "path",
"description": "Post ID",
"required": true,
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"description": "Post deleted successfully"
}
}
}
},
"/users": {
"get": {
"tags": ["users", "read"],
"summary": "Get All Users",
"description": "Retrieve all users",
"operationId": "getAllUsers",
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
"username": {"type": "string"},
"email": {"type": "string"},
"address": {"type": "object"},
"phone": {"type": "string"},
"website": {"type": "string"},
"company": {"type": "object"}
}
}
}
}
}
}
}
}
},
"/users/{id}": {
"get": {
"tags": ["users", "read"],
"summary": "Get User by ID",
"description": "Retrieve a specific user",
"operationId": "getUserById",
"parameters": [
{
"name": "id",
"in": "path",
"description": "User ID",
"required": true,
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"id": {"type": "integer"},
"name": {"type": "string"},
"username": {"type": "string"},
"email": {"type": "string"},
"address": {"type": "object"},
"phone": {"type": "string"},
"website": {"type": "string"},
"company": {"type": "object"}
}
}
}
}
},
"404": {
"description": "User not found"
}
}
}
},
"/comments": {
"get": {
"tags": ["comments", "read"],
"summary": "Get Comments",
"description": "Retrieve comments",
"operationId": "getComments",
"parameters": [
{
"name": "postId",
"in": "query",
"description": "Filter by post ID",
"required": false,
"schema": {
"type": "integer"
}
},
{
"name": "_limit",
"in": "query",
"description": "Number of comments to return",
"required": false,
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"description": "Successful response",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"postId": {"type": "integer"},
"id": {"type": "integer"},
"name": {"type": "string"},
"email": {"type": "string"},
"body": {"type": "string"}
}
}
}
}
}
}
}
}
}
}
};
// Convert using OpenApiLoader
return OpenApiLoader.fromMap(openApiSpec) ?? _fallbackPetStoreApi();
}