Description
Does anyone know a library that can consume a swagger file and provide nice interface for interacting with the API (something similar what suds did for SOAP) ? I'm particularly after auto-generated Pydantic definitions of swagger request/response objects.
I know this is not directly FastAPI related, but since all people here use FastAPI to create awesome APIs I think there is good chance you use similarly-awesome tools to consume those APIs.
Thanks in advance,
Zaar
I'm for it too, as stated in https://github.com/tiangolo/fastapi/issues/71
In the meantime, check this out. It might be what you looking for.
https://github.com/szczepanmasny/vue-crud
Simple CRUD demo
CRM demo
Credentials with limited privileges (readonly):
Login: [email protected]
Pass: ajSGenC0
pd: this is mostly frontend. it is needed anyways.
Maybe you're talking about
REQUESTS
http://docs.python-requests.org/en/master/user/quickstart/
No, I'm not talking about requests LOL :)
I'm talking about something like bravado, but with support for:
That's a very interesting idea. In fact, I think is a good idea for a new package.
Bravado seems cool, although as it has to infer and create new methods live from a schema, it doesn't have code generation, so, it can't have completion or type checking.
I think something very interesting to build would be a package that can read an OpenAPI, generate Pydantic classes for the JSON Schemas and generate requests code for the path operations. That would be great.
There are already some code generation tools, but I think they are wrote in Java or something, and the resulting Python code is too verbose. It would be nice to have something that generates these things automatically, and that the generated code is as clean and maintainable as if it was written by hand.
Let's see if someone else finds a tool like that (or gets inspiration from this issue to build one :grin: ).
@tiangolo
Would you commit autogenerated code to git? Or just generate it in dev / docker build in such place that code can import it?
From your experience with Pydantic, do you think implementing JSON Schemas to Pydantic classes conversion will be a straight-forward task?
I guess Path->Python code can be borrowed/inspired by what Bravado does.
P.S. Indeed it will be a new package :)
Would you commit [...]
If the code is as simple enough or comparable to what a developer would write, yes.
For example, I wouldn't add compiled/minified JS code to git. If it was a client that has type declarations, functions, etc, that the whole team can re-use, I would probably commit it. And when the API changes, the client could change, but the changes would be minimal, only in the sections that reflect those changes. But if the client was a binary, or minified JS, the whole thing would change, I wouldn't commit that. But here we're talking about Python, so, probably yes.
From your experience with Pydantic [...]
I think most of the JSON Schema cases (the simplest cases) should be more or less straightforward, the most complex/esoteric cases could be difficult or problematic, but I wouldn't expect a lot of those.
I guess Path->Python code can be borrowed/inspired by what Bravado does.
Yep, most probably yes. The only/main part missing would be actually generating the code.
The only/main part missing would be actually generating the code.
Any particular code-generation tools that you can think of here?
Related: https://github.com/Yelp/bravado/issues/306#issuecomment-485255089
Related!! https://github.com/samuelcolvin/pydantic/issues/643
The above is the first great step.
@tiangolo @haizaar While openapi-generator is written in Java, the vast majority of the code generation work is handled by the mustache templates; given the number of existing openapi-generator python client generators, I would expect a pydantic-models/fastapi-friendly generator could be created with minimal effort -- in particular, I suspect it would require zero changes to the java code if all you wanted was to generate pydantic models from an openapi spec. And given that you can specify templates to use at generation time, this would mean it would be usable against the existing release of openapi-generator. (It also wouldn't surprise me if an existing generator could be fully adapted into a full-fledged pydantic+requests client with only changes to the template files.)
While I agree that many of the existing openapi-generator client libraries are currently rather verbose (I haven't looked specifically at the python ones, but the description fits the ones I have seen), I think that is more a reflection of the implementation than of openapi-generator's capabilities. I am currently using a heavily streamlined client with my FastAPI server (in a non-python language) which was produced only by changing the templates of one of the existing clients.
In general, I would expect an approach leveraging openapi-generator to require substantially less initial effort and on-going maintenance. Would this be of interest? I would be happy to put some effort into it, but I would rather not if there is a separate serious effort to build this feature directly into pydantic/fastapi (samuelcolvin/pydantic#643)
@tiangolo @haizaar @dmontagu
I feel the code generation is exciting.
However, I think it's essential to decide what do we create FastAPI controller template before implement.
For example, Java server template has interfaces.
It's mean we can re-generate code And the implementation will be not broken.
Another case, Flask has "connexion" that improve relation flask and openapi.
If we want a simple server template(like a tutorial), then we can create a code generator right now :)
@dmontagu
I agree. We can save time by openapi-generator. It's a good idea.
I think Pydantic should be converted from a few types of file.
Very interesting insights @dmontagu and @koxudaxi !
Also, let me copy some my comment in that PR here:
I started a local experiment to generate Pydantic models from JSON Schema, using Jinja2 templates, and used in a recent project. I'm still not generating models that reference other models.
But I realized that probably a better approach would be to have two separated features (or packages if not suitable for Pydantic?):
create_model, generate the equivalent Python code as a text string e.g.:from pydantic import BaseModel, Schema
class SomeDynamicModel(BaseModel):
some_str_field: str
some_int_field: int = Schema(..., title="Some title")
The two features combined would result in being able to generate Pydantic models from JSON Schema.
But the separated components could be extended to, e.g.:
I'm just brainstorming here, but some of these ideas might be useful in case someone ( :wink: @koxudaxi ) implements it before me...
@haizaar @tiangolo @koxudaxi
I created a client generator mostly leveraging openapi-generator here. All models are pydantic BaseModel subclasses, and basically everything is strictly mypy- and flake8-compliant (apis, models, and client). I also threw in an auto-refreshing OAuth2.0 password flow extension to the client to show how easy it is to extend the generated client (not tested 馃槵).
You can use it to generate a client for your own API by just pointing it at your openapi.json url -- see the README.md for an example. So far I've only just tested it against my own API conventions, so it's possible it is obviously broken for some patterns (I'm not sure it could handle returning List[List[List[List[int]]]] right now, for example).
I haven't been able to test it much yet (but hopefully will be stress testing it in the next few days..), and so it probably has plenty of bugs still, but it's a start!
Let me know if you have any thoughts.
@dmontagu
Thank you for the notification.
Great work!
I want to create code-generator for a client. but, I have not started to develop it yet :(
I'm developing datamodel-code-generator which create pydantic models source code from openapi file.
datamodel-code-generator will support json-schema and GraphQL as input files.
Next, I will create server/client code-generator for fastapi :)
I think @dmontagu 's fastapi_client and @koxudaxi 's datamodel-code-generator probably solve this use case, right @haizaar ? May we close this issue?
@tiangolo yeap, although I'm not involved with FastAPI anymore (changed hats) but briefly looking at both fastapi_client and datamodel-code-generator I like them both.
/CC @bondib @motinani @worroc
Most helpful comment
That's a very interesting idea. In fact, I think is a good idea for a new package.
Bravado seems cool, although as it has to infer and create new methods live from a schema, it doesn't have code generation, so, it can't have completion or type checking.
I think something very interesting to build would be a package that can read an OpenAPI, generate Pydantic classes for the JSON Schemas and generate requests code for the path operations. That would be great.
There are already some code generation tools, but I think they are wrote in Java or something, and the resulting Python code is too verbose. It would be nice to have something that generates these things automatically, and that the generated code is as clean and maintainable as if it was written by hand.
Let's see if someone else finds a tool like that (or gets inspiration from this issue to build one :grin: ).