# oatpp-swagger
Github Repository
Example Project
oatpp-swagger - extension of oatpp module.
It provides Swagger-UI for oatpp applications.
Supports both "Simple" and "Async" oatpp APIs.
# Brief
Use
oatpp::swagger::Controllerwithoatpp::web::server::HttpConnectionHandlerUse
oatpp::swagger::AsyncControllerwithoatpp::web::server::AsyncHttpConnectionHandlerSwagger UI location -
http://localhost:<PORT>/swagger/uiOpenApi 3.0.0 specification location -
http://localhost:<PORT>/api-docs/oas-3.0.0.json
If you are using oatpp::web::server::api::ApiController most parts of your endpoints are documented automatically like:
- Endpoint name
- Parameters
- Request Body
You may add more information to your endpoint like follows:
ENDPOINT_INFO(createUser) {
info->summary = "Create new User";
info->addConsumes<Object<UserDto>>("application/json");
info->addResponse<Object<UserDto>>(Status::CODE_200, "application/json");
}
ENDPOINT("POST", "demo/api/users", createUser,
BODY_DTO(Object<UserDto>, userDto)) {
return createDtoResponse(Status::CODE_200, m_database->createUser(userDto));
}
More about endpoint annotation and API documentation read here.
# How to add Swagger UI to your project
- Add
oatpp::swagger::DocumentInfoandoatpp::swagger::Resourcescomponents to your AppComponents:
/**
* General API docs info
*/
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::swagger::DocumentInfo>, swaggerDocumentInfo)([] {
oatpp::swagger::DocumentInfo::Builder builder;
builder
.setTitle("User entity service")
.setDescription("CRUD API Example project with swagger docs")
.setVersion("1.0")
.setContactName("Ivan Ovsyanochka")
.setContactUrl("https://oatpp.io/")
.setLicenseName("Apache License, Version 2.0")
.setLicenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
.addServer("http://localhost:8000", "server on localhost");
return builder.build();
}());
/**
* Swagger-Ui Resources (<oatpp-examples>/lib/oatpp-swagger/res)
*/
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::swagger::Resources>, swaggerResources)([] {
// Make sure to specify correct full path to oatpp-swagger/res folder !!!
return oatpp::swagger::Resources::loadResources("<YOUR-PATH-TO-REPO>/lib/oatpp-swagger/res");
}());
- Create
oatpp::swagger::Controllerwith list of endpoints you whant to document and add it to router:
auto swaggerController = oatpp::swagger::Controller::createShared(<list-of-endpoints-to-document>);
router->addController(swaggerController);
Done!