chalice icon indicating copy to clipboard operation
chalice copied to clipboard

OpenAPI models from python typing

Open Sytten opened this issue 6 years ago • 9 comments

It would nice to be able to generate models from the Typing for Swagger/OpenAPI instead of the empty model currently used.

Sytten avatar Feb 05 '19 20:02 Sytten

Could you elaborate a little more on how you see this working? Some example code/swagger docs would be helpful. Thanks!

jamesls avatar Feb 07 '19 21:02 jamesls

Sure! Let's take a simple case:

class MyModel(NamedTuple):
    hello: str

@app.route("/")
def index() -> MyModel:
    return MyModel("world")

This could generate the following OpenAPI:

paths:
  /:
    get:
      responses:
        200:
          description: "successful operation"
          schema:
            $ref: "#/definitions/MyModel"
definitions:
  MyModel:
    type: "object"
    properties:
      hello:
        type: "string"

We should also use the same principle for the params and the body (if possible, but it would be harder since we don't have it as a param).

Maybe something easier for a first step would be to support the documentation as a docstring like what https://github.com/rochacbruno/flasgger does. We already support summary. We should also document this since I didn't see anything related to it.

Sytten avatar Feb 08 '19 02:02 Sytten

Ah I see, that's pretty interesting. Thanks for the suggestion, marking as a feature request.

jamesls avatar Feb 09 '19 00:02 jamesls

Is this already covered by something like https://pypi.org/project/apispec-chalice/ ?

dragon788 avatar Dec 02 '19 16:12 dragon788

@dragon788, no, that forces you to write a separate set of docs. To quote FastAPI's docs:

The way it works is that you write the definition of the schema using YAML format inside the docstring of each function handling a route.

But then, we have again the problem of having a micro-syntax, inside of a Python string (a big YAML).

The editor can't help much with that. And if we modify parameters or Marshmallow schemas and forget to also modify that YAML docstring, the generated schema would be obsolete.

ricky-sb avatar Mar 12 '20 22:03 ricky-sb

Hey, any update on this?

zygopleural avatar Nov 04 '21 11:11 zygopleural

Found an interesting project related to this issue: https://github.com/TestBoxLab/chalice-spec

shoutout to @jakemwood I'm excited to try it out!

knowsuchagency avatar Jan 23 '23 15:01 knowsuchagency

I wrote a simple gist adding a /docs endpoint to a chalice app using chalice-spec https://gist.github.com/knowsuchagency/8307980a51ee2d400f4d9271dc88942f

knowsuchagency avatar Jan 27 '23 17:01 knowsuchagency

Upvoting for this to be native support ! chalice-spec does not support everything in chalice, for example it's unknown if it works with blueprints

I wrote a simple gist adding a /docs endpoint to a chalice app using chalice-spec https://gist.github.com/knowsuchagency/8307980a51ee2d400f4d9271dc88942f

Mexflubber avatar Apr 09 '23 16:04 Mexflubber