openApiYamlExample static method

ApiDefinition openApiYamlExample()

Creates an API definition from YAML format OpenAPI specification.

Implementation

static ApiDefinition openApiYamlExample() {
  // Sample OpenAPI spec in YAML format (as string)
  const openApiYaml = '''
openapi: 3.0.0
info:
title: JSONPlaceholder API
description: A simple REST API for testing and prototyping (YAML 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
    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
    requestBody:
      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
    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
    parameters:
      - name: id
        in: path
        description: Post ID
        required: true
        schema:
          type: integer
    requestBody:
      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
    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
    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
    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
    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 YAML to ApiDefinition
  return OpenApiLoader.fromYamlString(openApiYaml, baseUrl: 'https://jsonplaceholder.typicode.com') ?? _fallbackBooksApi();
}