A C# code generator that converts Swagger 2.0 JSON files and JSON Schema directories into clean C# DTO classes and FluentValidation validators. Perfect for generating type-safe C# code from OpenAPI specifications.
Install Asynkron.Jsome as a .NET global tool:
# Install the global tool
dotnet tool install -g dotnet-jsome
# Update to the latest version
dotnet tool update -g dotnet-jsomeRequirements: .NET 8.0 Runtime or SDK
The most common use case is generating C# DTOs from a Swagger 2.0 specification:
# Generate from Petstore sample (built-in)
jsome
# Generate from your own Swagger file
jsome my-api-spec.json
# Generate from a URL
jsome https://petstore.swagger.io/v2/swagger.jsonInput Swagger Schema:
{
"User": {
"type": "object",
"properties": {
"id": { "type": "integer", "format": "int64" },
"name": { "type": "string" },
"email": { "type": "string", "format": "email" }
},
"required": ["id", "name"]
}
}Generated C# DTO:
using System.ComponentModel.DataAnnotations;
using Newtonsoft.Json;
namespace Generated;
public partial class User
{
[JsonProperty("id")]
[Required]
public long Id { get; set; }
[JsonProperty("name")]
[Required]
public string Name { get; set; }
[JsonProperty("email")]
public string Email { get; set; }
}Generated FluentValidator:
using FluentValidation;
namespace Generated.Validators;
public class UserValidator : AbstractValidator<User>
{
public UserValidator()
{
RuleFor(x => x.Id).NotEmpty();
RuleFor(x => x.Name).NotEmpty();
}
}# Specify output directory and namespace
jsome my-api.json --output ./Generated --namespace MyProject.DTOs
# Use System.Text.Json instead of Newtonsoft.Json
jsome my-api.json --system-text-json
# Generate modern C# with nullable types and required keyword
jsome my-api.json --modern
# Generate C# records instead of classes
jsome my-api.json --modern --records
# Skip confirmation prompts
jsome my-api.json --yesFor more control, use a configuration file to customize generation:
config.yaml:
global:
namespace: "MyApi.Generated"
generateEnumTypes: true
rules:
# Exclude sensitive properties
"User.Password":
include: false
# Custom validation
"User.Email":
validation:
required: true
pattern: "^[\\w-\\.]+@([\\w-]+\\.)+[\\w-]{2,4}$"
message: "Please enter a valid email address"Usage:
jsome my-api.json --config config.yaml --output ./GeneratedGenerate DTOs from multiple JSON Schema files:
# Process all JSON Schema files in a directory
jsome generate --schema-dir ./schemas --namespace MyProject.GeneratedNullable Reference Types & Required Keyword:
jsome my-api.json --modernGenerates:
public class User
{
public required string Name { get; set; } // Uses 'required' keyword
public string? Email { get; set; } // Explicitly nullable
}C# Records:
jsome my-api.json --modern --recordsGenerates:
public record User(
[JsonProperty("name")] required string Name,
[JsonProperty("email")] string? Email
);Generate Swashbuckle attributes for rich OpenAPI documentation:
jsome my-api.json --swashbuckle-attributesGenerates:
[JsonProperty("email")]
[SwaggerSchema(Description = "User's email address", Format = "email")]
[SwaggerExampleValue("[email protected]")]
public string Email { get; set; }Use custom Handlebars templates for different output formats:
# Use custom templates
jsome my-api.json --templates MyCustomTemplate.hbs
# Custom template directory
jsome my-api.json --template-dir ./my-templatesGenerate Protocol Buffers (.proto) files alongside C# code:
jsome my-api.json --proto --output ./generatedProperty Path Rules:
rules:
# Target specific nested properties
"Order.OrderDetail.Product.Name":
include: true
type: "string"
validation:
maxLength: 100
pattern: "^[A-Za-z0-9\\s]+$"
# Type name prefixes/suffixes
global:
typeNamePrefix: "Api"
typeNameSuffix: "DTO" # User becomes ApiUserDTOUsage: jsome generate [options] [swagger-file-path]
Options:
-s, --schema-dir <schema-dir> Directory containing JSON Schema files
-c, --config <config> Configuration file (YAML or JSON)
-n, --namespace <namespace> Override namespace
-o, --output <output> Output directory
-y, --yes Skip confirmation prompts
-t, --template-dir <template-dir> Custom template directory
-m, --modern Enable modern C# features
--records Generate C# records
--system-text-json Use System.Text.Json
--swashbuckle-attributes Generate Swashbuckle attributes
--proto Generate Protocol Buffers files
--templates Custom template files
-h, --help Show help
git clone https://github.com/asynkron/Asynkron.Jsome.git
cd Asynkron.Jsome
dotnet build
dotnet test
dotnet run --project src/Asynkron.Jsome- Fork the repository
- Create a feature branch
- Add tests for your changes
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License.