NSwag 在使用 OpenApi 3.0 JSON 文件时生成多个同名函数
NSwag generates multiple functions with the same name when using OpenApi 3.0 JSON file
我正在尝试使用 NSwag CLI 在 .NET Core 和 TypeScript 中生成客户端代码:
nswag run options.nswag
这适用于各种 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"
}
}
有没有办法让我控制函数名称的生成,以便添加一些判别式(类似于依赖 {controller}
令牌的 className
)?
问题:如何避免NSwag在使用OpenApi 3.0JSON文件时生成多个同名函数?
没有 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,
}
如果您使用 Swashbuckle 生成 API swagger 元数据,您可以将路由的名称 属性 或 HttpPost/Get/Delete 属性设置为类似
Name="[Controller][Action]"
然后,如果您确保 C# 中的方法名称是唯一的,并且避免在名称中使用下划线,那么您也会获得友好的 NSwag 客户端名称。
我正在尝试使用 NSwag CLI 在 .NET Core 和 TypeScript 中生成客户端代码:
nswag run options.nswag
这适用于各种 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"
}
}
有没有办法让我控制函数名称的生成,以便添加一些判别式(类似于依赖 {controller}
令牌的 className
)?
问题:如何避免NSwag在使用OpenApi 3.0JSON文件时生成多个同名函数?
没有 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,
}
如果您使用 Swashbuckle 生成 API swagger 元数据,您可以将路由的名称 属性 或 HttpPost/Get/Delete 属性设置为类似
Name="[Controller][Action]"
然后,如果您确保 C# 中的方法名称是唯一的,并且避免在名称中使用下划线,那么您也会获得友好的 NSwag 客户端名称。