Question
This Content is from Stack Overflow. Question asked by TheWebGuy
I have a .NET 6.0 API project. I am trying to generate a C# API client, so I added the NSwag.MSBuild nuget package and modified my API project file by adding the following:
<Target Name="NSwag" AfterTargets="PostBuildEvent" Condition=" '$(Configuration)' == 'Debug' ">
<Exec WorkingDirectory="$(ProjectDir)" EnvironmentVariables="ASPNETCORE_ENVIRONMENT=Development" Command="$(NSwagExe_Net60) run nswag.json /variables:Configuration=$(Configuration)" />
</Target>
I then created an nswag.json:
{
"runtime": "Net60",
"codeGenerators": {
"openApiToCSharpClient": {
"clientBaseClass": null,
"generateClientClasses": true,
"generateClientInterfaces": true,
"clientBaseInterface": null,
"injectHttpClient": true,
"disposeHttpClient": false,
"output": "../Blah/Client.g.cs",
"contractsOutputFilePath": "../Blah/Contracts.g.cs"
}
}
}
When the project builds it generates a client.g.cs in the Blah folder BUT the actual C# code it generates seems to be from some Pet API, for example here are a couple signatures in the IClient interface, I have no clue where this Pet API is coming from. Does anyone have any ideas:
/// <summary>
/// Add a new pet to the store
/// </summary>
/// <param name="accept_Language">The language you prefer for messages. Supported values are en-AU, en-CA, en-GB, en-US</param>
/// <param name="cookieParam">Some cookie</param>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task AddPetAsync(object body, string accept_Language, long cookieParam);
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <summary>
/// Add a new pet to the store
/// </summary>
/// <param name="accept_Language">The language you prefer for messages. Supported values are en-AU, en-CA, en-GB, en-US</param>
/// <param name="cookieParam">Some cookie</param>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task AddPetAsync(object body, string accept_Language, long cookieParam, System.Threading.CancellationToken cancellationToken);
/// <summary>
/// Update an existing pet
/// </summary>
/// <param name="accept_Language">The language you prefer for messages. Supported values are en-AU, en-CA, en-GB, en-US</param>
/// <param name="cookieParam">Some cookie</param>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task UpdatePetAsync(object body, string accept_Language, long cookieParam);
/// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
/// <summary>
/// Update an existing pet
/// </summary>
/// <param name="accept_Language">The language you prefer for messages. Supported values are en-AU, en-CA, en-GB, en-US</param>
/// <param name="cookieParam">Some cookie</param>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task UpdatePetAsync(object body, string accept_Language, long cookieParam, System.Threading.CancellationToken cancellationToken);
/// <summary>
/// Find pet by ID
/// </summary>
/// <param name="petId">ID of pet to return</param>
/// <returns>successful operation</returns>
/// <exception cref="ApiException">A server side error occurred.</exception>
System.Threading.Tasks.Task<Pet> GetPetByIdAsync(long petId);
Solution
I think this is the default behavior of the NSwag generator. You have to specify input for reading your specification.
In my case i have create a yaml file for api specification.
nswag.json file looks like following.
{
"runtime": "Net60",
"defaultVariables": null,
"documentGenerator": {
"fromDocument": {
"json": "",
"url": "openapi.yaml",
"output": null
}
},
"codeGenerators": {
"openApiToCSharpClient": {
"clientBaseClass": null,
"generateClientClasses": true,
"generateClientInterfaces": true,
"clientBaseInterface": null,
"injectHttpClient": true,
"disposeHttpClient": false,
"output": "../Blah/Client.g.cs",
"contractsOutputFilePath": "../Blah/Contracts.g.cs"
}
}
}
For futher details you can read here.
Answered by Abdul Jabbar
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 4.0.