vipes

vipes

Hi all. I’ve seen plenty of information about generating Swagger docs from a pre-existing Phoenix API. Has anyone come across a tool that has the ability to generate a Phoenix API from a Swagger / OpenAPI 3.0 specification? I’ve had a look at Swagger’s Codegen tool but it doesn’t support building server stubs for Elixir. Thank you :slight_smile:

Showing Posts 1 to 10

bryanhuntesl

bryanhuntesl

I’ve done quite a lot of research in this area - to the best of my knowledge no such tool exists. It would be very useful for migrating an implementation written in JavaScript, Scala, Python or whatever to Elixir.

Strikes me it wouldn’t be the hardest thing in the world to write. Parse openapi definition, generate the structs and stubs required by openapispex then fill in the blanks. @moxley might have some thoughts

vipes

vipes OP

Thanks @bryanhuntesl. I did think there wasn’t anything around, but thought it was worth posing the question.

moxley

moxley

I did some experiments around this kind of work a couple years ago, but didn’t get very far with it. The focus was on generating clients, and server stubs were an eventual goal.

gmile

gmile

For what it’s worth, there’s an Elixir “backend” in the openapi-generator, introduced back in 2017, and still alive – there’s a recent PR open from an early July 2022 by @halostatue)

I tried generating Elixir app from a OpenAPI spec provided in openapi-generator’s samples. Generation works. The generated code doesn’t look like Phoenix API though. I didn’t test it further, e.g. running an actual API server to see if it behaves as expected.

It’s not exactly an answer to generating a “Phoenix API”-compatible Elixir code, but better than nothing (I guess).

On a second view, scratch what I said. It’s a client code generation :see_no_evil:

Eiamnacken

Eiamnacken

To give some perspective. It is also difficult to keep this in sync with the openapi spec over time. Also, the generators get old very fast. From my experience from using this in java. We had more trouble with the generator. Instead, we built it manually and programmed unit tests to check that the implementation is in sync with the openapi spec.

And also the spec is quite complex as this is using all features json-schema has to offer.

halostatue

halostatue

Thanks for the mention. The opened client PR is about to merged, so that is a good thing.

I’d be happy to spend some time talking with someone on how OpenAPI generator could be extended to provide a server generator. There’s a few things noted in the documentation as “not compatible” in Elixir that make no sense to me, but that’s likely resolvable.

Even though the OpenAPI generator is written in one of my least favourite languages (Java), I found the codebase to be eminently approachable and entirely runnable in Docker so that I didn’t have to have a Java development environment set up.

reddy

reddy

It looks like the PR still hasn’t been merged, are the clients generated by the current release (not including your PR) usable/production ready or do you think it is better to wait for the PR to be merged before getting started?

halostatue

halostatue

The PR in question is the second of two, and the first was necessary to make the Elixir clients baseline usable again (certain things had drifted). This PR is more of an improvements PR that makes for better clients (the generated code looks more like what is expected, and requires less change when running mix format; the current released version will have more drift).

There are more changes that I would want to make based on the last several years of using Tesla and Elixir:

  1. Entirely remove the Tesla compile-time configuration, except for the adapter (and I’d probably make it easy to override that at runtime).
  2. Implement a behaviour and default implementation of request token authorization caching (use an optional con_cache dependency and detection of same).
  3. Look at other generators to see if unit tests using Mox, Tesla.Mock, or Bypass could be generated.
  4. Look over the way that OpenAPI Generator allows for the use of external templates (whether you have to provide a complete template set or can override individual templates, etc.) and improve the documentation on how that could be done on a per-project basis for Elixir, particularly documenting the extensions to the template functions that help with Elixir.
  5. Maybe add some combination template functions that cover common repeated cases (e.g., {[#atom}}{{#underscored}}}{{value}}{{/underscored}}{/atom}} could be replaced with {{#underscored_atom}}{{value}}{{/underscored_atom}} or something better.
  6. Maybe switch all the mustache templates for elixir to <%…%> pairs instead of {{…}} pairs because the latter really confuse things when tuples are involved (see an example of the latter in README.md.mustache).
  7. Make sure that the current restrictions that are listed on the Elixir clients are real. IIRC, there’s about 40% of the features of OpenAPI that are considered incompatible with Elixir; IMO that should be < 10%—but probably requires some explicit understanding of what those features should do, as well as coming up with good template implementations, etc.

Which is to say that, what’s there is good. The PR makes it even better. There’s a lot more work to be done. I suspect that if I do much more with it, I’ll end up as a reviewer on open Elixir tickets. :smiley:

ityonemo

ityonemo

I have an impl of this and I’m talking to my former boss to get it open sourced. If I can’t get that, I can provide tips if you want to imply your own.

ityonemo

ityonemo

Update:. The library is on its way. I’m currently refactoring the JSONschema stuff to be easier to debug

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews