• Stars
    star
    27
  • Rank 905,827 (Top 18 %)
  • Language
    Ada
  • License
    Apache License 2.0
  • Created about 7 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ada support for OpenAPI code generator

OpenAPI Ada Library

Alire Alire Build Status Test Status Coverage License GitLab Commits

OpenAPI Generator is a code generator that supports generation of API client libraries, server stubs and documentation automatically given an OpenAPI Spec.

The Ada client and server support has been integrated in OpenAPI Generator.

The OpenAPI Ada library is a small support library for the Ada code generator provided by OpenAPI Generator. The library provides support to serialize the data, make HTTP requests and support the OpenAPI Spec specific operations or types.

Version 0.7.0 - Aug 2023

  • Add support for OpenAPI Number and array of Number
  • Add support for Bearer API Key authorization
  • Update the openapi generator to version 7.0.1:
    • Fixed the number type that did not work consistently (float and double were not mapped correctly),
    • The consumes and produces now allow to use several mime types for a request or response,
    • The file and binary types were using an incorrect Ada mapping that was not supported by the Ada library,
    • Added x-ada-no-vector, x-ada-serialize-op extensions to better control the generated Ada code

List all versions

Build with Alire

OpenAPI Client

alr with openapi

For the HTTP connection, you can either use AWS or Curl and run one of the following commands:

alr with utilada_curl
alr with utilada_aws

OpenAPI Server

alr with openapi_server

For the server part, you must choose a servlet web container that will handle the requests. Two web server implementations are provided:

and you should run one of the following alr command depending on your choice:

alr with servletada_aws
alr with servletada_ews

Build and installation

The OpenAPI Ada library provides support for client and server. The client part has less constraints than the server part which needs more components. For both parts, before building this library, you may need to install the following projects:

If you also need to server part, you must also install the following components:

Then, to build OpenAPI Ada library, configure as follows:

./configure
make

And if you want the server part, configure and build with:

./configure --enable-server
make

For the installation, use the following command:

make install

The git repository comes with a pre-compiled OpenAPI Generator that will be installed in /usr/local/share/openapi-ada/openapi-generator-cli.jar. To help in launching the generator, a script is installed in /usr/local/bin/openapi-generator. You must have a Java JRE installed to be able to run the generator.

Docker

A docker container is available for those who want to try OpenAPI Ada without installing and building all required packages. To use the OpenAPI Ada docker container you can run the following commands:

sudo docker pull ciceron/openapi-ada

Using OpenAPI Ada

Generating the REST client from OpenAPI Spec

The command to generate an Ada REST client is the following:

  alr exec -- openapi-generate-client -i my-api.yaml \
       --additional-properties projectName=MyProject \
       --additional-properties openApiName=OpenAPI \
       --additional-properties httpSupport=Curl \
       --model-package MyProject.MyModule -o .

where my-api.yaml is the OpenAPI specification file that describes your API, MyProject is the name of the GNAT project to use, MyProject.MyModule is the name of the Ada package that will be the parent package of the generated Ada packages.

The generator will create several files in client/src/client with basically two packages: MyProject.MyModule.Models and MyProject.MyModule.Clients. The Models child package will contain the type definitions for the API operations describes in the YAML file and the Clients child package will contain the Client_Type tagged record with the API operations to invoke the REST API.

For example, if the YAML description file contains the following API operation:

openapi: 3.0.0
servers:
  - url: 'https://todo.vacs.fr/v1'
  - url: 'http://todo.vacs.fr/v1'
info:
  title: Todo API
  description: Todo API
  version: 1.0.0
  termsOfService: 'https://todo.vacs.fr/terms/'
  contact:
    email: [email protected]
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
tags:
  - name: tasks
    description: Tasks
paths:
  /:
    get:
      tags:
        - tasks
      summary: Redirect to the UI
      description: |
        Default operation to redirect to the UI index page.
      operationId: redirectTodos
      responses:
        '302':
          description: redirect to the UI page
  /todos:
    get:
      tags:
        - tasks
      summary: List the available tasks
      description: |
        The list of tasks can be filtered by their status.
      operationId: listTodos
      parameters:
        - name: status
          in: query
          description: Filters the tasks by their status
          required: false
          schema:
            type: string
            enum:
              - done
              - waiting
              - working
              - all
      responses:
        '200':
          description: successful operation
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/Todo'
        '400':
          description: Invalid status value
      security:
        - todo_auth:
            - 'read:todo'
security:
  - todo_auth: []
externalDocs:
  description: Find out more about Swagger
  url: 'http://swagger.io'
components:
  securitySchemes:
    todo_auth:
      type: oauth2
      flows:
        password:
          tokenUrl: 'http://localhost:8080/v1/oauth/token'
          scopes:
            'write:todo': Write a todo
            'read:todo': Read a todo
  schemas:
    Todo:
      type: object
      properties:
        id:
          type: integer
          format: int64
          description: The todo identifier
        title:
          type: string
          description: The todo title
        create_date:
          type: string
          format: date-time
          description: The todo creation date
        done_date:
          type: string
          format: date-time
          description: The todo resolution date
        status:
          type: string
          description: The todo state
          enum:
            - waiting
            - working
            - done
      required:
        - id
        - title
        - status
        - create_date
      example:
        id: 23
        title: Make the FOSDEM presentation
        description: password
        status: working
        create_date: '2017-12-24T00:00:00.000Z'
      xml:
        name: Todo

The generator will generate the following Ada code in the Models child package:

package Todos.Models is
   ...
   type Todo_Type is
     record
       Id : OpenAPI.Long;
       Title : OpenAPI.UString;
       Create_Date : OpenAPI.Datetime;
       Done_Date : OpenAPI.Nullable_Date;
       Status : OpenAPI.UString;
     end record;
     ...
