aj-foster

aj-foster

OpenAPI Generator - Generate client code from an OpanAPI description

Background

OpenAPI is a standard for documenting API resources. Libraries like Spex allow you to generate OpenAPI descriptions of your endpoints to share with consumers. Then, the OpenAPI community provides a series of code generators that take the API descriptions and create client code (for example, one function per API endpoint). They even support Elixir.

However, I was not satisfied with the code generation options available for Elixir. I craved more flexibility in the outputted code, and better ergonomics of the resulting libraries. Being written in Elixir would also be a bonus.

So, I created OpenAPI Generator for Elixir. It accepts an existing OpenAPI description and generates client code for the API endpoints.

What does it do?

After installing the project as a development-only dependency and adding your configuration, it provides a task mix api.gen which accepts an OpenAPI description document and writes formatted code inside of your project.

The generator aims to be highly configurable, allowing you to repeatably rename, merge, and ignore parts of the OpenAPI description. This way you can create more ergonomic client code regardless of any weird parts of the original API description. For example, GitHub’s OpenAPI 3.0 description had to make use of NullableRepository and other repetitive schemas that would only confuse users of the client code. Such a schema can easily be merged into the Repository or FullRepository schemas. (Also, those two schemas could be combined into two typespecs with a single struct definition.)

With this kind of careful configuration, client code maintainers can create libraries that have the ergonomics of a hand-crafted library while also keeping the maintainability of generated code. GitHub, as an example, has too many API endpoints to maintain by hand — but we shouldn’t have to sacrifice usability for the sake of automated code generation.

Learn more

Check out the repository and join the discussions tab with thoughts and feedback.

Most Liked

aj-foster

aj-foster

I’m pleased to share that I will be discussing this library at ElixirConf US 2023 in a talk titled Filling the Gaps in the Ecosystem. The initial schedule is below (subject to change):

Date: Wednesday, 6 September 2023
Time: 2:15pm ET
Location: Palm AB

Would love to see and chat with anyone who uses this project. And if there’s anything you’d like to hear about in the conference talk, please let me know!

11
Post #7
ityonemo

ityonemo

I highly disrecommend going the codegen route on this,my experience is it becomes extremely finicky to get deployment and code control right on this, tbh this is a strong case for macros. Full disclosure, I was planning on doing an OpenAPI client compiler immediately after I finish my server router compiler, if you want to collaborate dm me.

maartenvanvliet

maartenvanvliet

I’ve worked on a Stripe SDK (Stripe — Striped v0.5.0) though the code is now merged with the StripityStripe library. It generates the code from the api spec at compile time.

Also found that it is finicky, all public open api schemas seem to do things a little differently if you want to generate an idiomatic sdk. E.g Stripe doesn’t use the tags functionality of the spec which would have been useful. So, the customisability your library has is a big plus here.

Where Next?

Popular in Announcing Top

asiniy
Hey there! I wrote a download elixir package which does exactly what its name about - an easy way to download files. I saw solutions ab...
New
jakub-zawislak
Hi everyone, I’m coming from the Symfony (PHP) framework. I like Phoenix, but it has a one thing that was build much better in the Symfo...
New
ostinelli
Let’s write a database! Well not really, but I think it’s a little sad that there doesn’t seem to be a simple in-memory distributed KV da...
New
josevalim
EDIT: since Ecto 3.0 final version is out, this post was amended to use the final versions in the instructions below. Hi everyone, We a...
New
ahamez
Hi everyone, I’ve been working on this protobuf library for 3 years. We use it in the company I work for, EasyMile, to communicate with ...
New
hpopp
After just over two years in development, this latest version of Pigeon is what I finally consider done in regards to my original vision ...
New
josevalim
Hello everyone, We have just released NimbleCSV which is a small and fast CSV parsing library for Elixir. It allows developers to define...
New
kevinlang
Hey all, We have made an Ecto3 Adapter for SQLite3, ecto_sqlite3! We have successfully on-boarded the full suite of integration tests (...
New
wfgilman
I’ve cleaned up and open sourced three financial libraries I was using for my company. They are bindings for the APIs of these three comp...
New
pkrawat1
Hey guyz We at @aviabird are working on a payment library in elixir/phoenix. We are targeting March 2018 to add 56 Gateways to it. Have...
New

Other popular topics Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement