【问题标题】:Running NSwag without bootstrapping entire site在不引导整个站点的情况下运行 NSwag
【发布时间】:2021-10-29 07:53:48
【问题描述】:

我们正在使用 NSwag 使用如下命令生成 swagger 文件:

node_modules/.bin/nswag run foobar.nswag /runtime:NetCore31

这似乎实际上启动了应用程序,并且我们的一些引导代码将失败,因为生成 swagger 文档的进程无权访问连接字符串等。

有没有办法在不引导/启动整个应用程序的情况下生成文档?

有没有办法在启动代码中检测“swagger generation mode”,以便我们可以跳过该场景的步骤?

使用NSwag.AspNetCorenuget包版本13.11.3

foobar.nswag 文件:

{
  "runtime": "NetCore31",
  "defaultVariables": null,
  "documentGenerator": {
    "aspNetCoreToOpenApi": {
      "project": "Foobar.csproj",
      "msBuildProjectExtensionsPath": null,
      "configuration": null,
      "runtime": null,
      "targetFramework": null,
      "noBuild": false,
      "msBuildOutputPath": null,
      "verbose": true,
      "workingDirectory": null,
      "requireParametersWithoutDefault": false,
      "apiGroupNames": [
        "BACKEND"
      ],
      "defaultPropertyNameHandling": "Default",
      "defaultReferenceTypeNullHandling": "Null",
      "defaultDictionaryValueReferenceTypeNullHandling": "NotNull",
      "defaultResponseReferenceTypeNullHandling": "NotNull",
      "generateOriginalParameterNames": true,
      "defaultEnumHandling": "Integer",
      "flattenInheritanceHierarchy": false,
      "generateKnownTypes": true,
      "generateEnumMappingDescription": false,
      "generateXmlObjects": false,
      "generateAbstractProperties": false,
      "generateAbstractSchemas": true,
      "ignoreObsoleteProperties": false,
      "allowReferencesWithProperties": false,
      "excludedTypeNames": [],
      "serviceHost": null,
      "serviceBasePath": null,
      "serviceSchemes": [],
      "infoTitle": "My Title",
      "infoDescription": null,
      "infoVersion": "1.0.0",
      "documentTemplate": null,
      "documentProcessorTypes": [],
      "operationProcessorTypes": [],
      "typeNameGeneratorType": null,
      "schemaNameGeneratorType": null,
      "contractResolverType": null,
      "serializerSettingsType": null,
      "useDocumentProvider": false,
      "documentName": "v1",
      "aspNetCoreEnvironment": "Development",
      "createWebHostBuilderMethod": null,
      "startupType": null,
      "allowNullableBodyParameters": true,
      "output": "foobar-swagger.json",
      "outputType": "Swagger2",
      "newLineBehavior": "Auto",
      "assemblyPaths": [],
      "assemblyConfig": null,
      "referencePaths": [],
      "useNuGetCache": false
    }
  },
  "codeGenerators": {
    "openApiToTypeScriptClient": {
      "className": "{controller}Client",
      "moduleName": "",
      "namespace": "",
      "typeScriptVersion": 2.7,
      "template": "Fetch",
      "promiseType": "Promise",
      "httpClass": "HttpClient",
      "withCredentials": false,
      "useSingletonProvider": false,
      "injectionTokenType": "OpaqueToken",
      "rxJsVersion": 6.0,
      "dateTimeType": "Date",
      "nullValue": "Undefined",
      "generateClientClasses": false,
      "generateClientInterfaces": false,
      "generateOptionalParameters": false,
      "exportTypes": true,
      "wrapDtoExceptions": false,
      "exceptionClass": "ApiException",
      "clientBaseClass": null,
      "wrapResponses": false,
      "wrapResponseMethods": [],
      "generateResponseClasses": true,
      "responseClass": "SwaggerResponse",
      "protectedMethods": [],
      "configurationClass": null,
      "useTransformOptionsMethod": false,
      "useTransformResultMethod": false,
      "generateDtoTypes": true,
      "operationGenerationMode": "MultipleClientsFromOperationId",
      "markOptionalProperties": true,
      "generateCloneMethod": false,
      "typeStyle": "Class",
      "enumStyle": "Enum",
      "useLeafType": false,
      "classTypes": [],
      "extendedClasses": [],
      "extensionCode": null,
      "generateDefaultValues": true,
      "excludedTypeNames": [],
      "excludedParameterNames": [],
      "handleReferences": false,
      "generateConstructorInterface": true,
      "convertConstructorInterfaceData": false,
      "importRequiredTypes": true,
      "useGetBaseUrlMethod": false,
      "baseUrlTokenName": "API_BASE_URL",
      "queryNullValue": "",
      "useAbortSignal": false,
      "inlineNamedDictionaries": false,
      "inlineNamedAny": false,
      "templateDirectory": null,
      "typeNameGeneratorType": null,
      "propertyNameGeneratorType": null,
      "enumNameGeneratorType": null,
      "checksumCacheEnabled": false,
      "serviceHost": null,
      "serviceSchemes": null,
      "output": null,
      "newLineBehavior": "Auto"
    }
  }
}

