如何向NSwag DocumentGenerator添加身份验证。

huangapple go评论65阅读模式
英文:

How to add authentication to nswag documentGenerator

问题

我有一个nswag.json文件,其中包含生成C# HTTP客户端的配置。但是,我想要指向的Swagger URL 受到HTTP基本身份验证的保护。

是否可以将必要的用户名和密码添加到配置文件中?

类似这样的配置(在nswag.json的documentGenerator部分):

"documentGenerator": {
  "fromDocument": {
    "url": "https://example.com/swagger/v1/swagger.json",
    "output": null,
    "newLineBehavior": "Auto",
    "authorization": {
      "type": "Basic",
      "username": "example",
      "password": "example"
    }
  }
}

提前感谢!

英文:

I have a nswag.json file with my configuration to generate c# http clients. However, the swagger url that I want to point to is protected with Http basic auth.

Is it possible to add to the configuration file the necessary username and password?

Something that would look like this (from documentGenerator section in nswag.json):

"documentGenerator": {
"fromDocument": {
  "url": "https://example.com/swagger/v1/swagger.json",
  "output": null,
  "newLineBehavior": "Auto",
  "authorization": {
    "type": "Basic",
    "username": "example",
    "password": "example"
  }
}

Thanks in advance!

答案1

得分: 0

根据我所了解,似乎没有办法像这样实现:https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.Commands/Commands/Generation/FromDocumentCommand.cs

不确定这是否有所帮助,但您可以在url字段而非json字段下输入原始的 JSON 数据。

英文:

From what I can tell there is no way to achieve this like that https://github.com/RicoSuter/NSwag/blob/master/src/NSwag.Commands/Commands/Generation/FromDocumentCommand.cs

Not sure if this helps but you can put in the raw Json, under the json field instead of the url field.

答案2

得分: 0

我通过在同一预生成事件中执行脚本,成功实现了我想要的目标。使用该脚本,我可以在从API(.../swagger.json)获取Swagger规范时添加身份验证标头。之后,Swagger文件保存在项目目录中,并由NSwag用于生成客户端。
NSwag文件中的文档生成器看起来类似于这样:

"documentGenerator": {
    "fromDocument": {
        "json": "./swagger.json",
        "output": null,
        "newLineBehavior": "Auto"
    }
}

预生成事件:

<Target Name="PreBuild" BeforeTargets="BeforeBuild">
    <Exec ConsoleToMsBuild="true" WorkingDirectory="$(ProjectDir)" Command="python swagger_download.py" />
    <Exec ConsoleToMsBuild="true" WorkingDirectory="$(ProjectDir)" Command="$(NSwagExe_Net60) run nswag.json /variables:Configuration=$(Configuration)" />
    <ItemGroup>
        <Compile Include="GeneratedClients\*.cs" />
    </ItemGroup>
</Target>
英文:

I was able to achieve what I wanted by having a script executed before nswag in the same pre build event.
With the script I could add authentication headers to fetch swagger specification from the API (.../swagger.json). After that, the swagger file is saved in the project directory and is used by nswag to generate the clients.
Document generator in Nswag file looks something like this:

&quot;documentGenerator&quot;: {
&quot;fromDocument&quot;: {
  &quot;json&quot;: &quot;./swagger.json&quot;,
  &quot;output&quot;: null,
  &quot;newLineBehavior&quot;: &quot;Auto&quot;
}

}

Pre build event:

&lt;Target Name=&quot;PreBuild&quot; BeforeTargets=&quot;BeforeBuild&quot;&gt;
	&lt;Exec ConsoleToMsBuild=&quot;true&quot; WorkingDirectory=&quot;$(ProjectDir)&quot; Command=&quot;python swagger_download.py&quot; /&gt;
	&lt;Exec ConsoleToMsBuild=&quot;true&quot; WorkingDirectory=&quot;$(ProjectDir)&quot; Command=&quot;$(NSwagExe_Net60) run nswag.json /variables:Configuration=$(Configuration)&quot; /&gt;
	&lt;ItemGroup&gt;
		&lt;Compile Include=&quot;GeneratedClients\*.cs&quot; /&gt;
	&lt;/ItemGroup&gt;
&lt;/Target&gt;

huangapple
  • 本文由 发表于 2023年7月12日 23:14:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定