|
| 1 | +# OpenAPI Code Generation Tests |
| 2 | + |
| 3 | +Comprehensive test suite validating OpenAPI code generation pipeline using your actual `openapitools.json` configuration and custom templates. |
| 4 | + |
| 5 | +## Test Structure |
| 6 | + |
| 7 | +``` |
| 8 | +tests/ |
| 9 | +├── utils/test-helpers.js # Shared utilities |
| 10 | +└── unit/ |
| 11 | + ├── field-addition.test.js # Field scenarios & configuration validation |
| 12 | + └── openapi-normalizer.test.js # OpenAPI normalizer features |
| 13 | +``` |
| 14 | + |
| 15 | +## Running Tests |
| 16 | + |
| 17 | +```bash |
| 18 | +# Normal test run |
| 19 | +npm run test:unit |
| 20 | + |
| 21 | +# Inspect generated files |
| 22 | +KEEP_TEST_OUTPUT=1 npm run test:unit |
| 23 | + |
| 24 | +# Run specific tests |
| 25 | +npm run test:unit -- --testPathPattern="field-addition" |
| 26 | +``` |
| 27 | + |
| 28 | +## Test Coverage |
| 29 | + |
| 30 | +### Field Addition Tests (8 tests) |
| 31 | +Validates field addition scenarios and configuration compliance: |
| 32 | + |
| 33 | +- **Field Testing**: Base schema, optional fields (`field?: string`), required fields (`field: string`) |
| 34 | +- **Configuration Validation**: |
| 35 | + - TypeScript camelCase naming (`displayName` vs `display_name`) |
| 36 | + - ES6 exports (`export interface` vs `module.exports`) |
| 37 | + - Java 21 compatibility (no deprecated features) |
| 38 | + - Model filtering (`global-property models=TestModel`) |
| 39 | + - Custom templates (Java records vs classes) |
| 40 | + |
| 41 | +### OpenAPI Normalizer Tests (2 tests) |
| 42 | +Validates OpenAPI normalizer features with language-specific behavior: |
| 43 | + |
| 44 | +- **REF_AS_PARENT_IN_ALLOF**: Configuration-aware testing |
| 45 | + - TypeScript: Tests inheritance (`extends BaseModel`) vs flattening |
| 46 | + - Java: Always flattens properties (records don't support inheritance) |
| 47 | +- **Custom Template Compatibility**: Ensures normalizer works with custom templates |
| 48 | + |
| 49 | +## Key Features |
| 50 | + |
| 51 | +### Real Configuration Testing |
| 52 | +- Uses your actual `openapitools.json` (not mocked) |
| 53 | +- Tests adapt behavior based on your configuration |
| 54 | +- Validates custom templates are actually used |
| 55 | + |
| 56 | +### Language-Specific Validation |
| 57 | +- TypeScript: Interfaces, optional fields, ES6 exports, inheritance |
| 58 | +- Java: Records, annotations (`@Nonnull`, `@Nullable`), property flattening |
| 59 | + |
| 60 | +### Robust Design |
| 61 | +- Shared utilities prevent code duplication |
| 62 | +- Configuration-aware tests adapt to changes |
| 63 | +- Clean test isolation with conditional cleanup |
| 64 | +- Clear failure points with specific test names |
| 65 | + |
| 66 | +## Generated Validation Examples |
| 67 | + |
| 68 | +**TypeScript**: |
| 69 | +```typescript |
| 70 | +export interface TestModel { |
| 71 | + id: string; |
| 72 | + displayName: string; // camelCase naming |
| 73 | + optionalField?: string; // Optional field syntax |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +**Java**: |
| 78 | +```java |
| 79 | +public record TestModel( // Custom template (record vs class) |
| 80 | + @JsonProperty("id") @Nonnull String id, |
| 81 | + @JsonProperty("displayName") @Nonnull String displayName |
| 82 | +) {} |
| 83 | +``` |
| 84 | + |
| 85 | +## Adding New Tests |
| 86 | + |
| 87 | +**Extend existing files** for simple checks fitting current themes. |
| 88 | + |
| 89 | +**Create new files** for complex features deserving dedicated test suites: |
| 90 | + |
| 91 | +```javascript |
| 92 | +const { generateCodeWithConfig, createTestDirectory } = require('../utils/test-helpers') |
| 93 | + |
| 94 | +describe('New Feature Tests', () => { |
| 95 | + // Use shared utilities for consistency |
| 96 | +}) |
| 97 | +``` |
| 98 | + |
| 99 | +## Benefits |
| 100 | + |
| 101 | +- **Production validation**: Tests your actual build pipeline |
| 102 | +- **Regression protection**: Catches configuration and template breaks |
| 103 | +- **Maintainable**: Shared utilities, configuration-aware testing |
| 104 | +- **Developer-friendly**: Clear test names, easy debugging with `KEEP_TEST_OUTPUT` |
0 commit comments