-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
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
Contracts in API with icontract #1996
Comments
Hey, congrats on your project! 🎉 So, I'm actually not sure I'm familiar with that terminology, but I think here this is all covered by Pydantic. Otherwise, please check the docs to see if there's a specific use case that you need to solve and you don't find a way to do it. And if that's the case, then you can create a new issue following the template and I (or someone else) can help you figure out how to do it. But anyway, thanks for the interest. |
Hi @tiangolo , Contracts are about the logic. A minimal example would be a precondition that an integer argument You could also have it reversed and enforce postconditions: the result of the function needs to be larger than an argument You can have a look at https://en.wikipedia.org/wiki/Design_by_contract for a brief intro into the topic. Being able to write down the preconditions and postconditions and include them somehow in the spec (as verbatim Python code?) would be really great since the contracts would be automatically testable. |
Are you able to give an example of the sort of integration you are envisioning? |
Hi @Mause , @app.get("/items/{item_id}")
def item_exists(item_id: int, q: Optional[str] = None):
...
@app.delete("/items/{item_id}")
@icontract.ensure(lambda item_id, q: not item_exists(item_id, q)
def delete_item(item_id: int, q: Optional[str] = None):
... The body of the postcondition ( |
As a side note, apart from the more precise specs, you might also get automatic testing for free (e.g., I am currently working on integration with Hypothesis, https://github.com/mristin/icontract-hypothesis) and compile-time analysis (using something like https://github.com/pschanely/CrossHair). |
Do you mean in the openapi spec? |
Exactly, documented automatically in the OpenAPI spec that FastAPI generates. In that way we can obtain testable documentation. |
It sounds like an interesting idea, though would likely be best suited to being implemented as a library that integrates with fastapi - I look forward to seeing it :) |
Hi @Mause , I suppose the package should be called |
That would be a good place to start yes |
pydantic/pydantic#2097 might also be relevant 🙂 |
@Zac-HD indeed! Mind, though, that pydantic models only the structures and relationships within those structures. The contracts should be able to model a more general relations between the endpoints and even wider relations to context, dependencies etc. |
Just a notice for all the interested: I started working on https://github.com/mristin/fastapi-icontract/. |
I added the async support in icontract and made a recipe for a workaround for the lack of async lambdas (see this Python issue). |
I am having a hard time patching However, as (From async def swagger_ui_html(req: Request) -> HTMLResponse:
root_path = req.scope.get("root_path", "").rstrip("/")
openapi_url = root_path + self.openapi_url
oauth2_redirect_url = self.swagger_ui_oauth2_redirect_url
if oauth2_redirect_url:
oauth2_redirect_url = root_path + oauth2_redirect_url
return get_swagger_ui_html(
openapi_url=openapi_url,
title=self.title + " - Swagger UI",
oauth2_redirect_url=oauth2_redirect_url,
init_oauth=self.swagger_ui_init_oauth,
) I further need to monkey-patch Additionally, once routes are added to the router, I can not just change @tiangolo @Mause any ideas how I could add my plugin to Swagger UI? Edit: formatting |
I have just published the initial version of fastapi-icontract: https://github.com/mristin/fastapi-icontract/ together with the plugin to visualize contracts in Swagger UI: https://github.com/mristin/swagger-ui-plugin-contracts. I will battle-test them at work in the next couple of weeks and then plan to publish the first major version. |
(@tiangolo @Mause I suppose this is not a priority, but maybe you would like to consider at some point how to allow for more modular inclusion of Swagger UI plugins as the current architecture for that particular point of FastAPI is completely rigid, as opposed to |
Just a brief update for all the interested: I released an alpha version of fastapi-icontract 0.0.1 on pypi.org: https://pypi.org/project/fastapi-icontract/ Bug reports and feature requests are highly welcome! |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Hi,
I was thinking about integrating contracts (as in design-by-contract) into FastAPI. While Swagger allows for some trivial contracts (e.g., string patterns and bounds on numeric parameters), more complex logic is not directly supported.
Do you already have any thoughts on this?
I maintain icontract library (http://github.com/Parquery/icontract), so I could try to integrate it somehow with FastAPI and include the code of the contracts in the generated documentation (similar to sphinx-icontract, http://github.com/Parquery/sphinx-icontract).
Please let me know if you are interested.
The text was updated successfully, but these errors were encountered: