openapi: 3.1.0
info:
  title: Items API
  version: 1.0.0
  description: Small CRUD skeleton for a single items resource.
servers:
  - url: http://127.0.0.1:8012
paths:
  /items:
    get:
      operationId: listItems
      summary: List items
      responses:
        '200':
          description: All items
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Item'
              example:
                - id: 1
                  name: widget
                  quantity: 3
        '500':
          $ref: '#/components/responses/ServerError'
    post:
      operationId: createItem
      summary: Create an item
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ItemCreate'
            example:
              name: widget
              quantity: 3
      responses:
        '201':
          description: Created item
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Item'
        '400':
          $ref: '#/components/responses/BadRequest'
        '409':
          $ref: '#/components/responses/Conflict'
        '422':
          $ref: '#/components/responses/Unprocessable'
        '500':
          $ref: '#/components/responses/ServerError'
  /items/{item_id}:
    parameters:
      - name: item_id
        in: path
        required: true
        schema:
          type: integer
    get:
      operationId: getItem
      summary: Get one item
      responses:
        '200':
          description: The item
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Item'
        '404':
          $ref: '#/components/responses/NotFound'
        '500':
          $ref: '#/components/responses/ServerError'
    patch:
      operationId: updateItem
      summary: Update an item
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/ItemUpdate'
            example:
              quantity: 5
      responses:
        '200':
          description: Updated item
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Item'
        '400':
          $ref: '#/components/responses/BadRequest'
        '404':
          $ref: '#/components/responses/NotFound'
        '409':
          $ref: '#/components/responses/Conflict'
        '422':
          $ref: '#/components/responses/Unprocessable'
        '500':
          $ref: '#/components/responses/ServerError'
    delete:
      operationId: deleteItem
      summary: Delete an item
      responses:
        '204':
          description: Deleted
        '404':
          $ref: '#/components/responses/NotFound'
        '500':
          $ref: '#/components/responses/ServerError'
components:
  schemas:
    Item:
      type: object
      required: [id, name, quantity]
      properties:
        id:
          type: integer
          example: 1
        name:
          type: string
          example: widget
        quantity:
          type: integer
          example: 3
    ItemCreate:
      type: object
      required: [name, quantity]
      properties:
        name:
          type: string
          minLength: 1
          example: widget
        quantity:
          type: integer
          minimum: 0
          example: 3
    ItemUpdate:
      type: object
      properties:
        name:
          type: string
          minLength: 1
          example: widget
        quantity:
          type: integer
          minimum: 0
          example: 5
    Error:
      type: object
      required: [code, message]
      properties:
        code:
          type: string
          example: not_found
        message:
          type: string
          example: Item 42 does not exist.
        details:
          type: object
          additionalProperties: true
  responses:
    BadRequest:
      description: Malformed request
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            code: bad_request
            message: Request body is not valid JSON.
    NotFound:
      description: Resource does not exist
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            code: not_found
            message: Item 42 does not exist.
    Conflict:
      description: Name already in use
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            code: conflict
            message: An item named widget already exists.
    Unprocessable:
      description: Validation failed
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            code: validation_error
            message: quantity must be 0 or greater.
    ServerError:
      description: Unexpected server error
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Error'
          example:
            code: server_error
            message: Something went wrong on the server.
