OpenAPIModelGenerator 0.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package OpenAPIModelGenerator --version 0.1.1
                    
NuGet\Install-Package OpenAPIModelGenerator -Version 0.1.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenAPIModelGenerator" Version="0.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenAPIModelGenerator" Version="0.1.1" />
                    
Directory.Packages.props
<PackageReference Include="OpenAPIModelGenerator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpenAPIModelGenerator --version 0.1.1
                    
#r "nuget: OpenAPIModelGenerator, 0.1.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OpenAPIModelGenerator@0.1.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OpenAPIModelGenerator&version=0.1.1
                    
Install as a Cake Addin
#tool nuget:?package=OpenAPIModelGenerator&version=0.1.1
                    
Install as a Cake Tool

OpenAPI Model Generator

NuGet Version License: MIT

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:

  1. 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");
    
  2. Execute the Workflow: Call the Execute method to process the OpenAPI file and generate models.

    await generator.Execute();
    
  3. 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 of ILogger<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 to CodeGen.
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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.2.0 125 11/30/2024
0.1.1 109 11/25/2024
0.1.0 118 11/22/2024