OpenAPIModelGenerator 0.1.1
See the version list below for details.
dotnet add package OpenAPIModelGenerator --version 0.1.1
NuGet\Install-Package OpenAPIModelGenerator -Version 0.1.1
<PackageReference Include="OpenAPIModelGenerator" Version="0.1.1" />
<PackageVersion Include="OpenAPIModelGenerator" Version="0.1.1" />
<PackageReference Include="OpenAPIModelGenerator" />
paket add OpenAPIModelGenerator --version 0.1.1
#r "nuget: OpenAPIModelGenerator, 0.1.1"
#:package OpenAPIModelGenerator@0.1.1
#addin nuget:?package=OpenAPIModelGenerator&version=0.1.1
#tool nuget:?package=OpenAPIModelGenerator&version=0.1.1
OpenAPI Model Generator
OpenAPI Model Generator is a NuGet package designed to simplify generating plain C# models (POCOs) from OpenAPI specifications. Using the Microsoft.OpenApi library, this package reads OpenAPI documents, processes them, and outputs well-structured C# classes ready for use in your project.
Features
- Reads OpenAPI Specs: Uses
Microsoft.OpenApi.Readers
to parse OpenAPI documents. - Generates C# Models: Converts OpenAPI schemas into C# classes, complete with namespaces and
Newtonsoft.Json
attributes. - Highly Configurable: Specify input/output paths and namespaces for generated files.
- Built-in Logging: Leverages
Microsoft.Extensions.Logging
for detailed diagnostics and error reporting.
Installation
Install the package from NuGet:
dotnet add package OpenAPIModelGenerator
Usage
Example Workflow
Below is a step-by-step guide to use the ModelGenerator
class in your project:
Initialize the
ModelGenerator
: Provide input and output file paths, along with a logger (e.g., from a dependency injection container).using Microsoft.Extensions.Logging; using OpenAPIModelGenerator; var logger = LoggerFactory.Create(builder => builder.AddConsole()).CreateLogger<ModelGenerator>(); var generator = new ModelGenerator(logger, @"path\to\input.yaml", @"path\to\output", "MyNamespace");
Execute the Workflow: Call the
Execute
method to process the OpenAPI file and generate models.await generator.Execute();
Output Files: The generated
.cs
files will be placed in the specified output directory.
API Reference
ModelGenerator
Constructor
public ModelGenerator(
ILogger<ModelGenerator> logger,
string inputFilePath,
string outputFilePath,
string outPutNameSpace = "CodeGen")
logger
: An instance ofILogger<ModelGenerator>
for logging details during execution.inputFilePath
: Path to the OpenAPI specification file (YAML or JSON).outputFilePath
: Path where the generated.cs
files will be stored.outPutNameSpace
(optional): Namespace for the generated C# models. Defaults toCodeGen
.
Methods
Execute()
Executes the workflow of reading, computing, and generating models.Private Methods:
ReadInputFileAsync
: Reads and validates the OpenAPI file.Compute
: Processes the OpenAPI document and creates class syntax.WriteOutputFile
: Writes the generated syntax to output files.
Example Output
Here�s an example of a generated class:
using Newtonsoft.Json;
namespace MyNamespace
{
public class SampleModel
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("name")]
public string Name { get; set; }
}
}
Logging Details
The package provides extensive logging for each stage:
- Information: Reads OpenAPI version and diagnostics.
- Warnings: Highlights potential issues like warnings in the OpenAPI document.
- Errors: Reports critical issues and stops execution.
License
This project is licensed under the MIT License.
Contributing
Contributions are welcome! Feel free to open an issue or submit a pull request for enhancements, bug fixes, or feature requests.
Contact
For any questions or feedback, 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. |
-
net8.0
- Microsoft.CodeAnalysis (>= 3.9.0)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.0)
- Microsoft.OpenApi (>= 1.6.21)
- Microsoft.OpenApi.Readers (>= 1.6.21)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.