-
-
Notifications
You must be signed in to change notification settings - Fork 107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make it possible to register the same model definition multiple times in doc.definitions
#103
Comments
Hi there, About reuse from sanic import Sanic
from sanic.response import json
from sanic_openapi import doc, swagger_blueprint
app = Sanic(strict_slashes=True)
app.blueprint(swagger_blueprint)
class PaginationObject:
start = doc.Integer()
limit = doc.Integer()
class ListResourceAProducer:
pagination = doc.Object(PaginationObject)
foo = doc.String()
class ListResourceBProducer:
pagination = doc.Object(PaginationObject)
bar = doc.String()
@app.get("/resource_a")
@doc.summary("List all resource a.")
@doc.produces(ListResourceAProducer)
async def list_resource_a(request):
return json({"pagination": {"start": 0, "limit": 10}, "foo": "foo"})
@app.get("/resource_b")
@doc.summary("List all resource b.")
@doc.produces(ListResourceBProducer)
async def list_resource_b(request):
return json({"pagination": {"start": 0, "limit": 10}, "bar": "bar"})
if __name__ == "__main__":
app.run() Does this cover your need? |
Thanks for the quick response. My use-case is a bit more complicated, but it can be condensed into something like this code: class ConsumedModel:
foo = str
bar = str
@app.post("/foo")
@doc.consumes(doc.Object(ConsumedModel, object_name="FooConsumes"), content_type="application/json", location="body")
def foo(request: Request):
return "foo"
@app.post("/bar")
@doc.consumes(doc.Object(ConsumedModel, object_name="BarConsumes"), content_type="application/json", location="body")
def bar(request: Request):
return "bar" As you will see, in this case only the Back to the proposed solution: I can see that it might cause problems for users who declare different models with the same name in multiple modules for example, but using the fully qualified class name instead of |
Got your point, thanks for your case sharing. I also totally agree with your solution. Would you wanna submit a PR for it? |
Sure, i can submit a PR. According to the readme, Python 3.5 must be supported (no f-strings). Will it still be the case in the next release? |
In my personal opinion, drop python 3.5 support is totally acceptable since |
Please have a look the the pull request (#104). Thanks! |
Just a bit of clarification about why Sanic decided to drop Python 3.5 beginning with the 19.6 release... The decision was not because we had any syntax we particularly had in mind. In fact, there are still downloads of Sanic using Python 3.5. But, since we have an long-term support release (v 18.12), we decided to extend that support thru the sunseting of Python 3.5 in September 2020. Therefore, Sanic will continue to work on Python 3.5 as long as that release of Python is officially supported, albeit on the 18.12LTS. As for the decision why 19.6 is dropping it, the issue mainly has to do with the reliance upon third-party libraries needed before ASGI could be adopted. With that said, I would encourage this project to continue supporting 3.5 until the 18.12LTS is no longer supported UNLESS there is a specific need. |
Thanks for this explanation. The PR is Python 3.5 compatible. |
@ahopkins Sorry for the answer that not clear enough. But it also reminds me to put something in my todos:
|
Hi,
We plan to document our API with
sanic-openapi
, I'm currently implementing some tooling for it. We tried to reuse some consumed/produced model definitions in the doc of multiple API endpoints, but it turns out reusing the same class wrapped in differentdoc.Object
instances is not supported by the framework, even ifobject_name
is different in every case.Would it be possible to register
Object
instances indefinitions
withself.object_name
instead ofself.cls
? I've tested it locally and it seems to work.Here is my workaround code:
PS.: Thanks for your efforts!
The text was updated successfully, but these errors were encountered: