openapi: "3.0.0"
info:
  version: 1.0.0
  title: OpenApi Petstore
  license:
    name: MIT
servers:
        - url: http://localhost:9090/petstore/v1
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            format: int32
      responses:
        '200':
          description: An paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
    post:
      summary: Create a pet
      operationId: createPets
      tags:
        - pets
      responses:
        '201':
          description: Null response
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Pet:
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Pets:
      type: array
      items:
        $ref: "#/components/schemas/Pet"
    Error:
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string

OpenAPI To Ballerina

If you already have an OpenApi Specification(OAS)-3 document for your service(s), you can use that contract to generate a Ballerina source code. Currently, Ballerina supports two types of code generation. ‘gen-service’ generates a mock version of the Ballerina service for a provided OAS definition. ballerina openapi gen-service <module-name>:<service-name> <openapi-contract> ‘gen-client’ generates a Ballerina client endpoint for a provided OAS definition. ballerina openapi gen-client [<module>:]<client-name> <openapi-file> [-o=<outputFileName>]

openapi: "3.0.0"
info:
  version: 1.0.0
  title: OpenApi Petstore
  license:
    name: MIT
servers:
        - url: http://localhost:9090/petstore/v1
paths:
  /pets:
    get:
      summary: List all pets
      operationId: listPets
      tags:
        - pets
      parameters:
        - name: limit
          in: query
          description: How many items to return at one time (max 100)
          required: false
          schema:
            type: integer
            format: int32
      responses:
        '200':
          description: An paged array of pets
          headers:
            x-next:
              description: A link to the next page of responses
              schema:
                type: string
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
    post:
      summary: Create a pet
      operationId: createPets
      tags:
        - pets
      responses:
        '201':
          description: Null response
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
  /pets/{petId}:
    get:
      summary: Info for a specific pet
      operationId: showPetById
      tags:
        - pets
      parameters:
        - name: petId
          in: path
          required: true
          description: The id of the pet to retrieve
          schema:
            type: string
      responses:
        '200':
          description: Expected response to a valid request
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Pets"
        default:
          description: unexpected error
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Error"
components:
  schemas:
    Pet:
      required:
        - id
        - name
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
        tag:
          type: string
    Pets:
      type: array
      items:
        $ref: "#/components/schemas/Pet"
    Error:
      required:
        - code
        - message
      properties:
        code:
          type: integer
          format: int32
        message:
          type: string
# Run the following command in a Ballerina project to generate the service from OpenAPI contract.
$ ballerina openapi gen-service petStore:petService openapi_to_ballerina.yaml
Note: This is an Experimental tool ship under ballerina hence this will only support limited set of functionality.
Service generated successfully and the OpenApi contract is copied to petStore/resources. this location will be referenced throughout the ballerina project.
Following files were created.
src/
- petStore
-- petservice.bal
-- schema.bal
# Run the following command in a Ballerina project to generate the service client from OpenAPI contract.
$ ballerina openapi gen-client petservice_client openapi_to_ballerina.yaml
Note: This is an Experimental tool ship under ballerina hence this will only support limited set of functionality.
Client generated successfully.
Following files were created.
src/
- client
-- openapi_petstore.bal
-- schema.bal