Heya, have you run into other Kotlin/Java code gen...
# server
j
Heya, have you run into other Kotlin/Java code generators from OpenAPI than https://github.com/OpenAPITools/openapi-generator/ and https://github.com/swagger-api/swagger-codegen ? They both leave a lot to be desired on the generating model classes side (not to mention, say, clients) with quite buggy implementations compared to, say, OpenAPI Typescript generator.
s
Yes, I’ve used them but the generations is indeed quite bad. It’s practically unuseable from my experience 😞 I’ve only looked at KotlinX Serialization/Ktor though, and I’ve had to resort to writing custom templates in some cases. To for example generate only models, and write client by hand. I’ve considered writing a custom generator, but lack the time atm for it unless I can do it as part of my actual work.
j
I mean, I've tried all Java/Kotlin generators and for some basic cases they work. But in general the create uncompilable stuff or just drop some things. Even for models. I was wondering if there are some tools I've missed as I've also thought about creating my own tooling (which is quite a big ask for general case).
d
Http4k wrote our own generator (see Https://toolbox.http4k.org) for precisely that reason -the official template-based implementations leave a lot to be desired. It is quite tricky though TBF - the malleability of the spec (and the number of people who are using hand rolled illegal specs makes it even harder). 🙃
s
and the number of people who are using hand rolled illegal specs makes it even harder
💯
d
We decided on Kotlin poet for the generation - at least then you are quite assured that it compiles. Plus we test it.. 😉
1
j
Thanks, I'll check if your generator generates better (or, well, just working) code in our cases for models.
I was trying to find the actual generator, but is it so that it's only available as a web service like here as http4k cli tool says the following
-l, --location URL or file (will be uploaded for processing) of OpenApi specification (STRING?)
? I didn't get it to generate any model classes though and after putting my type into a path parameter, I got 400 response.
d
@Jarkko Miettinen yes - the source code of the generator isn't open source unfortunately. In terms of the test case - can you share the URL you were using and we can try to look at why it failed. If you get a 400, then what would be handy is to paste the content of the response (it should contain trace information which we use to determine why the generator didn't like the spec).
j
Oki, that's fine and also a point towards writing our own solution or changing the approach to API -> spec. Trace id is 9bef7a1fa03f58a8
d
not the trace id - the content of the message 🙂
for privacy reasons, the generator is entirely stateless 🙂
j
You mean my request? As that's all the http4k-cli gives out as a response message.
d
ah ok. That indicates that it couldn't even parse the input file. Did you upload a file or a URL?
j
I uploaded this file. Which gives me constant 400. I'd wager it's this oneOf many enums thing that I tried to be able to write descriptions for permission inline instead of having them in a separate description in the container object.
d
The response does contain the log BTW. I'll take a look when I get a chance 🙂
Looking at the spec, I can see that we definitely don't support it - oneof for object properties are one of the things that aren't supported by our generator. This is more of a general problem in Kotlin because you're into the realms of generating a type tree (sealed type hierarchy maybe?). It's much more of a natural fit for Typescript because you have proper union types.
j
Yeah, allOf maps nicely but for oneOf you’ll need a sealed type hierarchy. OpenAPITools don’t fail to generate, they just create non-sense 😀 . I was thinking that for a hand-rolled generator these could be supported but problem would be that if you A|B|C in one place and B|C p, B in the two places would be a different type. Which could still be okay for limited use as long as you’d generate some helpers. I didn’t notice the response, should’ve used directly the web tool with inspector
333 Views