This is a low code initiation to make a dotnet api with OpenApi Spec.
The steps heavily rely the cli part of the openapi-generator-tool.
openapi-generator generate -i openapi.yaml -o . -g aspnetcore --additional-properties=aspnetCoreVersion=6.0Even though it generates for dotnet v6.0, it is possible to modify the Org.OpenAPITools.csproj, field:
Project.PropertyGroup.TargetFramework from net6.0 to net8.0
Near the bottom, between
...
app.UseRouting();
app.UseEndpoints(endpoints =>
...insert app.UseAuthorization();, like so:
...
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
...Inside Org.OpenAPITools.Authentication.ApiKeyRequirementHandler, change the method:
SucceedRequirementIfApiKeyPresentAndValid, so that it always hits: context.Succeed(requirement);
We have effectively disabled auth, you have to provide your own means of doing so.
Inside: Org.OpenAPITools.Controllers.ComputerApiController we just want the examples to return json, so we remove the
xml-part.
Build with the generated build command build.sh or build.bat and run it with:
dotnet run -p src/Org.OpenAPITools/Org.OpenAPITools.csprojgoto: http://localhost:8080/openapi/index.html to see the OpenApi spec ui
and call curl localhost:8080/api/computer/10 to receive some json.