我正在尝试使用 NSwag CLI 在 .NET Core 和 TypeScript 中生成客户端代码:
nswag run options.nswag
Run Code Online (Sandbox Code Playgroud)
这适用于各种 Swagger 2.0 JSON 文件,但会为 OpenApi 3.0 JSON 文件生成多个具有完全相同名称的函数。
我的 options.swag 文件具有以下 .NET 内容(TypeScript 以类似的方式生成并遇到相同的问题):
{
"runtime": "NetCore21",
"defaultVariables": null,
"documentGenerator": {
"fromDocument": {
"url": "https://somedomain.com/openapi.json",
"output": null
}
},
"codeGenerators": {
"openApiToCSharpClient": {
"clientBaseClass": null,
"configurationClass": null,
"generateClientClasses": true,
"generateClientInterfaces": false,
"injectHttpClient": true,
"disposeHttpClient": true,
"protectedMethods": [],
"generateExceptionClasses": true,
"exceptionClass": "ClientApiException",
"wrapDtoExceptions": true,
"useHttpClientCreationMethod": false,
"httpClientType": "System.Net.Http.HttpClient",
"useHttpRequestMessageCreationMethod": false,
"useBaseUrl": true,
"generateBaseUrlProperty": true,
"generateSyncMethods": false,
"exposeJsonSerializerSettings": false,
"clientClassAccessModifier": "public",
"typeAccessModifier": "public",
"generateContractsOutput": false,
"contractsNamespace": "DataLake",
"contractsOutputFilePath": null,
"parameterDateTimeFormat": "s",
"generateUpdateJsonSerializerSettingsMethod": true,
"serializeTypeInformation": false,
"queryNullValue": "",
"className": "{controller}Client",
"operationGenerationMode": "MultipleClientsFromOperationId",
"additionalNamespaceUsages": [],
"additionalContractNamespaceUsages": [],
"generateOptionalParameters": false,
"generateJsonMethods": false,
"enforceFlagEnums": false,
"parameterArrayType": "System.Collections.Generic.IEnumerable",
"parameterDictionaryType": "System.Collections.Generic.IDictionary",
"responseArrayType": "System.Collections.Generic.ICollection",
"responseDictionaryType": "System.Collections.Generic.IDictionary",
"wrapResponses": false,
"wrapResponseMethods": [],
"generateResponseClasses": true,
"responseClass": "SwaggerResponse",
"namespace": "SwaggerApiClientGenerationToolTest",
"requiredPropertiesMustBeDefined": true,
"dateType": "System.DateTimeOffset",
"jsonConverters": null,
"anyType": "object",
"dateTimeType": "System.DateTimeOffset",
"timeType": "System.TimeSpan",
"timeSpanType": "System.TimeSpan",
"arrayType": "System.Collections.Generic.ICollection",
"arrayInstanceType": "System.Collections.ObjectModel.Collection",
"dictionaryType": "System.Collections.Generic.IDictionary",
"dictionaryInstanceType": "System.Collections.Generic.Dictionary",
"arrayBaseType": "System.Collections.ObjectModel.Collection",
"dictionaryBaseType": "System.Collections.Generic.Dictionary",
"classStyle": "Poco",
"generateDefaultValues": true,
"generateDataAnnotations": true,
"excludedTypeNames": [],
"excludedParameterNames": [],
"handleReferences": false,
"generateImmutableArrayProperties": false,
"generateImmutableDictionaryProperties": false,
"jsonSerializerSettingsTransformationMethod": null,
"inlineNamedArrays": false,
"inlineNamedDictionaries": false,
"inlineNamedTuples": true,
"inlineNamedAny": false,
"generateDtoTypes": true,
"templateDirectory": null,
"typeNameGeneratorType": null,
"propertyNameGeneratorType": null,
"enumNameGeneratorType": null,
"serviceHost": null,
"serviceSchemes": null,
"output": "TheClient.cs"
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让我控制函数名称的生成,以便添加一些判别式(类似于className依赖于{controller}令牌)?
问题:使用 OpenApi 3.0 JSON 文件时如何避免 NSwag 生成多个同名函数?
小智 7
如果没有 OpenAPI.json 文件,就很难为您的案例提供答案。但是您应该能够通过更改 OperationGenerationMode 来实现不同的方法命名。
public enum OperationGenerationMode
{
/// <summary>Multiple clients from the Swagger operation ID in the form '{controller}_{action}'.</summary>
MultipleClientsFromOperationId,
/// <summary>From path segments (operation name = last segment, client name = second to last segment).</summary>
MultipleClientsFromPathSegments,
/// <summary>From the first operation tag and path segments (operation name = last segment, client name = first operation tag).</summary>
MultipleClientsFromFirstTagAndPathSegments,
/// <summary>From the first operation tag and operation ID (operation name = operation ID, client name = first operation tag).</summary>
MultipleClientsFromFirstTagAndOperationId,
/// <summary>From the Swagger operation ID.</summary>
SingleClientFromOperationId,
/// <summary>From path segments suffixed by HTTP operation name</summary>
SingleClientFromPathSegments,
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11413 次 |
| 最近记录: |