oatpp-swagger/README.md

133 lines
4.5 KiB
Markdown
Raw Normal View History

2019-01-27 19:30:19 +08:00
# oatpp-swagger [![oatpp build status](https://dev.azure.com/lganzzzo/lganzzzo/_apis/build/status/oatpp.oatpp-swagger)](https://dev.azure.com/lganzzzo/lganzzzo/_build?definitionId=2)
2018-07-28 04:59:35 +08:00
Swagger UI for oatpp services
2018-08-02 05:34:55 +08:00
2019-05-06 06:34:29 +08:00
Read more:
- [About oatpp](https://oatpp.io/)
- [What is Swagger UI](https://swagger.io/tools/swagger-ui/)
- [Endpoint annotation and API documentation in oatpp](https://oatpp.io/docs/components/api-controller/#endpoint-annotation-and-api-documentation).
2018-08-02 05:34:55 +08:00
## Example
2019-01-29 23:39:47 +08:00
For full example project see: [Example CRUD-API project with Swagger UI](https://github.com/oatpp/example-crud)
2018-08-02 05:34:55 +08:00
## Brief
- Use ```oatpp::swagger::Controller``` with ```oatpp::web::server::HttpConnectionHandler```
- Use ```oatpp::swagger::AsyncController``` with ```oatpp::web::server::AsyncHttpConnectionHandler```
- Swagger UI location - ```http://localhost:<PORT>/swagger/ui```
- OpenApi 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:
```c++
ENDPOINT_INFO(createUser) {
info->summary = "Create new User";
2020-04-27 07:53:40 +08:00
info->addConsumes<UserDto>("application/json");
info->addResponse<UserDto>(Status::CODE_200, "application/json");
info->addResponse(Status::CODE_500);
2018-08-02 05:34:55 +08:00
}
ENDPOINT("POST", "demo/api/users", createUser,
2020-04-27 07:53:40 +08:00
BODY_DTO(UserDto, userDto)) {
2018-08-02 05:34:55 +08:00
return createDtoResponse(Status::CODE_200, m_database->createUser(userDto));
}
```
### How to add Swagger UI to your project
1) Add ```oatpp::swagger::DocumentInfo``` and ```oatpp::swagger::Resources``` components to your AppComponents:
```c++
/**
* 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");
2019-09-05 07:57:05 +08:00
// When you are using the AUTHENTICATION() Endpoint-Macro you must add an SecurityScheme object (https://swagger.io/specification/#securitySchemeObject)
// For basic-authentication you can use the default Basic-Authorization-Security-Scheme like this
// For more complex authentication schemes you can use the oatpp::swagger::DocumentInfo::SecuritySchemeBuilder builder
// Don't forget to add info->addSecurityRequirement("basic_auth") to your ENDPOINT_INFO() Macro!
2019-09-05 07:57:05 +08:00
.addSecurityScheme("basic_auth", oatpp::swagger::DocumentInfo::SecuritySchemeBuilder::DefaultBasicAuthorizationSecurityScheme());
2019-08-30 21:21:19 +08:00
2018-08-02 05:34:55 +08:00
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");
}());
```
2) Create ```oatpp::swagger::Controller``` with list of endpoints you whant to document and add it to router:
```c++
auto swaggerController = oatpp::swagger::Controller::createShared(<list-of-endpoints-to-document>);
swaggerController->addEndpointsToRouter(router);
```
3) cmake :
Add the following lines to your CMakeLists.txt project file
```
find_package(oatpp 1.3.0 REQUIRED)
if(oatpp_FOUND)
message(STATUS "Found oatpp version: ${oatpp_VERSION_STRING}")
else()
message(FATAL_ERROR "Could not find oatpp")
endif()
find_package(oatpp-swagger 1.3.0 REQUIRED)
if(oatpp-swagger_FOUND)
message(STATUS "Found oatpp-swagger version: ${oatpp-swagger_VERSION_STRING}")
else()
message(FATAL_ERROR "Could not find oatpp-swagger")
endif()
include_directories(${oatpp_INCLUDE_DIRS})
include_directories(${oatpp-swagger_INCLUDE_DIRS})
set(SWAGGER_ROOT_PATH "/swagger" CACHE STRING "Default root path to the Swagger")
set(SWAGGER_UI_PATH "/ui" CACHE STRING "Default path suffix to the Swagger UI")
add_compile_definitions(
SWAGGER_ROOT_PATH="${SWAGGER_ROOT_PATH}"
SWAGGER_UI_PATH="${SWAGGER_UI_PATH}"
-DOATPP_SWAGGER_RES_PATH="${OATPP_BASE_DIR}/bin/oatpp-swagger/res"
)
target_link_libraries (project PUBLIC
PUBLIC oatpp::oatpp
PUBLIC oatpp::oatpp-swagger
)
```
2018-08-02 05:34:55 +08:00
**Done!**