end Todos.Models;

and the following code in the Clients child package:

package Todos.Clients is
   ...
   type Client_Type is new OpenAPI.Clients.Client_Type with null record;
   --  List the available tasks
   --  The list of tasks can be filtered by their status.
   procedure List_Todos
      (Client : in out Client_Type;
       Status : in OpenAPI.Nullable_UString;
       Result : out Todos.Models.Todo_Type_Vectors.Vector);

   --  Redirect to the UI
   --  Default operation to redirect to the UI index page.
   procedure Redirect_Todos
      (Client : in out Client_Type);
   ...
end Todos.Clients;

Initialization

The HTTP/REST support is provided by Ada Util and encapsulated by OpenAPI Ada. This support is either based on Curl or on AWS. The OpenAPI code generator uses Curl by default and this can be changed when running the openapi-generator tool and changing the following option:

       --additional-properties httpSupport=aws

If you want to use Curl, the initialization should use the following:

   Util.Http.Clients.Curl.Register;

But if you want to use AWS, you will initialize with:

   Util.Http.Clients.AWS.Register;

Curl may be easier to start with because AWS does not support HTTP DELETE operation except in some latest version.

After the initialization is done, you will declare a client instance to access the API operations:

   C : Todos.Clients.Client_Type;

The 'Client_Type' is the generated type that will implement the operations described in the OpenAPI description file.

And you should initialize the server base URI you want to connect to:

  C.Set_Server ("http://localhost:8080/v1");

At this stage, you can use the generated operation.

Calling an operation

Let's retrieve some todo list by calling the 'List_Todo' operation. This operation needs an integer as input parameter and returns a 'Pet_Type' object that contains all the pet information. You will first declare the pet instance as follows:

  List     : Todos.Models.Todo_Type_Vectors.Vector;

And then call the 'Get_Pet_By_Id' operation:

  C.List_Todos ((Is_Null => True, Value => <>), List);

At this stage, you can access the list of todos:

procedure Print (Todo : in Todos.Models.Todo_Type) is
begin
   Put (OpenAPI.Long'Image (Todo.Id));
   Set_Col (6);
   Put (OpenAPI.To_String (Todo.Status));
   Set_Col (15);
   Put (Ada.Calendar.Formatting.Image (Todo.Create_Date));
   Set_Col (40);
   if Todo.Done_Date.Is_Null then
      Put ("-");
   else
      Put (Ada.Calendar.Formatting.Image (Todo.Done_Date.Value));
   end if;
   Set_Col (60);
   Put (OpenAPI.To_String (Todo.Title));
   New_Line;
end Print;

  for T of List loop
     Print (T);
  end loop;

Documentation

The OpenAPI Ada sources as well as a wiki documentation is provided on:

Examples

More Repositories

1

ada-awa

Ada Web Application - Framework to build high performance secure web applications
Ada
93
star
2

ada-util

Ada Utility Library - Composing streams, processes, logs, serialization, encoders and more
Ada
69
star
3

sql-benchmark

Tool to make SQL benchmark on different drivers, languages and databases
Ada
31
star
4

ada-keystore

Ada Keystore - protect your sensitive data with secure storage
Ada
31
star
5

ada-ado

Ada Database Objects
Ada
25
star
6

ada-security

Ada Security - OAuth 2.0 client and server framework to secure web applications
Ada
22
star
7

ada-enet

Ada Embedded Network Stack
Ada
21
star
8

ada-wiki

Ada Wiki Engine - Wiki parser and renderer with several Wiki syntaxes
Ada
19
star
9

etherscope

Ethernet traffic monitor on a STM32F746 board
Ada
17
star
10

dynamo

Dynamo Ada Application Code Generator
Ada
17
star
11

ada-asf

Ada Server Faces - Web Server Faces JSR 252, JSR 314 and JSR 344
Ada
12
star
12

resource-embedder

ARE - Advanced Resource Embedder include files, scripts, images in Ada, C, Go binaries
Ada
10
star
13

ada-search

Ada
9
star
14

ada-bfd

Ada BFD is an Ada binding for the GNU Binutils BFD library. It allows to read binary ELF, COFF files by using the GNU BFD.
Ada
8
star
15

swagger-ada-todo

Simple todo list server with OpenAPI
Ada
8
star
16

ada-el

Ada EL - Expression Language Library (JSR245)
Ada
7
star
17

ada-servlet

Ada Servlet - Web Servlet Library following JSR 154, JSR 315
Ada
7
star
18

atlas

AWA Demonstrator
TSQL
7
star
19

ada-css

Ada parser for CSS files with CSS Object Model API
Ada
6
star
20

ada-lzma

Ada binding for liblzma compression library
Ada
6
star
21

mat

Memory Analysis Tool
Ada
6
star
22

ada-stemmer

Multi natural language stemmer with Snowball generator
Ada
4
star
23

stm32-ui

STM32 UI library and tools
Ada
4
star
24

ada-rest-api-server-benchmark

Benchmark for REST api on Ada servers
Ada
3
star
25

jason

Project and ticket management
Ada
3
star
26

bbox-ada-api

Ada Binding for the Bbox API
Ada
3
star
27

wi2wic

Wiki 2 Wiki Converter
Ada
2
star
28

hestia

Heat Controller
Ada
2
star
29

ada-mail

Mail tools
Ada
2
star
30

ada-libsecret

Ada Binding for the libsecret library
Ada
2
star
31

babel

Babel Backup
Ada
1
star
32

ada-awe

Automatically exported from code.google.com/p/ada-awe
Ada
1
star
33

helios

Helios Fast Reliable Monitoring Agent
Ada
1
star
34

awa-alire-index

1
star
35

xcra

Automatically exported from code.google.com/p/xcra
C++
1
star