英文:
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:
"documentGenerator": {
"fromDocument": {
"json": "./swagger.json",
"output": null,
"newLineBehavior": "Auto"
}
}
Pre build event:
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论