当前位置: 首页 > 知识库问答 >
问题:

使用OpenApi 3.0 JSON文件时,NSwag生成多个同名函数

彭俊智
2023-03-14

我正在尝试在这两种语言中生成客户端代码。使用NSwag CLI的NET Core和TypeScript:

nswag run options.nswag

这适用于各种Swagger 2.0 JSON文件,但为OpenApi 3.0 JSON文件生成多个具有完全相同名称的函数。

我的选择。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"
    }
}

是否有一种方法可以控制函数名的生成,以便添加一些判别式(类似于依赖于标记的类名)?

问:在使用OpenApi 3.0 JSON文件时,如何避免NSwag生成多个同名函数?

共有2个答案

戚学文
2023-03-14

如果您使用Swashback生成API swagger元数据,那么可以将路由的Name属性或HttpPost/Get/Delete属性设置为

Name="[Controller][Action]"

然后,如果您确保C#中的方法名称是唯一的,并且避免在名称中使用下划线,那么您也可以获得友好的NSwag客户端名称。

仲孙小云
2023-03-14

没有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,
}

https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.Commands/Commands/CodeGeneration/OperationGenerationMode.cs

 类似资料:
  • 我正在使用NSwag并尝试将OpenAPI JSON文档转换为版本2。这是我的配置: 但是,当我将生成的OpenAPI文件粘贴到Swagger Editor中时,它会显示错误: 如何配置NSwag以生成正确的OpenAPI 2.0文件?

  • 问题内容: 我正在尝试将我的reducer的结果输出到多个文件。数据结果全部包含在一个文件中,其余结果根据它们所尊重的文件中的类别进行划分。我知道使用0.18可以使用MultipleOutputs做到这一点,并且它尚未被删除。但是,我正在尝试使我的应用程序兼容0.20+。现有的多输出功能仍然需要JobConf(我的应用程序使用Job和Configuration)。如何根据密钥生成多个输出? 问题答

  • @cucumberoptions(features=“src/test/resources/features/xxxxx/xxxxx.feature”,标签=“@tag1”,胶={“com.xxxx.sfdc.opportunities.stepdefinities”},格式={“pretty”,“html:target/cucumber-reports/cucumber-pretty”,“jso

  • 我有一个服务器应用程序,它在启动时加载多个脚本文件(用于处理特定的数据集-字段)。脚本应被解析,脚本的“表达式”-数据应存储在映射中(按列名),以便以后可以从那里访问和执行它们。 有两种类型的脚本。“简单”的函数只包含一个函数,而复杂的函数目前的结构类似于下面的示例(可能有更多私有函数/字段): 和是服务器应用程序将使用的“公共”功能<加载/评估文件后,将执行一次代码>自检,并在需要时对传入数据执

  • 我已经用SFTPinboundFileSynchronizingMessageSource配置了我的应用程序。我希望一个消息被接收与文件作为结果有效载荷每当一个新的文件被发现(即,一个新的文件名和/或时间戳)。以下是我所拥有的: 除了在随后的轮询中获得相同的文件名和不同的修改后的时间戳的情况之外,这种方法很有效。在这种情况下,我得到消息的空结果。当时间戳不同但文件名相同时,如何确保生成消息?

  • 如何通过< code>ItextSharp合并多个pdf文件(运行时生成),然后打印它们。 我找到了下面的链接,但考虑到存储的pdf文件不是我的情况,该方法需要pdf名称。 我有多个报告,我将通过以下方法将它们转换为< code>pdf文件: 现在我想将所有生成的文件()合并到一个pdf文件中以打印它