Make OpenAPI the source of truth by enforcing that all routes and request/response shapes are validated in CI, preventing drift between implementation and spec.
Must be secure, tested, and documented Should be efficient and easy to review Code: internal/contract/openapi_contract_test.go, openapi/, cmd/openapi-validate/
Fork the repo and create a branch git checkout -b feature/openapi-ci-enforcement Implement changes Ensure OpenAPI validator runs in CI for PRs Add failing tests when an endpoint isn’t represented in spec Add a contributor checklist for updating OpenAPI Validate security assumptions Ensure auth headers and security schemes are correctly described Test and commit Run tests go test ./... Cover edge cases Backward-compatible response changes and versioning strategy Include test output and security notes Add “spec-first” policy note in PR description Example commit message feat: enforce OpenAPI contract validation in CI
Minimum 95 percent test coverage Clear documentation Timeframe: 96 hours
Based on analysis of the Stellabill backend repository, the following flaws would immediately undermine OpenAPI contract enforcement if not addressed first:
Location: internal/routes/routes.go
Issue: Multiple registrations of the same endpoints causing confusion about active routes:
/plansregistered twice (lines 88-92 and 112-113)/subscriptionsregistered multiple times (lines 94-98, 108-109)/subscriptions/:idregistered multiple times (lines 100-104, 110-111)
Immediate Impact: Contract tests may validate against incorrect or duplicate route definitions, leading to false positives/negatives.
Industry Standard Fix:
- Consolidate all route registrations to a single location per endpoint
- Establish clear API versioning strategy (choose either
/apior/api/v1, not both) - Remove all duplicate route definitions
- Create a route registration table or registry for clarity
Location: internal/contract/openapi_contract_test.go
Issue: Only validates responses for 4 hardcoded endpoints:
/api/health/api/plans/api/subscriptions/api/subscriptions/sub_test
Immediate Impact: Majority of endpoints (admin, statements, reconciliation, etc.) have zero contract validation, creating false sense of security.
Industry Standard Fix:
- Replace hardcoded endpoint validation with dynamic iteration through ALL registered routes
- For each route, validate response schema against OpenAPI specification
- Ensure validation covers all HTTP methods for each endpoint
- Maintain parallel test execution for performance
Location: internal/contract/openapi_contract_test.go
Issue: Zero validation of request components:
- Query parameters
- Request headers (including authentication)
- Request bodies (for POST/PUT/PATCH)
- Path parameter validation beyond basic existence
Immediate Impact: Contract enforcement only half-implemented; clients could send invalid requests that appear to pass validation.
Industry Standard Fix:
- For each validated route, create comprehensive RequestValidationInput
- Validate query parameters against OpenAPI specifications
- Validate headers (especially auth headers)
- Validate request bodies with appropriate media types
- Test both valid and invalid request scenarios
Location: internal/routes/routes.go
Issue: Mixed use of /api and /api/v1 path prefixes creating ambiguity about actual API structure.
Immediate Impact: OpenAPI spec cannot accurately represent the API if implementation uses conflicting versioning strategies.
Industry Standard Fix:
- Establish single, clear versioning strategy (recommend
/api/v1for versioned endpoints) - Move all versioned routes under consistent path prefix
- Keep unversioned endpoints (like
/api/health) separate if intentional - Update OpenAPI spec to match actual implemented paths
- Eliminate all duplicate route registrations in
routes.go - Establish consistent API path structure
- Verify all routes register exactly once
- Run existing tests to ensure no regression
- Replace hardcoded endpoint validation with dynamic route iteration
- Implement comprehensive response validation for ALL routes
- Add request validation (query, headers, body) for each route
- Ensure security scheme validation (auth requirements)
- Maintain test performance through parallel execution
- Enhance
cmd/openapi-validateto provide detailed mismatch reporting - Add validation that all documented endpoints are implemented
- Add validation that all implemented endpoints are documented
- Provide clear error messages for contract violations
- Verify CI pipeline runs enhanced contract tests
- Update contribution documentation with OpenAPI workflow checklist
- Add spec-first development guidelines
- Document versioning and backward compatibility strategy
After implementing this refined plan:
- ✅ Zero duplicate route registrations in implementation
- ✅ Contract tests validate 100% of implemented routes for responses
- ✅ Contract tests validate 100% of implemented routes for requests
- ✅ Consistent API path structure without versioning confusion
- ✅ CI fails when implementation deviates from OpenAPI spec
- ✅ Clear contributor guidance for maintaining API contract
- ✅ Security requirements validated in contract tests