Skip to content

Modern C++ 20 compile time OpenAPI parser and code generator implementation

License

Notifications You must be signed in to change notification settings

talanton/openapipp

 
 

Repository files navigation

OpenApi++ : openapipp

This is a proof of concept, currently under active work to become the best OpenAPI implementation for C++.

It allows compile time C++ code generation for an OpenAPI configuration.

To compile the project and pull dependencies run

Get a free tipi by registering to tipi.build

tipi build . -t linux-cxx20

CppCon 2021 Talk

Featured in the CppCon 2021 Talks : Our Adventures With REST API in C++ : Making it Easy.

OpenApi JSON Compile Time client generation

Add the content of the petstore openapi.json to a C++ constexpr variable prefixed by _cx_json.

  static constexpr auto openapi_json = R"x( 
  {
    "openapi": "3.0.2",
    "info": {
      "title": "Swagger Petstore - OpenAPI 3.0",
      "description": "This is a sample Pet Store Server.",
      "termsOfService": "http://swagger.io/terms/",
      "contact": {
        "email": "apiteam@swagger.io"
      },
      "license": {
        "name": "Apache 2.0",
        "url": "http://www.apache.org/licenses/LICENSE-2.0.html"
      },
      "version": "1.0.6"
      
    }
    ...
    ...
    ...
    ...
  })x"_cx_json

  constexpr auto client = client_t<decltype(openapi_json)>{};
  using pre::cx::key_value_pair;

  auto reply = client.call_operation<"addPet">(std::make_tuple(
    key_value_pair<"id" , int>  {43},
    key_value_pair<"name" , std::string>  {"Haskell"s},
    key_value_pair<"photoUrls" , std::vector<std::string>>  { {"little_cat.png"s} }
  ));

Generate "C++ Typings" for autocompletion support

mp11::mp_rename<poor_person_metaclass::all_operations, mp11::mp_inherit> print_code{};

This will print the code to #include <> on a later build, that allows you then to use this syntax for the HTTP POST /Pet remote call:

auto second_reply = addPet(Pet{{43}, {"Haskell junior"s}, {{"some_cat.png"}} }); 

`

Libraries used in this work

About

Modern C++ 20 compile time OpenAPI parser and code generator implementation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 100.0%