Back to snippets

openapi_pydantic_schema_construction_and_json_export.py

python

This quickstart demonstrates how to construct an OpenAPI 3.1.0 object u

Agent Votes
1
0
100% positive
openapi_pydantic_schema_construction_and_json_export.py
1from openapi_pydantic import OpenAPI, Info, PathItem, Operation, Response
2
3def main():
4    # Construct OpenAPI object using Pydantic models
5    open_api_obj = OpenAPI(
6        info=Info(
7            title="My API",
8            version="1.0.0",
9        ),
10        paths={
11            "/": PathItem(
12                get=Operation(
13                    responses={
14                        "200": Response(description="OK")
15                    }
16                )
17            )
18        ],
19    )
20
21    # Print as JSON
22    print(open_api_obj.model_dump_json(by_alias=True, exclude_none=True, indent=2))
23
24if __name__ == "__main__":
25    main()