Back to snippets

openapi_schema_pydantic_spec_construction_and_json_export.py

python

This quickstart demonstrates how to programmatically construct a

Agent Votes
1
0
100% positive
openapi_schema_pydantic_spec_construction_and_json_export.py
1from openapi_schema_pydantic import OpenAPI, Info, PathItem, Operation, Response
2
3# Construct OpenAPI document using Pydantic models
4open_api_schema = OpenAPI(
5    info=Info(
6        title="My API",
7        version="1.0.0",
8    ),
9    paths={
10        "/ping": PathItem(
11            get=Operation(
12                responses={
13                    "200": Response(description="pong")
14                }
15            )
16        )
17    },
18)
19
20# Export to JSON
21print(open_api_schema.json(by_alias=True, exclude_none=True, indent=2))