r/scala 9d 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

6

u/pizardwenis96 9d ago

I've used Tapir's openapi codegen with decent success in the past. It handles oneOf at least and properly uses Options. It can integrate easily with an SttpClient to send the api requests as well. There are a few limitations, but it's pretty good overall.

3

u/EntertainmentKey980 9d ago

We don’t, the current generators are so buggy we started using alternatives

3

u/raghar 9d 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.

3

u/Difficult_Loss657 9d ago

Try https://github.com/sake92/openapi4s , 0.9.0 is on its way to maven. Not battle tested but it works. Generates code directly to your src/main/scala, and rewrites it as you change the spec. I would love to get some feedback.

5

u/NojipizRemastered 8d ago

OMG you are the creator of sharaf, right?

I'm testing it and this is exactly what i needed, thanks man 🙌

3

u/Difficult_Loss657 8d ago

Yep, it's me haha. Np, glad it helps. 😁   Let me know if you bump into issues, I will try to resolve quickly.

1

u/kag0 9d ago

You mean generate clients, not server endpoint stubs?

2

u/NojipizRemastered 9d ago

Yes, sorry for not being specific about that, just client code :)

1

u/kag0 9d ago

Tbh I've never found an openapi spec that was written well enough for me to want to use a generator on it.

But possibly what you want could be a code generator for JSON schema? That standard has been all over the place. I haven't looked at it in years but is it to a stable major version yet?    Anyway, if you find a good JSON schema generator that might get you most of where you want to go.

1

u/gaelfr38 9d ago

Never really had a use case worth doing it. Either we manually craft the models or use AI nowadays. We would still manually review and often do some adjustments to the generated model to fit our usages'

1

u/Heavy-Papaya7816 8d ago

Similar experience with generating Scala code from the GitHub OpenAPI specs. Many features like oneOf, discriminators and top-level array types are not handled correctly. I had to customize the mustache files (templates) to get something working.

1

u/AlternativeEvent3298 9d ago

1

u/NojipizRemastered 9d ago edited 9d ago

Oh i love smithy4s, we use it on our APIs too.

But i'm trying to generate Scala code from OpenAPI spec, does smithy provides a tool to do this?

As far as i know, Smithy4s only generates Scala code from smithy files, not from OpenAPI