0

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!

João Fernandes
  • 75
  • 1
  • 10

2 Answers2

0

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.

Zach Hutchins
  • 801
  • 1
  • 12
  • 16
0

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>
João Fernandes
  • 75
  • 1
  • 10