|
| 1 | +# OpenAPI JSON-RPC JSDoc Generator |
| 2 | + |
| 3 | +**ALWAYS follow these instructions first. Only fall back to search or additional context gathering if the information here is incomplete or found to be in error.** |
| 4 | + |
| 5 | +OpenAPI JSON-RPC JSDoc is a Node.js library that generates OpenAPI 3.0 specifications from JSDoc comments in JavaScript files containing JSON-RPC API methods. It parses JSDoc annotations and creates complete OpenAPI schemas with request/response structures for JSON-RPC 2.0 APIs. |
| 6 | + |
| 7 | +## Working Effectively |
| 8 | + |
| 9 | +### Quick Start & Dependencies |
| 10 | +- **Node.js requirement**: Node.js >= 16 (confirmed working with v20.19.4) |
| 11 | +- **Install dependencies**: `npm install` |
| 12 | + - Takes approximately 30 seconds to complete |
| 13 | + - Will show 9 security vulnerabilities (known issue, doesn't affect functionality) |
| 14 | + - **NEVER CANCEL**: Set timeout to 60+ seconds for reliability |
| 15 | + |
| 16 | +### Core Testing & Validation |
| 17 | +- **Run unit tests**: `npm test -- --timeout=5s --match='t*'` |
| 18 | + - Takes approximately 1-2 seconds |
| 19 | + - Runs the two main unit tests (t1, t2) that validate OpenAPI generation |
| 20 | + - **Skip integration test**: The full `npm test` includes an HTTP integration test that times out after 10 seconds - this is a known issue and doesn't affect core functionality |
| 21 | + |
| 22 | +- **Manual functionality validation** (ALWAYS run after changes): |
| 23 | +```bash |
| 24 | +# Create test file in repository root |
| 25 | +cat > test-validation.js << 'EOF' |
| 26 | +const openapiJSONRpcJSDoc = require('./index.js'); |
| 27 | +async function test() { |
| 28 | + const result = await openapiJSONRpcJSDoc({ |
| 29 | + api: '/api/', |
| 30 | + servers: [{ url: 'http://localhost:8080' }], |
| 31 | + packageUrl: './package.json', |
| 32 | + files: './test/api/*.js', |
| 33 | + }); |
| 34 | + console.log('✅ Generated paths:', Object.keys(result.paths)); |
| 35 | + console.log('✅ API:', result.info.title, 'v' + result.info.version); |
| 36 | +} |
| 37 | +test().catch(console.error); |
| 38 | +EOF |
| 39 | + |
| 40 | +# Run validation test |
| 41 | +node test-validation.js |
| 42 | +``` |
| 43 | +- **Expected output**: Should show generated paths `[ '/api/v1', '/api/v2' ]` and `✅ API: openapi-jsonrpc-jsdoc v1.1.2` |
| 44 | +- **Takes**: Less than 1 second (typically 0.3 seconds) |
| 45 | +- **CRITICAL**: Always run this validation after making changes to `index.js` |
| 46 | + |
| 47 | +## Validation Scenarios |
| 48 | + |
| 49 | +### Core Functionality Testing |
| 50 | +After making any changes to the library code (`index.js`), **ALWAYS** run these validation scenarios: |
| 51 | + |
| 52 | +1. **JSDoc Parsing Test**: |
| 53 | + - The library should parse JSDoc comments from `test/api/v1.js` and `test/api/v2.js` |
| 54 | + - Generated OpenAPI spec should include both `/api/v1` and `/api/v2` endpoints |
| 55 | + - v1 endpoint should include parameter definitions from JSDoc @param tags |
| 56 | + - v2 endpoint should be marked as deprecated |
| 57 | + |
| 58 | +2. **OpenAPI Schema Validation**: |
| 59 | + - Generated JSON should be valid OpenAPI 3.0 format |
| 60 | + - Each endpoint should have POST method with JSON-RPC request body schema |
| 61 | + - Request schema should include required fields: `method`, `id`, `jsonrpc`, and `params` (when applicable) |
| 62 | + |
| 63 | +3. **Parameter Processing Test**: |
| 64 | + - Changes to parameter parsing logic require testing with complex JSDoc structures |
| 65 | + - Test with nested object parameters and various type annotations |
| 66 | + |
| 67 | +## Repository Structure |
| 68 | + |
| 69 | +### Key Files and Directories |
| 70 | +``` |
| 71 | +/ |
| 72 | +├── index.js # Main library file (174 lines) - core OpenAPI generation logic |
| 73 | +├── package.json # NPM package configuration - dependencies and scripts |
| 74 | +├── test/ |
| 75 | +│ ├── index.test.js # AVA test suite - unit and integration tests |
| 76 | +│ └── api/ # Sample API files for testing |
| 77 | +│ ├── v1.js # Example API with parameters and JSDoc |
| 78 | +│ └── v2.js # Example deprecated API |
| 79 | +├── .github/ |
| 80 | +│ └── workflows/ # CI/CD configuration |
| 81 | +│ ├── nodejs.yml # Main CI pipeline |
| 82 | +│ └── npmpublish.yml # NPM publishing workflow |
| 83 | +└── README.md # Usage documentation and examples |
| 84 | +``` |
| 85 | + |
| 86 | +### Important Code Sections |
| 87 | +- **JSDoc parsing**: Lines 4-15 in `index.js` - uses jsdoc-x library |
| 88 | +- **Parameter processing**: Lines 126-168 in `index.js` - converts JSDoc @param to OpenAPI schema |
| 89 | +- **Schema generation**: Lines 64-125 in `index.js` - creates OpenAPI endpoint definitions |
| 90 | + |
| 91 | +## Build and CI Information |
| 92 | + |
| 93 | +### No Build Process Required |
| 94 | +- This is a library package - no compilation or build step needed |
| 95 | +- Main entry point is `index.js` which exports a single async function |
| 96 | +- **Do not** look for build scripts like `npm run build` - they don't exist |
| 97 | + |
| 98 | +### CI/CD Pipeline (.github/workflows/nodejs.yml) |
| 99 | +- Runs on every push |
| 100 | +- Uses Node.js 14.x (outdated but functional) |
| 101 | +- Steps: `npm i` → `npm test` |
| 102 | +- **Known issue**: CI may fail due to test timeout, but core functionality remains valid |
| 103 | + |
| 104 | +### Dependencies |
| 105 | +- **Production**: `jsdoc-x ~4.1.0` (JSDoc parsing) |
| 106 | +- **Development**: `ava ~3.15.0` (testing), `express ~5.1.0` + `express-openapi-validator ~5.5.3` (integration testing) |
| 107 | +- **Security warnings**: 9 known vulnerabilities in dev dependencies - does not affect production usage |
| 108 | + |
| 109 | +## Common Development Tasks |
| 110 | + |
| 111 | +### Adding New JSDoc Tags Support |
| 112 | +1. Modify parameter parsing logic in `index.js` lines 126-168 |
| 113 | +2. Add test case in `test/api/` directory with new JSDoc pattern |
| 114 | +3. Update unit tests in `test/index.test.js` |
| 115 | +4. **ALWAYS validate** with manual functionality test |
| 116 | + |
| 117 | +### Debugging Generation Issues |
| 118 | +1. **Check JSDoc syntax**: Ensure proper `@param` and `@description` tags in API files |
| 119 | +2. **Verify file patterns**: The `files` parameter must match existing .js files |
| 120 | +3. **Test with minimal example**: |
| 121 | +```javascript |
| 122 | +// Create simple test API file |
| 123 | +/** |
| 124 | + * @description Test API |
| 125 | + * @param {object} params - parameters |
| 126 | + * @param {string} params.id - user id |
| 127 | + */ |
| 128 | +module.exports = (params) => params.id; |
| 129 | +``` |
| 130 | + |
| 131 | +### Testing Changes |
| 132 | +- **Unit tests**: Run `npm test -- --match='t*' --timeout=5s` (avoids integration test timeout) |
| 133 | +- **Manual validation**: Always run the functionality validation script above |
| 134 | +- **Integration testing**: The HTTP integration test times out - this is expected and doesn't indicate problems |
| 135 | + |
| 136 | +## Known Issues and Workarounds |
| 137 | + |
| 138 | +### Test Suite Timeout |
| 139 | +- **Issue**: Full `npm test` times out on HTTP integration test after 10 seconds |
| 140 | +- **Workaround**: Run unit tests only with `npm test -- --match='t*' --timeout=5s` |
| 141 | +- **Impact**: Core functionality is unaffected, unit tests validate library correctness |
| 142 | + |
| 143 | +### Security Vulnerabilities |
| 144 | +- **Issue**: 9 vulnerabilities in development dependencies (ava, jsdoc toolchain) |
| 145 | +- **Status**: Known issue, doesn't affect production library usage |
| 146 | +- **Action**: Do not run `npm audit fix --force` as it introduces breaking changes |
| 147 | + |
| 148 | +### Node.js Version Compatibility |
| 149 | +- **Requirement**: Node.js >= 16 (package.json engines field) |
| 150 | +- **CI uses**: Node.js 14.x (should be updated to 16+ but currently functional) |
| 151 | +- **Testing confirmed**: Works with Node.js 20.19.4 |
| 152 | + |
| 153 | +## Quick Reference Commands |
| 154 | + |
| 155 | +```bash |
| 156 | +# Fresh setup |
| 157 | +npm install # ~7-30 seconds |
| 158 | + |
| 159 | +# Validate functionality |
| 160 | +npm test -- --match='t*' --timeout=5s # ~1-2 seconds, unit tests only |
| 161 | + |
| 162 | +# Manual library test (create test-validation.js first, see above) |
| 163 | +node test-validation.js # <1 second |
| 164 | + |
| 165 | +# Check package info |
| 166 | +npm list --depth=0 # Show direct dependencies |
| 167 | +node --version # Verify Node.js version >= 16 |
| 168 | +``` |
| 169 | + |
| 170 | +## Timing Expectations |
| 171 | +- **npm install**: 7-30 seconds (varies by network/cache, NEVER CANCEL - set 60+ second timeout) |
| 172 | +- **Unit tests**: 1-2 seconds |
| 173 | +- **Manual validation**: <1 second (typically 0.3 seconds) |
| 174 | +- **Full test suite**: 11+ seconds (times out, use unit tests instead) |
| 175 | + |
| 176 | +**REMEMBER**: This library has no build process. Focus on testing JSDoc parsing and OpenAPI generation accuracy. |
0 commit comments