我想根据 openApi 规范生成 TypeScript 客户端。我正在使用 NSwagStudio 创建配置文件。我保留默认的
{controller}Client
值,但占位符不返回任何内容,类名称只是 Client
。
可能是什么问题?
"codeGenerators": {
"openApiToTypeScriptClient": {
"className": "{controller}Client",
"operationGenerationMode": "MultipleClientsFromOperationId",
...
[ApiController]
public class WorkoutsController : ControllerBase
{
private readonly IWorkoutService _workoutService;
public WorkoutsController(IWorkoutService workoutService)
{
_workoutService = workoutService;
}
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(WorkoutResponse), StatusCodes.Status200OK)]
[HttpGet(ApiEndpoints.Workouts.Get)]
public async Task<IActionResult> Get([FromRoute] Guid id, CancellationToken token)
{
var workout = await _workoutService.GetByIdAsync(id, token);
if (workout is null)
return NotFound();
var workoutResponse = workout.ToWorkoutResponse();
return Ok(workoutResponse);
}
...
{
"openapi": "3.0.1",
"info": {
"title": "fitflow.RestApi",
"version": "1.0"
},
"paths": {
"/api/workouts/{id}": {
"get": {
"tags": [
"Workouts"
],
"parameters": [
{
"name": "id",
"in": "path",
"required": true,
"style": "simple",
"schema": {
"type": "string",
"format": "uuid"
}
}
],
"responses": {
"200": {
"description": "Success",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/WorkoutResponse"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/WorkoutResponse"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/WorkoutResponse"
}
}
}
},
"404": {
"description": "Not Found",
"content": {
"text/plain": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/ProblemDetails"
}
}
}
}
}
}
},
"/api/workouts": {
...
确保您已从 NuGet 安装了
NSwag.AspNetCore
软件包。否则,如果您安装了 Swashbuckle.AspNetCore
,它将默认为该设置,在这种情况下这是不可取的。
此外,您的
Program.cs
中可能会缺少以下几行的组合。这是通过 NSwag 生成 OpenAPI 3.0 所必需的:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApiDocument();
这些是用于提供 Swagger 文档的 NSwag 扩展:
var app = builder.Build();
app.UseOpenApi(); // use in place of app.UseSwagger()
app.UseSwaggerUi3(); // use in place of app.UseSwaggerUI()