-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
The following code snippets show how to automatically generate the client code with a T4 template in Visual Studio.
Note: If you use ReSharper, you should check out ForTea for enhanced Visual Studio T4 editing support.
For a complete T4 template which generates client code, choose two snippets from this page: One to load the Swagger specification (for example from a Swagger file or a Web API assembly) and one to transform the read Swagger specification into code.
The following code loads an ASP.NET Web API assembly and generates the client for a configured Web API controller class.
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\build\NSwag\NJsonSchema.dll" #>
<#@ assembly name="$(SolutionDir)\build\NSwag\NJsonSchema.CodeGeneration.dll" #>
<#@ assembly name="$(SolutionDir)\build\NSwag\NSwag.Core.dll" #>
<#@ assembly name="$(SolutionDir)\build\NSwag\NSwag.CodeGeneration.dll" #>
<#@ assembly name="$(SolutionDir)\build\NSwag\NSwag.AssemblyLoader.dll" #>
<#@ import namespace="NSwag.CodeGeneration" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Reflection" #>
<#@ import namespace="Microsoft.CSharp" #>
<#@ output extension=".ts" #>
<#
// CONFIGURATION
var assemblyPath = @"../../bin/NSwag.Demo.Web.dll";
var controllerClass = "NSwag.Demo.Web.Controllers.PersonsController";
var defaultUrlTemplate = "api/{controller}/{action}/{id}";
// -------------
var generator = new WebApiAssemblyToSwaggerGenerator(new WebApiAssemblyToSwaggerGeneratorSettings
{
AssemblyPath = Path.GetFullPath(Path.GetDirectoryName(Host.TemplateFile) + assemblyPath),
DefaultUrlTemplate = defaultUrlTemplate
});
var service = generator.GenerateForSingleController(controllerClass);
// TODO: Add client code generation (see below)
#>
<#= code #>
After the client code is generated the given .NET DLL file is unloaded.
To generate the client code from the Swagger specification which is stored in a file in your project, use the following code in your T4 file:
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\build\NSwag\NJsonSchema.dll" #>
<#@ assembly name="$(SolutionDir)\build\NSwag\NJsonSchema.CodeGeneration.dll" #>
<#@ assembly name="$(SolutionDir)\build\NSwag\NSwag.Core.dll" #>
<#@ assembly name="$(SolutionDir)\build\NSwag\NSwag.CodeGeneration.dll" #>
<#@ import namespace="NSwag" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="Microsoft.CSharp" #>
<#@ output extension=".ts" #>
<#
// CONFIGURATION
var filePath = @"../../ServiceDefinitions/MyService.swagger";
// -------------
var fullFilePath = Path.GetFullPath(Path.GetDirectoryName(Host.TemplateFile) + filePath);
var service = SwaggerService.FromJson(File.ReadAllText(fullFilePath));
// TODO: Add client code generation (see below)
#>
<#= code #>
Only the path to the Swagger specification file is required.
To generate the client code from the Swagger specification from a remote server, use the following code in your T4 file:
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="$(SolutionDir)\build\NSwag\NJsonSchema.dll" #>
<#@ assembly name="$(SolutionDir)\build\NSwag\NJsonSchema.CodeGeneration.dll" #>
<#@ assembly name="$(SolutionDir)\build\NSwag\NSwag.Core.dll" #>
<#@ assembly name="$(SolutionDir)\build\NSwag\NSwag.CodeGeneration.dll" #>
<#@ import namespace="NSwag" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="Microsoft.CSharp" #>
<#@ output extension=".ts" #>
<#
// CONFIGURATION
var url = @"http://localhost:22093/api/Persons/Swagger";
// -------------
var service = SwaggerService.FromUrl(url);
// TODO: Add client code generation (see below)
#>
<#= code #>
Only the URL to where the Swagger specification is served is required.
Required namespaces:
<#@ import namespace="NSwag.CodeGeneration.CodeGenerators.TypeScript" #>
Required code:
var provider = new CSharpCodeProvider();
var clientGenerator = new SwaggerToTypeScriptClientGenerator(service, new SwaggerToTypeScriptClientGeneratorSettings
{
ClassName = provider.CreateEscapedIdentifier(Path.GetFileNameWithoutExtension(Host.TemplateFile))
});
var code = clientGenerator.GenerateFile();
Required namespaces:
<#@ import namespace="NSwag.CodeGeneration.CodeGenerators.CSharp" #>
Required code:
var provider = new CSharpCodeProvider();
var settings = new SwaggerToCSharpClientGeneratorSettings
{
ClassName = provider.CreateEscapedIdentifier(Path.GetFileNameWithoutExtension(Host.TemplateFile)),
};
settings.CSharpGeneratorSettings.Namespace = Host.ResolveParameterValue("directiveId", "namespaceDirectiveProcessor", "namespaceHint");
var clientGenerator = new SwaggerToCSharpClientGenerator(service, settings);
var code = clientGenerator.GenerateFile();