# ru-openapi-cg
[](https://crates.io/crates/ru-openapi-cg)
[](https://opensource.org/licenses/MIT)
A powerful OpenAPI 3.0 code generator written in Rust that supports multiple programming languages and frameworks.
## Features
- 🚀 **Multi-language Support**: Generate code for Rust, Flutter/Dart, TypeScript, Python, and Java
- 🎯 **Template-based Generation**: Flexible template system using Handlebars
- 🔧 **Extension/Implementation Pattern**: Generate code as extensions or implementations for existing classes
- 📝 **Automatic Type Mapping**: Smart type conversion from OpenAPI schemas to target language types
- 🔗 **Parameter Support**: Full support for path, query, header, and body parameters
- 📚 **Documentation**: Auto-generate comments and documentation from OpenAPI descriptions
- ⚡ **Fast & Efficient**: Built with Rust for high performance
## Installation
```bash
cargo install ru-openapi-cg
```
## Quick Start
### Basic Usage
```bash
# Generate Flutter/Dart extension
ru-openapi-cg -i openapi.json -t templates/flutter_dart.hbs -o api_client.dart -x MyApiClient
# Generate TypeScript extension
ru-openapi-cg -i openapi.json -t templates/typescript_ext.hbs -o api_client.ts -x MyApiClient
# Generate Python class
ru-openapi-cg -i openapi.json -t templates/python_ext.hbs -o api_client.py -x MyApiClient
# Generate Java class
ru-openapi-cg -i openapi.json -t templates/java_ext.hbs -o ApiClient.java -x MyApiClient
# Generate Rust implementation
ru-openapi-cg -i openapi.json -t templates/rust_impl.hbs -o api_client.rs -x MyStruct
```
### Command Line Options
- `-i, --input`: OpenAPI specification file (JSON/YAML)
- `-t, --template`: Template file path
- `-o, --output`: Output file path
- `-x, --target`: Target class/struct name for extension/implementation
## Supported Languages
### Flutter/Dart
- Extension-based approach
- Named parameters with required/optional support
- Automatic JSON serialization
- Error handling with debug logging
### TypeScript
- Extension function pattern
- Fetch API integration
- URL parameter building
- Type-safe responses
### Python
- Class inheritance pattern
- Requests library integration
- Query parameter support
- JSON handling
### Java
- Class extension pattern
- HttpClient integration (Java 11+)
- Jackson JSON processing
- URL encoding support
### Rust
- Trait implementation pattern
- Reqwest HTTP client
- Async/await support
- Error handling
## Template System
The generator uses Handlebars templates for maximum flexibility. You can create custom templates or modify existing ones:
```handlebars
// Example template structure
{{#each endpoints}}
pub async fn {{this.name}}(
{{#each this.params}}{{this.name}}: {{this.ty}}, {{/each}}
) -> Result<{{this.response_type}}, Error> {
// Generated implementation
}
{{/each}}
```
## Type Mapping
Automatic type conversion from OpenAPI schemas:
| integer | int | number | int | Integer | i32 |
| integer (int64) | int | number | int | Long | i64 |
| number | double | number | float | Double | f64 |
| string | String | string | str | String | String |
| boolean | bool | boolean | bool | Boolean | bool |
| array | List<T> | T[] | List[T] | List<T> | Vec<T> |
| object | Map<String, dynamic> | any | dict | Map<String, Object> | HashMap<String, Value> |
## Examples
### Input OpenAPI Specification
```json
{
"openapi": "3.0.0",
"paths": {
"/api/users/{id}": {
"get": {
"operationId": "getUser",
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"schema": {"type": "integer"}
}
],
"responses": {
"200": {
"content": {
"application/json": {
"schema": {"type": "object"}
}
}
}
}
}
}
}
}
```
### Generated Flutter/Dart Code
```dart
extension ApiClient on MyApiClient {
/// getUser
Future<GetUserResponse> getUser({
required int id,
}) async {
try {
final uri = "/api/users/{id}".replaceAll("id", id.toString());
return await _sdk.get<GetUserResponse>(uri,
fromJson: (p0) => GetUserResponse.fromJson(p0));
} catch (e) {
debugPrint('getUser error: $e');
rethrow;
}
}
}
```
## Contributing
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Changelog
### v0.1.0
- Initial release
- Multi-language support (Rust, Flutter/Dart, TypeScript, Python, Java)
- Template-based code generation
- Extension/implementation patterns
- Automatic type mapping
- Parameter support (path, query, body)