|
| 1 | +# BenchMesh Tools – OpenAPI Generator |
| 2 | + |
| 3 | +This folder contains helper scripts for the BenchMesh Serial Service. |
| 4 | + |
| 5 | +- `driver_cli.py` – Manual driver testing utility |
| 6 | +- `gen_openapi.py` – Generate the OpenAPI specification from the FastAPI app |
| 7 | + |
| 8 | +## gen_openapi.py |
| 9 | + |
| 10 | +Generate the OpenAPI specification (JSON and/or YAML) for the BenchMesh FastAPI app. |
| 11 | + |
| 12 | +By default it writes into: |
| 13 | + |
| 14 | +``` |
| 15 | +benchmesh-serial-service/src/benchmesh_service/doc/ |
| 16 | +``` |
| 17 | + |
| 18 | +with filenames `openapi.json` and `openapi.yaml`. |
| 19 | + |
| 20 | +### Prerequisites |
| 21 | + |
| 22 | +- Python 3.10+ |
| 23 | +- PyYAML (optional, only needed to emit YAML): |
| 24 | + - `pip install pyyaml` |
| 25 | + |
| 26 | +### Running from the repo root |
| 27 | + |
| 28 | +Most commonly you’ll run the script from the repository root and point `PYTHONPATH` at the package source directory so Python imports the in-repo code: |
| 29 | + |
| 30 | +``` |
| 31 | +PYTHONPATH=benchmesh-serial-service/src \ |
| 32 | + python3 -m benchmesh_service.tools.gen_openapi --help |
| 33 | +``` |
| 34 | + |
| 35 | +Generate both JSON and YAML (defaults): |
| 36 | + |
| 37 | +``` |
| 38 | +PYTHONPATH=benchmesh-serial-service/src \ |
| 39 | + python3 -m benchmesh_service.tools.gen_openapi |
| 40 | +``` |
| 41 | + |
| 42 | +Explicit output directory and formats: |
| 43 | + |
| 44 | +``` |
| 45 | +# Write into the default doc folder (explicit), both formats |
| 46 | +PYTHONPATH=benchmesh-serial-service/src \ |
| 47 | + python3 -m benchmesh_service.tools.gen_openapi \ |
| 48 | + --out-dir benchmesh-serial-service/src/benchmesh_service/doc \ |
| 49 | + --basename openapi \ |
| 50 | + --formats both |
| 51 | +
|
| 52 | +# JSON only |
| 53 | +PYTHONPATH=benchmesh-serial-service/src \ |
| 54 | + python3 -m benchmesh_service.tools.gen_openapi --formats json |
| 55 | +
|
| 56 | +# YAML only (requires PyYAML) |
| 57 | +PYTHONPATH=benchmesh-serial-service/src \ |
| 58 | + python3 -m benchmesh_service.tools.gen_openapi --formats yaml |
| 59 | +``` |
| 60 | + |
| 61 | +### Options |
| 62 | + |
| 63 | +- `--app` – App path in the form `module:var` (default: `benchmesh_service.api:app`) |
| 64 | +- `--out-dir` – Output directory (default: `src/benchmesh_service/doc` inside the package) |
| 65 | +- `--basename` – Base filename without extension (default: `openapi`) |
| 66 | +- `--formats` – One of `json`, `yaml`, `both` (default: `both`) |
| 67 | + |
| 68 | +### Notes |
| 69 | + |
| 70 | +- The script imports the FastAPI app and calls `app.openapi()`. It does not run the server or start background threads, and it does not require hardware connections. |
| 71 | +- If you have a non-default configuration path for the service at runtime, it does not affect spec generation. The OpenAPI definition is static relative to the defined routes. |
| 72 | +- If PyYAML is not installed, the script will still write JSON and will print a warning about skipping YAML output. |
| 73 | + |
| 74 | +### Troubleshooting |
| 75 | + |
| 76 | +- If Python seems to import an older installed version of the package, ensure you set `PYTHONPATH=benchmesh-serial-service/src` when invoking the module. |
| 77 | +- If you modify routes in `benchmesh_service/api.py`, re-run this script to regenerate the spec. |
0 commit comments