- Add OpenApiDocs controller for serving bundled API docs - Split monolithic api-docs.yaml into modular components/ - Add organized paths/ directory with endpoint definitions - Create bundling scripts (JS, PHP, Python) for merging docs - Add API_DOCS_README.md with documentation guidelines - Update Routes.php for new API documentation endpoints - Update swagger.php view and TestDefSiteModel
5.1 KiB
CLQMS OpenAPI Documentation Structure
Overview
The OpenAPI documentation has been reorganized into a modular structure for better maintainability.
Architecture: Server-side resolution (CodeIgniter 4)
- Modular files in
paths/andcomponents/schemas/directories - CodeIgniter controller merges files on-the-fly
- Single endpoint
/api-docsserves complete specification - No build step or bundling required
How It Works
- Modular Development: Edit files in
paths/andcomponents/schemas/ - Server-Side Merge:
OpenApiDocscontroller loads and merges all files - Single Endpoint: Access
/api-docsto get complete merged specification - Cache Support: Can add caching layer for production
Recommended Usage
For Swagger UI: Use the CodeIgniter route /api-docs (already configured in app/Views/swagger.php)
For development: Edit modular files directly - changes are reflected immediately on next request
Structure
public/
├── api-docs.yaml # Main entry point with schema references
├── api-docs.bundled.yaml # Bundled version (use this for Swagger UI)
├── components/
│ └── schemas/
│ ├── authentication.yaml
│ ├── common.yaml
│ ├── edge-api.yaml
│ ├── master-data.yaml
│ ├── orders.yaml
│ ├── organization.yaml
│ ├── patient-visit.yaml
│ ├── patient.yaml
│ ├── specimen.yaml
│ ├── tests.yaml
│ └── valuesets.yaml
├── paths/
│ ├── authentication.yaml
│ ├── demo.yaml
│ ├── edge-api.yaml
│ ├── master-data.yaml
│ ├── orders.yaml
│ ├── organization.yaml
│ ├── patient-visits.yaml
│ ├── patients.yaml
│ ├── results.yaml
│ ├── specimen.yaml
│ ├── tests.yaml
│ └── valuesets.yaml
└── bundle-api-docs.py # Bundler script
Architecture
Server-Side Resolution
The application uses a CodeIgniter 4 controller (app/Controllers/OpenApiDocs.php) that:
- Loads
public/api-docs.yaml(base spec with schemas) - Scans
public/paths/*.yamlfiles - Merges all paths into the base spec
- Outputs complete YAML response
Benefits
- ✅ No bundling step - Files merged at runtime
- ✅ Immediate updates - Changes reflect immediately
- ✅ Modular structure - Separate files by domain
- ✅ CI4 compatible - Works with CodeIgniter 4
- ✅ Nginx ready - Can migrate to Nginx later
- ✅ Cacheable - Can add caching for production
Usage
For Development
Edit files directly:
- Paths:
public/paths/*.yaml - Schemas:
public/components/schemas/*.yaml
For Production/Swagger UI
Endpoint: http://localhost/clqms01/api-docs
The endpoint merges all files server-side and returns a complete OpenAPI specification that Swagger UI can consume.
Caching (Optional)
For production, you can add caching to the OpenApiDocs controller:
// In OpenApiDocs::index()
$cacheKey = 'openapi_spec_' . filemtime(FCPATH . 'api-docs.yaml');
if ($cached = cache()->get($cacheKey)) {
return $this->response->setBody($cached);
}
// ... build spec ...
// Cache for 5 minutes
cache()->save($cacheKey, $yaml, 300);
Configuration Update
Update your application configuration to use the bundled file:
PHP (CodeIgniter):
// Already updated in app/Views/swagger.php:
url: "<?= base_url('api-docs.bundled.yaml') ?>"
JavaScript/Node:
// Change from:
const spec = await fetch('/api-docs.yaml');
// To:
const spec = await fetch('/api-docs.bundled.yaml');
Benefits
- Modular: Each domain has its own file
- Maintainable: Easier to find and update specific endpoints
- Team-friendly: Multiple developers can work on different files
- Schema Reuse: Schemas defined once, referenced everywhere
- Better Versioning: Individual domains can be versioned separately
Migration Notes
- From bundled to server-side: Already done! The system now uses server-side resolution.
- Nginx migration: When ready to migrate to Nginx, the approach remains the same (CI4 handles the merging).
- Back to bundling: If needed, the bundler script is still available:
python bundle-api-docs.py
Troubleshooting
Schema Resolution Errors
If you see errors like "Could not resolve reference", ensure:
- Schema files exist in
components/schemas/ - References use correct relative paths (e.g.,
./components/schemas/patient.yaml#/Patient) - You're using the bundled file (
api-docs.bundled.yaml) for OpenAPI tools
Path Resolution Errors
OpenAPI 3.0 doesn't support external file references for paths. Always use the bundled version for serving the documentation.
Migration Notes
- The original
api-docs.yamlis preserved with schema references only - All paths have been moved to individual files in
paths/directory - A bundler script merges everything into
api-docs.bundled.yaml - Update your application to serve
api-docs.bundled.yamlinstead