Fastapi: [FEATURE] Swagger Codegen Like Project Generation

Created on 3 May 2019  Â·  15Comments  Â·  Source: tiangolo/fastapi

Rapid Application Development for the Enterprise
I've recently started using FastAPI and admire the framework. Great work. I typically work in enterprises that may have dozens of HTTP APIs. The current swagger code gen tool, although not perfect, is a great tool for bootstrapping applications. The FastAPI cookie cutter template is great, however, it doesn't provide the kind of scaffolding abilities large teams with numerous APIs need for rapid prototyping, especially in scenarios where an SSO is involved and the need is for a resource server only.

Describe the solution you'd like
A scaffolder similar to swagger code gen that takes an OpenAPI spec as an input and outputs a scaffolded application. Similar to the Flask code gen template, the output would include basic project setup, model generation, and route stubs.

I am willing to contribute to this effort. I think most of the pieces are in place in the current framework and this is a matter of developing the tooling. The framework already handles the OpenAPI abstraction needed as the foundation for a code generator.

answered

Most helpful comment

I have created a new code generator for fastapi.
fastapi-code-generator can create main.py and models.py from an OpenAPI file.

Great! Can I tweet about that?

All 15 comments

Thanks, that's a very interesting idea.

I imagine it would depend on Jinja2 as a dependency, so I guess it would be a good idea for an additional/external package, to not enforce Jinja2 as a dependency of FastAPI itself.

Do you have any other ideas/insights about how to approach it?

I think it could use Jinja2. I agree that it would probably be best to be a separate package. There are other tools available, such as swagger-codegen itself and Yeoman generator that could potentially be used to provide the base scaffolding tooling, however, FastApi already has the needed abstraction and utilizing one of those tools might add a lot of overhead for development and for users.

At a high level, I think this should be limited to scaffolding the models, routes, unit tests, and possibly Docker setup. It would be up to the users to implement their preferred middle tier, which will allow the generator to be agnostic of things like SQLAlchemy or other ORMs/datastore providers.

When?

Seconded, this would be a very useful feature. Currently we go from code -> docs, sometimes I work with other teams and we agree on an interface via a yaml, which I then want to use to automatically generate the functions to make sure our code doesn't differ from the spec.

@pawarren For what it's worth, couldn't you confirm the code didn't differ from the spec by just validating the FastAPI-generated schema matched the agreed-upon schema for any implemented routes? Obviously requires you to manually generate some boilerplate that could theoretically be automated, but at least it should be possible to validate agreement on the spec.

(To be clear, I think it would be great to have a FastAPI server generator, but it's a nontrivial amount of work to implement. So I wanted to provide a potential workaround since it may be a while before such a server generator exists.)

connexion is a framework that uses the openapi spec to dispatch requests to an underlying framework (currently supports Flask and aiohttp).

Do you think that providing a connexion driver for fastapi can be a reasonable solution?

Here's the aiohttp one. https://github.com/zalando/connexion/blob/master/connexion/apis/aiohttp_api.py

What I was envisioning with this feature was more for bootstrapping a
project. An example use case would be a "lift and shift" type project where
you want to expose an existing DB or segment of an existing DB as an API.
That's a typical task when breaking up monoliths. In this case, the models
could be easily defined ahead of time and most of the code is boilerplate
CRUD.

I've used Connexion and found it to be difficult to maintain, since the
OpenApi spec is not auto generated from the API itself.

On Wed, Jun 10, 2020, 4:32 PM Roberto Polli notifications@github.com
wrote:

connexion https://github.com/zalando/connexion is a framework that uses
the openapi spec to dispatch requests to an underlying framework (currently
supports Flask and aiohttp).

Do you think that providing a connexion driver for fastapi can be a
reasonable solution?

Here's the aiohttp one.
https://github.com/zalando/connexion/blob/master/connexion/apis/aiohttp_api.py

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/tiangolo/fastapi/issues/197#issuecomment-642279044,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/ADDPDIFT5HJ4TWV2EGNZO4DRV73V3ANCNFSM4HKKVDOQ
.

If your goal is to generate a stub from the spec, probably it can be achieved adding a FastAPI class here https://github.com/swagger-api/swagger-codegen-generators/tree/master/src/main/java/io/swagger/codegen/v3/generators/python

and reusing part of the moustache templates.

The connexion goal is to enforce a design-driven API implementation, so that's what I expect from that framework :)

Sure, I just figured that since FastApi can already convert models into an OpenAPI spec and there are already well maintained example applications, the inverse is possible and it wouldn't have to be written in Java. I do agree with @dmontagu that this is a non-trivial amount of work.

I also think there is a difference between enforcing design-driven API implementation like connexion and bootstrapping a project. I was approaching this from the standpoint of running something to get up and running quickly or for creating POCs.

This issue has been open for a year, and since I created it, I think it's time I step up and work on this myself.

@jamescrowley321
I create and use a fastapi generator for private projects.
I can't open now, because the code generator is optimized for my project.

I must remove some business context in the generator to publish it.
If you and someone want it then, I will do it and publish this weekend or next week.
Also, I develop and use datamodel-code-generator to generate the fast api code from openapi.
The generator can generate a pydantic model and type-hint of arguments from a schema.
I believe I can create a fast-code-generator soon.

I have created a new code generator for fastapi.
fastapi-code-generator can create main.py and models.py from an OpenAPI file.
The custom template is supported which is written in jinja2.
It means you can create your special main.py or other files.

This project is an experimental phase.
But, It's a good start pint.

I have created a new code generator for fastapi.
fastapi-code-generator can create main.py and models.py from an OpenAPI file.

Great! Can I tweet about that?

@ioggstream
Yes, of course :smile:

Awesome @koxudaxi ! :rocket: :nerd_face: :tada:

Thanks for building it! :gift:

I think that solves this need/issue :smile: :cake:

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Was this page helpful?
0 / 5 - 0 ratings