nestjsx / crud

NestJs CRUD for RESTful APIs
https://github.com/nestjsx/crud/wiki
MIT License
4.01k stars 526 forks source link

Need recommendation: SDK generation from swagger #521

Open MrMaz opened 4 years ago

MrMaz commented 4 years ago

First of all, thank you for an amazing module!

We have run into an issue with trying to get a viable Javascript SDK generated from the swagger spec. It is very specific to the multiple signatures for getMany routes when alwaysPaginate: false (which is the default). The results in a oneOf response type definition.

We tried flipping alwaysPaginate on, but that causes a problem with the react-admin data provider we are using, because it does not support the alwaysPaginate option. Also, we would prefer to leave this setting off for performance reasons.

Does anyone have knowledge of an SDK generator (hopefully that generates Typescript) that correctly handles the oneOf definitions, or possibly some swagger config setting to get a generator to correctly map the responses to the correct type?

Thank you!

michaelyali commented 4 years ago

have you tried https://www.npmjs.com/package/nswag?

MrMaz commented 4 years ago

@zMotivat0r thanks for the reply!

I checked out nswag based on your advice 👍. That project is very impressive, but the tooling is pretty intimidating, especially being primarily on a Linux/Mac env here.

I have looked at autorest before, which I noticed that nswag uses, and was unable to use because it does not support the oneOf switch yet. I will dig deeper into nswag to see if they have overcome this with some abstractions.

Are you using nswag successfully to generate any SDKs for CRUD?

Thanks again!

humbertowoody commented 4 years ago

Hi @MrMaz !

Have you checked out https://github.com/swagger-api/swagger-codegen ?

I use it to generate SDKs for 5 languages, publish clients to its respective package managers and we've had no issues for the pagination so far (that i'm aware of). However, we had to tweak the OA3 generated file, mostly because multiple authentication methods and error-handling specifics, but you can use the auto-generated file as a starting point and give it some manual modifications each deploy. If you are just planning on exposing part of your API (say, some public facing endpoints), you are probably going to be better just writing the file itself so you can customize it further than this (amazing and awesome) project aims to in terms of Swagger auto-generation.

The particular project I linked have a public API (https://generator.swagger.io/) so you can integrate with GitHub Actions or your favorite CI/CD tooling, but you can run it locally to have extra configuration options! The generated clients include docs (for the specific language) and are ready to be published in the language-specific package manager! The project is written in Java and I personally use it for TypeScript, Python, Go, Java and Elixir and the final SDKs seem to be pretty easy to use and integrate (i am not fluent in all of them myself :c).

Hope it helps :)

MrMaz commented 4 years ago

Hey @humbertowoody !

Yes, we have actually used swagger-codegen quite a bit, but we have had limited success with OAS3 support when it comes to Typescript and some of the more advanced settings.

Are you having any luck generating valid SDK methods when you have a oneOf schema definition? This is the piece that is giving us the most trouble.

Many of the generated SDKs either switch this to an any type on the response, and then expect the implementing code to test and assign to the correct class/dto/interface depending on the signature.

I have read a lot about adding a discriminator to the spec and the response, but I am not confident enough that I know exactly what to do, or if that is even really supported without digging into the generator templates.

Thanks for your detailed response, I really appreciate it. :)