Generate A New ASP.NETCore WebAPI Using OpenAPI Generator
Video
Bootstrapping A Project Using OpenAPI Generator
- Clone our starter repository from GitHub:bash
git clone --recursive https://github.com/redhat-appdev-practice/runtimes-dotnet-ef6-openapi.git
- This starter repo will have:
- OpenAPI Specification for our Todo API (
todo_openapi.yaml
) - A script to launch OpenAPI Generator (
generate.sh
) - Customized Generator Templates for ASP.NETCore 3.1 Development (
openapi-generator-aspnetcore3-templates
) - A Compose file to launching all of the components together locally (
docker-compose.yaml
) - A Helm Chart for deploying the application to OpenShift or another Kubernetes platform (
helm
)
- OpenAPI Specification for our Todo API (
- This starter repo will have:
- The API Specification we are using is for a TodoList application and the API Docs can be seen HERE
- Change into the cloned directory (which will from here on be referred to as <solution root>) and generate the solutionbash
./generate.sh
- Add some of the generated classes to
.gitignore
**/Controllers/** **/Attributes/** **/Authentication/** **/Converters/** **/Filters/** **/Models/** **/OpenApi/** **/wwwroot/**
- The code created by OpenAPI Generator should never be added to source control. It is meant to be re-generated at build time, every time. Some files, such as
Startup.cs
orProgram.cs
will probably end up being modified by us, the developers, and as such should be added to the.openapi-generator-ignore
and never re-generated.
- The code created by OpenAPI Generator should never be added to source control. It is meant to be re-generated at build time, every time. Some files, such as
- Open the newly created project in your IDE
- Run a restore on the projectbash
dotnet restore
- Create a new directory under the
RedHat.TodoList
project calledControllersImpl
- This is where our implementation code which extends the abstract generated code will live.
- Create a new class which extends the
DefaultApiController
class and name itDefaultApiControllerImpl
- In your IDE, ensure that the new class stubs out the required abstract methods as shown below:csharp
public class DefaultApiControllerImpl: DefaultApiController { public override ActionResult CreateTodo(Todo todo) { throw new System.NotImplementedException(); } public override ActionResult DeleteTodo(Guid todoId) { throw new System.NotImplementedException(); } public override ActionResult<Todo> GetTodo(Guid todoId) { throw new System.NotImplementedException(); } public override ActionResult<List<Todo>> Gettodos() { throw new System.NotImplementedException(); } public override ActionResult UpdateTodo(Guid todoId, Todo todo) { throw new System.NotImplementedException(); } }
- In your IDE, ensure that the new class stubs out the required abstract methods as shown below:
- At this point, you can build and run the service and see the Swagger/OpenAPI Docsbash
cd src/RedHat.TodoList dotnet run
- Open A Browser and point it to http://localhost:8080/
- You should see the Swagger/OpenAPI Documentation, but any attempt to use the API should return a
NotImplementedException
.