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

带控制器的OpenApi/Swagger前缀函数?

石喜
2023-03-14

目前,我正在使用一个使用开放Api规范的api,该规范是使用斯瓦格生成的。在 API 中有多个终结点,例如 /客户/获取字节 Id 和 /许可证/获取字节 Id。这会导致代码生成生成一个客户端,该客户端仅使用数字列出方法。

最好有一个名为“许可证代代辅助(...)”“客户代代辅助同步”(...)的方法。(或任何区别他们的东西)

(如何)使用我的nswag配置实现这一点?我也可以改变OpenApi json的生成

我的 nswag 文件如下所示:

{
   "runtime": "Default",
   "defaultVariables": null,
   "documentGenerator": {
      "fromDocument": {
      "json": "$(InputSwagger)",
      "url": "http://redocly.github.io/redoc/openapi.yaml",
      "output": null
      }
   },
   "codeGenerators": {
      "openApiToCSharpClient": {
      "clientBaseClass": null,
      "configurationClass": null,
      "generateClientClasses": true,
      "generateClientInterfaces": true,
      "injectHttpClient": true,
      "disposeHttpClient": false,
      "protectedMethods": [],
      "generateExceptionClasses": true,
      "exceptionClass": "$(ClientName)Exception",
      "wrapDtoExceptions": true,
      "useHttpClientCreationMethod": false,
      "httpClientType": "System.Net.Http.HttpClient",
      "useHttpRequestMessageCreationMethod": false,
      "useBaseUrl": false,
      "generateBaseUrlProperty": true,
      "generateSyncMethods": false,
      "exposeJsonSerializerSettings": true,
      "clientClassAccessModifier": "public",
      "typeAccessModifier": "public",
      "generateContractsOutput": false,
      "contractsNamespace": null,
      "contractsOutputFilePath": null,
      "parameterDateTimeFormat": "s",
      "generateUpdateJsonSerializerSettingsMethod": true,
      "serializeTypeInformation": false,
      "queryNullValue": "",
      "className": "$(ClientName)Client",
      "operationGenerationMode": "MultipleClientsFromOperationId",
      "additionalNamespaceUsages": [],
      "additionalContractNamespaceUsages": [],
      "generateOptionalParameters": false,
      "generateJsonMethods": true,
      "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": "$(ClientName)Response",
      "namespace": "$(ClientNamespace)",
      "requiredPropertiesMustBeDefined": false,
      "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": "$(GeneratedSwaggerClientFile)"
      }
   }
}

我的swagger文件看起来像这样

{
   "openapi": "3.0.1",
   "info": {
      "title": "ConfigurationManagement.Api",
      "version": "v1"
   },
   "paths": {
      "/api/customer/getbyid/{id}": {
         "get": {
            "tags": [
               "Customer"
            ],
            "summary": "Gets an Customer by the id",
            "parameters": [
               {
                  "name": "id",
                  "in": "path",
                  "description": "The id of the customer.",
                  "required": true,
                  "schema": {
                     "type": "string",
                     "format": "uuid"
                  }
               }
            ],
            "responses": {
               "200": {
                  "description": "Success",
                  "content": {
                     "application/json": {
                        "schema": {
                           "$ref": "#/components/schemas/CustomerDtoSingleItemResultDto"
                        }
                     }
                  }
               }
            }
         }
      },
...
      "/api/license/getbyid/{id}": {
         "get": {
            "tags": [
               "License"
            ],
            "summary": "Gets an License by the id",
            "parameters": [
               {
                  "name": "id",
                  "in": "path",
                  "description": "The id of the license.",
                  "required": true,
                  "schema": {
                     "type": "string",
                     "format": "uuid"
                  }
               }
            ],
            "responses": {
               "200": {
                  "description": "Success",
                  "content": {
                     "application/json": {
                        "schema": {
                           "$ref": "#/components/schemas/LicenseDtoSingleItemResultDto"
                        }
                     }
                  }
               }
            }
         }
      },
...
   }
}

共有1个答案

臧弘和
2023-03-14

我分两步解决了这个问题:

    < li >我在生成swagger文件的api中添加了一个IOperationFilter
   public class OperationControllerPrefixFilter : IOperationFilter
   {
      public void Apply(OpenApiOperation operation, OperationFilterContext context)
      {
         // for example: api/customer/list will become [] {'customer', 'list'}
         var arr = context.ApiDescription.RelativePath
            .Split('/')
            .Skip(1)
            .Take(2)
            .ToArray();

         // becomes customer_list
         operation.OperationId = $"{arr[0]}_{arr[1]}";
         Debug.WriteLine(operation.OperationId);
      }
   }
 类似资料:
  • 有没有人对Swagger Codegen有一些经验?目前,我正在努力执行Swagger Codegen CLI。在本教程之后,我首先尝试通过OpenAPI生成器生成Dart代码:https://clearpoint.digital/blog/accelerate-flutter-development-with-contract-first-openapi-and-dart-code-genera

  • 问题内容: 我有一个名为“ seeder”的软件包: 现在我想用MyFunc前缀调用所有函数 我想要这样的东西: 这个输出: EDIT1 :在此示例中,parentKey是在循环中更改的字符串变量 但是GC说: 使用没有选择器的包播种机 问题答案: 您无法通过函数名称获得函数,而这正是您想要做的。原因是,如果Go工具可以检测到未显式引用某个函数(因此无法访问该函数),则该函数甚至可能无法编译为可执

  • 我有一个Spring启动应用程序。我选择将控制器实现为定义endpoint及其相应实现的接口(即EndpointX、EndpointXController W/EndpointXController是实现)。我在接口文件中有对swagger的所有注释,以防止实现类的混乱;但是,我在swagger UI上看到重复的endpoint,如下所示: 这是我的文档设置: 如何告诉Swagger/Swagge

  • 我使用Spring Boot2.0.2和Freemarker作为我的web应用程序。我的webapp的静态内容无法加载嵌套/前缀控制器。 spring Boot:将REST与静态内容分离 如有任何帮助,我们将不胜感激。

  • 注意:我查看了这篇类似的旧文章,但当时不可用。

  • 使用OpenApi比Swagger的实际优势是什么? 我是openApi技术的新手,只是想知道openApi中有什么比Swagger更多的特性。网上的文件对我没有帮助。有人能帮帮我吗。