|
| 1 | +--- |
| 2 | +sidebar_position: 99 |
| 3 | +--- |
| 4 | + |
| 5 | +# OpenAPI |
| 6 | + |
| 7 | +Input support; `openapi` |
| 8 | + |
| 9 | +- OpenAPI 3.0.x |
| 10 | +- OpenAPI 3.1.x |
| 11 | +- Swagger 2.0 (legacy support) |
| 12 | + |
| 13 | +## Basic Usage |
| 14 | + |
| 15 | +### Configuration |
| 16 | + |
| 17 | +Create a configuration file that specifies OpenAPI as the input type: |
| 18 | + |
| 19 | +```json |
| 20 | +{ |
| 21 | + "inputType": "openapi", |
| 22 | + "inputPath": "./api/openapi.yaml", |
| 23 | + "language": "typescript", |
| 24 | + "generators": [ ... ] |
| 25 | +} |
| 26 | +``` |
| 27 | + |
| 28 | +## Supported Generators |
| 29 | + |
| 30 | +### Custom Generator |
| 31 | + |
| 32 | +For advanced use cases, you can create [custom generators](../generators/custom.md): |
| 33 | + |
| 34 | +```json |
| 35 | +{ |
| 36 | + "inputType": "openapi", |
| 37 | + "inputPath": "./api/openapi.yaml", |
| 38 | + "language": "typescript", |
| 39 | + "generators": [ |
| 40 | + { |
| 41 | + preset: 'custom', |
| 42 | + ... |
| 43 | + renderFunction: ({generator, inputType, openapiDocument, dependencyOutputs}) |
| 44 | + { |
| 45 | + const modelinaGenerator = new JavaFileGenerator({}); |
| 46 | + modelinaGenerator.generateCompleteModels(...) |
| 47 | + } |
| 48 | + } |
| 49 | + ] |
| 50 | +} |
| 51 | + |
| 52 | +``` |
| 53 | + |
| 54 | +## Advanced Features |
| 55 | + |
| 56 | +### External References |
| 57 | + |
| 58 | +The OpenAPI parser automatically resolves external `$ref` references: |
| 59 | + |
| 60 | +```yaml |
| 61 | +components: |
| 62 | + schemas: |
| 63 | + Pet: |
| 64 | + $ref: './schemas/pet.yaml#/Pet' |
| 65 | + User: |
| 66 | + $ref: 'https://api.example.com/schemas/user.json#/User' |
| 67 | +``` |
| 68 | +
|
| 69 | +### OpenAPI 3.1 Features |
| 70 | +
|
| 71 | +Full support for OpenAPI 3.1 features including: |
| 72 | +
|
| 73 | +- JSON Schema 2020-12 compatibility |
| 74 | +- `const` keyword |
| 75 | +- `if`/`then`/`else` conditionals |
| 76 | +- Enhanced `examples` support |
| 77 | + |
| 78 | +### Validation and Error Handling |
| 79 | + |
| 80 | +The parser provides detailed validation errors: |
| 81 | + |
| 82 | +```typescript |
| 83 | +// If validation fails, you'll get detailed error information |
| 84 | +try { |
| 85 | + const document = await loadOpenapi(context); |
| 86 | +} catch (error) { |
| 87 | + console.error('OpenAPI validation failed:', error.message); |
| 88 | + // Error message includes line numbers and specific validation issues |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +## Examples |
| 93 | + |
| 94 | +### REST API Client Generation |
| 95 | + |
| 96 | +Generate a complete TypeScript client for your REST API: |
| 97 | + |
| 98 | +```json |
| 99 | +{ |
| 100 | + "inputType": "openapi", |
| 101 | + "inputPath": "./api/openapi.yaml", |
| 102 | + "language": "typescript", |
| 103 | + "generators": [ ] |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## Best Practices |
| 108 | + |
| 109 | +1. **Schema Organization**: Use `$ref` to organize complex schemas into separate files |
| 110 | +2. **Validation**: Always validate your OpenAPI documents before generation |
| 111 | +3. **Versioning**: Include version information in your API specifications |
| 112 | +4. **Documentation**: Use `description` fields extensively for better generated code |
| 113 | +5. **Examples**: Provide examples in your schemas for better understanding |
| 114 | + |
| 115 | +## Troubleshooting |
| 116 | + |
| 117 | +### Common Issues |
| 118 | + |
| 119 | +1. **Invalid $ref**: Ensure all referenced files exist and are accessible |
| 120 | +2. **Schema Validation**: Check that your OpenAPI document follows the specification |
| 121 | +3. **File Format**: Verify that YAML/JSON syntax is correct |
| 122 | +4. **Circular References**: Avoid circular `$ref` dependencies |
| 123 | + |
| 124 | +## FAQ |
| 125 | + |
| 126 | +### Can I use both OpenAPI and AsyncAPI in the same project? |
| 127 | + |
| 128 | +Yes! You can have separate configuration files for each input type and generate code to different output directories. |
| 129 | + |
| 130 | +### What's the difference between OpenAPI 3.0 and 3.1? |
| 131 | + |
| 132 | +OpenAPI 3.1 is fully compatible with JSON Schema 2020-12 and includes additional features like `const`, conditional schemas, and enhanced examples support. |
| 133 | + |
| 134 | +### How do I handle authentication in generated clients? |
| 135 | + |
| 136 | +Define security schemes in your OpenAPI document, and the generated client code will include appropriate authentication handling. |
| 137 | + |
| 138 | +### Can I customize the generated code? |
| 139 | + |
| 140 | +Yes, use the custom generator preset to create your own templates and generation logic. |
0 commit comments