C
C#2y ago
Anton

NSwag openapi2tsclient keep property names

The generator here makes the names camel case by default and does a mapping.
this.country = _data["Country"];
this.city = _data["City"];
this.address = _data["Address"];
this.latitude = _data["Latitude"];
this.longitude = _data["Longitude"];
this.country = _data["Country"];
this.city = _data["City"];
this.address = _data["Address"];
this.latitude = _data["Latitude"];
this.longitude = _data["Longitude"];
How can I make the ts names be pascal case? All I know is that there's a PropertyNameGeneratorType parameter which takes a string, but I couldn't find any documentation on what values it can take.
2 Replies
Anton
Anton2y ago
GitHub
GitHub - RicoSuter/NSwag: The Swagger/OpenAPI toolchain for .NET, A...
The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript. - GitHub - RicoSuter/NSwag: The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
Anton
Anton2y ago
After looking at a few issues, I realized you're meant to just do it in C#
using System.IO;
using NJsonSchema;
using NJsonSchema.CodeGeneration.TypeScript;
using NSwag;
using NSwag.CodeGeneration.TypeScript;
using Nuke.Common;

partial class Build
{
Target GenerateSwaggerTypeScriptClient => _ => _
.Executes(async () =>
{
// TODO: share this bit of configuration
const string swaggerJsonUrl = "https://localhost:7186/swagger/v1/swagger.json";
var document = await OpenApiDocument.FromUrlAsync(swaggerJsonUrl);

var settings = new TypeScriptClientGeneratorSettings();
settings.CodeGeneratorSettings.PropertyNameGenerator = KeepNamesPropertyNameGenerator.Instance;

var generator = new TypeScriptClientGenerator(document, settings);
var code = generator.GenerateFile();

var outputFilPath = ViteDirectory / "src" / "api-client.ts";
File.WriteAllText(outputFilPath, code);
});
}

public class KeepNamesPropertyNameGenerator : TypeScriptPropertyNameGenerator
{
public static readonly KeepNamesPropertyNameGenerator Instance = new();

public override string Generate(JsonSchemaProperty property)
{
return property.Name;
}
}
using System.IO;
using NJsonSchema;
using NJsonSchema.CodeGeneration.TypeScript;
using NSwag;
using NSwag.CodeGeneration.TypeScript;
using Nuke.Common;

partial class Build
{
Target GenerateSwaggerTypeScriptClient => _ => _
.Executes(async () =>
{
// TODO: share this bit of configuration
const string swaggerJsonUrl = "https://localhost:7186/swagger/v1/swagger.json";
var document = await OpenApiDocument.FromUrlAsync(swaggerJsonUrl);

var settings = new TypeScriptClientGeneratorSettings();
settings.CodeGeneratorSettings.PropertyNameGenerator = KeepNamesPropertyNameGenerator.Instance;

var generator = new TypeScriptClientGenerator(document, settings);
var code = generator.GenerateFile();

var outputFilPath = ViteDirectory / "src" / "api-client.ts";
File.WriteAllText(outputFilPath, code);
});
}

public class KeepNamesPropertyNameGenerator : TypeScriptPropertyNameGenerator
{
public static readonly KeepNamesPropertyNameGenerator Instance = new();

public override string Generate(JsonSchemaProperty property)
{
return property.Name;
}
}