【问题讨论】:

  • 我相信这取决于您的 nswag.json 以及您生成文档的方式。你能发布你的 nswag.json 吗?了解您正在使用的 nswag 版本等也会很有用。
  • @S.tenBrinke 我添加了更多信息:)
  • 你有没有想过这个问题?如果是这样,我的回答有帮助吗?如果是这样,请接受它作为答案。如果您找到了不同的解决方案,请将其发布为答案并将其标记为已接受。如果您仍有问题,也请告诉我们! :)

标签: asp.net-core nswag


【解决方案1】:

我没有在你的 nswag.json 中看到任何非常奇怪的东西;也许你仍然需要在你配置 OpenApi/Swagger 的地方发布你的 Startup.cs。

您是否可能在您的ConfigureServices() 中做一些奇怪的事情,例如运行需要特定设置(如连接字符串)的特定代码。例如?我相信这是一个不好的做法。我也相信 nswag 确实会运行你的项目/运行ConfigureServices()(但我只是凭记忆猜测),所以这可能会导致问题,是的。

否则,请尝试以下操作:

  • 我个人将 targetFramework 设置为 net5.0(您使用的是 netcore 3.1,因此需要不同的值),您将其留空
  • 这可能是多种情况的组合:
    • 我将 nobuild 设置为 false,因为我在运行 nswag 之前构建了我的项目,因为它会随机导致很多问题,当 nswag 尝试执行此操作时项目未正确构建或其他问题。
    • 我不使用 node_module 来构建 nswag.json。我使用the nswag.msbuild 包并将其全部放在一个单独的Api.Client 程序集中,并带有recommended setup
    • 我还在我的 nswag.json 中使用 "configuration": "$(Configuration)"

我刚刚想出了一个你可能会使用的非常丑陋的解决方案,但我相信这将是一个丑陋的解决方法,因为我认为你的问题在于在 ConfigureServices() 中使用了错误的做法。

解决方案是在您的 API 启动时将另一个变量传递给它,或者将环境更改为 Nswag 或类似的东西,所以在您的代码中您可以执行类似 if(environment != "nswag") {} 的操作,但同样,丑陋。 .

【讨论】:

  • 我在我的代码中偶然发现了类似的情况,我确实在我的 ConfigureServices 中做了“奇怪的事情”,我需要一个连接字符串来正确设置服务。您将此描述为一种不好的做法,我想可能是这样。但是我应该怎么做呢?
  • 我建议您创建一个新问题,也许可以通过您的问题链接向我发送邮件/回复此消息。你可以做很多事情。我相信您可以通过不同的方式生成您的 nswag。这种方式是使用实际的组装和启动之类的东西,但您也可以只使用 openapi 规范。您可以在 nswag github wiki 和 nswag 桌面工具中找到更多相关信息。此外,也许我们可以让连接字符串更漂亮一点,这样它就可以工作,或者如果不能,在代码周围使用一个 IF 语句,这样当 nswag 生成它的代码时你就不会执行它。丑陋但有效
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
  • 2016-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多