OpenAPIModelCLI 0.2.0
dotnet tool install --global OpenAPIModelCLI --version 0.2.0
dotnet new tool-manifest
dotnet tool install --local OpenAPIModelCLI --version 0.2.0
#tool dotnet:?package=OpenAPIModelCLI&version=0.2.0
nuke :add-package OpenAPIModelCLI --version 0.2.0
OpenAPI Model Generator CLI
OpenAPI Model Generator CLI is a lightweight command-line interface for generating plain C# models (POCOs) from OpenAPI specifications. It leverages the OpenAPIModelGenerator library to streamline development workflows.
Features
- Generate Models Quickly: Turn OpenAPI schemas into C# models in seconds.
- Custom Output Settings: Specify output directory and namespace for generated files.
- Comprehensive Logs: Monitor each step of the process via the console.
- Platform Agnostic: Works seamlessly on Windows, macOS, and Linux.
Installation
Install the CLI globally using the .NET CLI:
dotnet tool install -g openapi-gen
To update to the latest version:
dotnet tool update -g openapi-gen
Usage
Command Syntax
openapi-gen -i <InputFilePath> -o <OutputDirectory> -n <Namespace>
Options
Option | Description | Required | Default |
---|---|---|---|
-i , --input |
Path to the OpenAPI specification file | Yes | N/A |
-o , --output |
Directory for the generated C# files | Yes | N/A |
-n , --namespace |
Namespace for the generated files | No | CodeGen |
-a , --attributes |
Specify attributes for generated class properties | No | null |
-u , --usings |
Specify usings for generated files | No | null |
-n , --documentation |
Require xml documentation generation on class properties | No | false |
-h , --help |
Displays the help message | No | N/A |
Examples
Generate Models with Custom Namespace
openapi-gen -i ./openapi.yaml -o ./GeneratedModels -n MyCustomNamespace
Generate Models with NewtonSoft Attributes & Documentation
openapi-gen -i ./openapi.yaml -o ./GeneratedModels -n MyCustomNamespace -a JsonProperty= -u Newtonsoft.Json -n true
*Note
Attributes values are not required for Newtonsoft.Json.JsonProperty and System.Text.Json.Serialization.JsonPropertyName. Values will be generated based on open api spec.
*Note
Attributes that do not require a value in the attributes string should have nothing after the equals sign. This includes no white space.
Generate Models with Comma Separated Inputs
openapi-gen -i ./openapi.yaml -o ./GeneratedModels -n MyCustomNamespace -a JsonProperty=,CustomAttribute=Include -u Newtonsoft.Json,System.Text.Json.Serializaton -n true
Generate Models with Default Namespace
openapi-gen -i ./openapi.json -o ./Models
Display Help
openapi-gen --help
Example Output
Here’s an example of a generated class:
using Newtonsoft.Json;
namespace MyCustomNamespace
{
public class SampleModel
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
}
Logging
The CLI provides real-time logs to ensure transparency and traceability:
- INFO: Indicates the progress of the operation.
- WARNING: Alerts about potential issues in the input file.
- ERROR: Stops execution for critical errors.
Example Log Output
[INFO] Reading OpenAPI specification from ./openapi.yaml
[INFO] OpenAPI version detected: 3.1.0
[INFO] Generating models in ./GeneratedModels
[INFO] Successfully generated 5 models!
Troubleshooting
Common Issues
Invalid Input File Path: Ensure the file path provided via
--input
exists and is accessible.Permission Issues: Verify you have write permissions for the output directory.
Invalid OpenAPI Document: Use an OpenAPI validator to confirm the document adheres to the OpenAPI specification.
License
This project is licensed under the MIT License.
Contributing
We welcome contributions! If you'd like to report an issue, request a feature, or submit a pull request, please visit the GitHub repository.
Contact
For questions or support, please reach out via the repository's Issues section.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
This package has no dependencies.