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.

First Post!

dimitarvp

dimitarvp

I have taken a look, though not a super detailed one.

It did seem to me you only support httpoison? IMO a huge selling point would be the ability to choose which HTTP library you want supported, including with extra options a la Finch that also needs to know pool sizes and such.

I like the intermediate layer of abstraction, though I do wonder whether it might have a performance impact. Likely not.

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 #6
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

michalmuskala
Hello everybody. I have just released Jason - a new JSON library. You might be wondering, why do we need a new library? The primary foc...
New
versilov
Could not wait for the missing Elixir ML libraries to appear, so, I wrote one myself, taking https://github.com/sdwolfz/exlearn as a foun...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
Qqwy
TypeCheck: Fast and flexible runtime type-checking for your Elixir projects. Core ideas Type- and function specifications are const...
336 14713 100
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
martinthenth
Hello everybody :wave: Recently, some of my colleagues talked about database ids and uuids and their problems, and I remembered the pain...
New
scohen
Lexical Lexical is a next-generation language server for the Elixir programming language. Features Context aware code completion As-you...
New

Other popular topics Top

jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement