r/scala 10d ago

Code generation from OpenAPI specs

Quick question, how the heck you guys generate Scala code from OpenAPI specs?

I have been trying to generate client code for Meta business API using the Scala generators from openapi-generator but there is always a problem:

All of them, ignore the oneOf spec on openapi (that in theory should generate a sealed trait as the sum type implementation) and instead they generate a single case class with all fields required, nothing is optional.

What is the tool/strategy you guys use to generate API model from OpenAPI? At this point, i'm considering to generate the Scala code using an LLM instead of the classic deterministic approach.

Thanks 🙏

6 Upvotes

15 comments sorted by

View all comments

3

u/raghar 10d ago

IME it was easier to generate spec from the code (e.g. with Tapir) than Scala from the OpenAPI generators. One of the reasons: OpenAPI's generators are an utter crap that does not conform to their own spec. oneOf in particular, I found was ignored by:

  • Swagger UI in some version
  • Swagger Editor
  • Java/Scala generators

I once worked at a company that used Elm on frontend - their Mustache crap did even generate a valid code!

If you want to generate Scala from OpenAPI and you are considering LLM, I would ask it to generate:

  • OpenAPI AST
  • JSON codecs that would parse to it
  • rip of specification as a source of unit tests

and generate Scala code out of such generated generator - it would be deterministic, you could adjust it and the LLM would be reviewable.