Skip to content

Commit cdc1923

Browse files
committed
feat: add swagger API example
1 parent abe47f0 commit cdc1923

12 files changed

Lines changed: 2585 additions & 7 deletions

BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,12 @@ docs_index(
6565
srcs = [
6666
"@rules_docs_e2e_git_last_updated//:docs",
6767
"@rules_docs_e2e_smoke//:docs",
68+
"@rules_docs_examples_swagger//:docs",
6869
],
6970
nav = {
7071
"@rules_docs_e2e_smoke//:docs": "e2e/smoke",
7172
"@rules_docs_e2e_git_last_updated//:docs": "e2e/git_last_updated",
73+
"@rules_docs_examples_swagger//:docs": "examples/swagger",
7274
},
7375
title = "examples",
7476
)

MODULE.bazel

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,20 @@ docgen.mkdocs(
3939
)
4040
use_repo(docgen, "mkdocs")
4141

42-
E2E_FOLDERS = [
43-
"git_last_updated",
44-
"smoke",
42+
NESTED_MODULES = [
43+
"e2e/git_last_updated",
44+
"e2e/smoke",
45+
"examples/swagger",
4546
]
4647

4748
[
4849
[
49-
bazel_dep(name = "rules_docs_e2e_%s" % folder, version = "0.0.0", dev_dependency = True),
50+
bazel_dep(name = "rules_docs_%s" % escaped_folder, version = "0.0.0", dev_dependency = True),
5051
local_path_override(
51-
module_name = "rules_docs_e2e_%s" % folder,
52-
path = "e2e/%s" % folder,
52+
module_name = "rules_docs_%s" % escaped_folder,
53+
path = folder,
5354
),
5455
]
55-
for folder in E2E_FOLDERS
56+
for folder in NESTED_MODULES
57+
for escaped_folder in [folder.replace("/", "_")]
5658
]

MODULE.bazel.lock

Lines changed: 331 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/swagger/BUILD

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
2+
load("@rules_docs//docgen:defs.bzl", "docs")
3+
load("@mkdocs//:defs.bzl", "mkdocs_build", "mkdocs_config", "mkdocs_serve")
4+
load("@rules_python//python:pip.bzl", "compile_pip_requirements")
5+
6+
compile_pip_requirements(
7+
name = "requirements",
8+
src = "pyproject.toml",
9+
requirements_txt = "requirements.txt",
10+
)
11+
12+
docs(
13+
name = "other_docs",
14+
entry = "api-reference.md",
15+
data = ["openapi.yaml"],
16+
readme_content = "API Reference",
17+
)
18+
19+
# Main documentation configuration
20+
docs(
21+
name = "docs",
22+
nav = {
23+
"README.md": "Home",
24+
":other_docs": "API Reference (Redoc)",
25+
},
26+
rewrite_path = "examples/swagger",
27+
visibility = ["//visibility:public"],
28+
)
29+
30+
# Generate mkdocs configuration
31+
mkdocs_config(
32+
name = "mkdocs_config",
33+
docs = ":docs",
34+
mkdocs_base = "mkdocs.tpl.yaml",
35+
)
36+
37+
# Build documentation site
38+
mkdocs_build(
39+
name = "mkdocs",
40+
config = ":mkdocs_config",
41+
docs = [":docs"],
42+
site_dir = "site",
43+
visibility = ["//visibility:public"],
44+
)
45+
46+
# Serve documentation locally for development
47+
# Recommended: run with ibazel for auto-reload on changes
48+
mkdocs_serve(
49+
name = "mkdocs.serve",
50+
config = ":mkdocs_config",
51+
docs = [":docs"],
52+
visibility = ["//visibility:public"],
53+
)

examples/swagger/MODULE.bazel

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
module(name = "rules_docs_examples_swagger")
2+
3+
bazel_dep(name = "rules_docs")
4+
5+
local_path_override(
6+
module_name = "rules_docs",
7+
path = "../..",
8+
)
9+
10+
bazel_dep(name = "rules_python", version = "1.7.0")
11+
12+
# Configure Python and pip dependencies
13+
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
14+
pip.parse(
15+
hub_name = "mkdocs_deps",
16+
python_version = "3.11",
17+
requirements_lock = "//:requirements.txt",
18+
)
19+
use_repo(pip, "mkdocs_deps")
20+
21+
# Configure docgen with mkdocs plugins
22+
docgen = use_extension("@rules_docs//docgen:extensions.bzl", "docgen")
23+
docgen.mkdocs(plugins = ["mkdocs-redoc-tag"], pypi_hub = "@mkdocs_deps")
24+
use_repo(docgen, "mkdocs")

examples/swagger/MODULE.bazel.lock

Lines changed: 836 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/swagger/README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Swagger/OpenAPI Example
2+
3+
This directory contains an OpenAPI 3.0 specification file that demonstrates rich API documentation suitable for use with SwaggerUI.
4+
We then run Bazel actions to run the docgen tool and stitch into the parent repository's documentation site.
5+
6+
## Files
7+
8+
- `openapi.yaml` - Complete OpenAPI 3.0 specification for a Task Management API
9+
10+
## Features Demonstrated
11+
12+
The OpenAPI specification includes:
13+
14+
- **Comprehensive API Documentation**: Rich descriptions for all endpoints, parameters, and schemas
15+
- **Multiple Endpoints**: CRUD operations for tasks, projects, and users
16+
- **Request/Response Examples**: Multiple examples for different use cases
17+
- **Detailed Schemas**: Well-documented data models with validation rules
18+
- **Error Responses**: Standardized error response formats
19+
- **Authentication**: Bearer token authentication scheme
20+
- **Pagination**: Pagination support for list endpoints
21+
- **Filtering & Sorting**: Query parameters for filtering and sorting results
22+
23+
## Viewing with SwaggerUI
24+
25+
Run `bazel run mkdocs.serve`
26+
27+
Then open http://localhost:8080
28+
29+
## API Overview
30+
31+
The Task Management API provides:
32+
33+
- **Tasks**: Create, read, update, and delete tasks with status, priority, assignments, and due dates
34+
- **Projects**: Organize tasks into projects
35+
- **Users**: Manage users who can be assigned to tasks
36+
37+
All endpoints require Bearer token authentication and return JSON responses.

examples/swagger/api-reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<redoc src="./openapi.yaml"></redoc>

examples/swagger/mkdocs.tpl.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
site_name: Swagger Example
2+
repo_url: https://github.com/jacobshirley/rules_docs/tree/main/examples/swagger
3+
docs_dir: docs
4+
site_dir: site
5+
plugins:
6+
- redoc-tag

0 commit comments

Comments
 (0)