Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a8dab27

Browse files
committedMay 17, 2024·
Update to the latest oatpp API
1 parent 2cba24c commit a8dab27

14 files changed

+78
-53
lines changed
 

‎CMakeLists.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
cmake_minimum_required(VERSION 3.1)
22
project(crud)
33

4-
set(CMAKE_CXX_STANDARD 11)
4+
set(CMAKE_CXX_STANDARD 17)
55

66
add_library(crud-lib
77
src/controller/StaticController.hpp
@@ -25,9 +25,9 @@ target_include_directories(crud-lib PUBLIC src)
2525

2626
## link libs
2727

28-
find_package(oatpp 1.3.0 REQUIRED)
29-
find_package(oatpp-swagger 1.3.0 REQUIRED)
30-
find_package(oatpp-sqlite 1.3.0 REQUIRED)
28+
find_package(oatpp 1.4.0 REQUIRED)
29+
find_package(oatpp-swagger 1.4.0 REQUIRED)
30+
find_package(oatpp-sqlite 1.4.0 REQUIRED)
3131

3232
target_link_libraries(crud-lib
3333
# Oat++

‎src/App.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ void run() {
3434
oatpp::network::Server server(connectionProvider,
3535
connectionHandler);
3636

37-
OATPP_LOGD("Server", "Running on port %s...", connectionProvider->getProperty("port").toString()->c_str());
37+
OATPP_LOGd("Server", "Running on port {}...", connectionProvider->getProperty("port").toString())
3838

3939
server.run();
4040

@@ -49,17 +49,17 @@ void run() {
4949
*/
5050
int main(int argc, const char * argv[]) {
5151

52-
oatpp::base::Environment::init();
52+
oatpp::Environment::init();
5353

5454
run();
5555

5656
/* Print how many objects were created during app running, and what have left-probably leaked */
5757
/* Disable object counting for release builds using '-D OATPP_DISABLE_ENV_OBJECT_COUNTERS' flag for better performance */
5858
std::cout << "\nEnvironment:\n";
59-
std::cout << "objectsCount = " << oatpp::base::Environment::getObjectsCount() << "\n";
60-
std::cout << "objectsCreated = " << oatpp::base::Environment::getObjectsCreated() << "\n\n";
59+
std::cout << "objectsCount = " << oatpp::Environment::getObjectsCount() << "\n";
60+
std::cout << "objectsCreated = " << oatpp::Environment::getObjectsCreated() << "\n\n";
6161

62-
oatpp::base::Environment::destroy();
62+
oatpp::Environment::destroy();
6363

6464
return 0;
6565
}

‎src/AppComponent.hpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99

1010
#include "oatpp/web/server/HttpConnectionHandler.hpp"
1111
#include "oatpp/web/server/HttpRouter.hpp"
12+
#include "oatpp/web/mime/ContentMappers.hpp"
13+
1214
#include "oatpp/network/tcp/server/ConnectionProvider.hpp"
1315

14-
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
16+
#include "oatpp/json/ObjectMapper.hpp""
1517
16-
#include "oatpp/core/macro/component.hpp"
18+
#include "oatpp/macro/component.hpp"
1719
1820
/**
1921
* Class which creates and holds Application components and registers components in oatpp::base::Environment
@@ -35,10 +37,16 @@ class AppComponent {
3537
/**
3638
* Create ObjectMapper component to serialize/deserialize DTOs in Controller's API
3739
*/
38-
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
39-
auto objectMapper = oatpp::parser::json::mapping::ObjectMapper::createShared();
40-
objectMapper->getDeserializer()->getConfig()->allowUnknownFields = false;
41-
return objectMapper;
40+
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::mime::ContentMappers>, apiContentMappers)([] {
41+
42+
auto json = std::make_shared<oatpp::json::ObjectMapper>();
43+
json->serializerConfig().json.useBeautifier = true;
44+
45+
auto mappers = std::make_shared<oatpp::web::mime::ContentMappers>();
46+
mappers->putMapper(json);
47+
48+
return mappers;
49+
4250
}());
4351
4452
/**
@@ -61,10 +69,12 @@ class AppComponent {
6169
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::network::ConnectionHandler>, serverConnectionHandler)([] {
6270
6371
OATPP_COMPONENT(std::shared_ptr<oatpp::web::server::HttpRouter>, router); // get Router component
64-
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper); // get ObjectMapper component
72+
OATPP_COMPONENT(std::shared_ptr<oatpp::web::mime::ContentMappers>, contentMappers); // get ContentMappers component
6573
6674
auto connectionHandler = oatpp::web::server::HttpConnectionHandler::createShared(router);
67-
connectionHandler->setErrorHandler(std::make_shared<ErrorHandler>(objectMapper));
75+
connectionHandler->setErrorHandler(std::make_shared<ErrorHandler>(
76+
contentMappers->getMapper("application/json"))
77+
);
6878
return connectionHandler;
6979
7080
}());

‎src/SwaggerComponent.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
#include "oatpp-swagger/Model.hpp"
66
#include "oatpp-swagger/Resources.hpp"
7-
#include "oatpp/core/macro/component.hpp"
7+
#include "oatpp/macro/component.hpp"
88

99
/**
1010
* Swagger ui is served at

‎src/controller/StaticController.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
#define CRUD_STATICCONTROLLER_HPP
44

55
#include "oatpp/web/server/api/ApiController.hpp"
6-
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
7-
#include "oatpp/core/macro/codegen.hpp"
8-
#include "oatpp/core/macro/component.hpp"
6+
#include "oatpp/json/ObjectMapper.hpp"
7+
#include "oatpp/macro/codegen.hpp"
8+
#include "oatpp/macro/component.hpp"
99

1010
#include OATPP_CODEGEN_BEGIN(ApiController) //<- Begin Codegen
1111

1212
class StaticController : public oatpp::web::server::api::ApiController {
1313
public:
14-
StaticController(const std::shared_ptr<ObjectMapper>& objectMapper)
15-
: oatpp::web::server::api::ApiController(objectMapper)
14+
StaticController(const std::shared_ptr<oatpp::web::mime::ContentMappers>& apiContentMappers)
15+
: oatpp::web::server::api::ApiController(apiContentMappers)
1616
{}
1717
public:
1818

1919
static std::shared_ptr<StaticController> createShared(
20-
OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper) // Inject objectMapper component here as default parameter
20+
OATPP_COMPONENT(std::shared_ptr<oatpp::web::mime::ContentMappers>, apiContentMappers) // Inject ContentMappers
2121
){
22-
return std::make_shared<StaticController>(objectMapper);
22+
return std::make_shared<StaticController>(apiContentMappers);
2323
}
2424

2525
ENDPOINT("GET", "/", root) {

‎src/controller/UserController.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include "service/UserService.hpp"
66

77
#include "oatpp/web/server/api/ApiController.hpp"
8-
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
9-
#include "oatpp/core/macro/codegen.hpp"
8+
#include "oatpp/web/mime/ContentMappers.hpp"
9+
#include "oatpp/macro/codegen.hpp"
1010

1111
#include OATPP_CODEGEN_BEGIN(ApiController) //<- Begin Codegen
1212

@@ -15,17 +15,17 @@
1515
*/
1616
class UserController : public oatpp::web::server::api::ApiController {
1717
public:
18-
UserController(OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
19-
: oatpp::web::server::api::ApiController(objectMapper)
18+
UserController(OATPP_COMPONENT(std::shared_ptr<oatpp::web::mime::ContentMappers>, apiContentMappers))
19+
: oatpp::web::server::api::ApiController(apiContentMappers)
2020
{}
2121
private:
2222
UserService m_userService; // Create user service.
2323
public:
2424

2525
static std::shared_ptr<UserController> createShared(
26-
OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper) // Inject objectMapper component here as default parameter
26+
OATPP_COMPONENT(std::shared_ptr<oatpp::web::mime::ContentMappers>, apiContentMappers) // Inject ContentMappers
2727
){
28-
return std::make_shared<UserController>(objectMapper);
28+
return std::make_shared<UserController>(apiContentMappers);
2929
}
3030

3131
ENDPOINT_INFO(createUser) {

‎src/db/UserDb.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include "dto/UserDto.hpp"
66
#include "oatpp-sqlite/orm.hpp"
77

8+
#include "oatpp/base/Log.hpp"
9+
810
#include OATPP_CODEGEN_BEGIN(DbClient) //<- Begin Codegen
911

1012
/**
@@ -23,7 +25,7 @@ class UserDb : public oatpp::orm::DbClient {
2325
migration.migrate(); // <-- run migrations. This guy will throw on error.
2426

2527
auto version = executor->getSchemaVersion();
26-
OATPP_LOGD("UserDb", "Migration - OK. Version=%lld.", version);
28+
OATPP_LOGd("UserDb", "Migration - OK. Version={}.", version);
2729

2830
}
2931

‎src/dto/StatusDto.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
#ifndef CRUD_STATUSDTO_HPP
33
#define CRUD_STATUSDTO_HPP
44

5-
#include "oatpp/core/macro/codegen.hpp"
6-
#include "oatpp/core/Types.hpp"
5+
#include "oatpp/macro/codegen.hpp"
6+
#include "oatpp/Types.hpp"
77

88
#include OATPP_CODEGEN_BEGIN(DTO)
99

‎src/dto/UserDto.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef UserDto_hpp
22
#define UserDto_hpp
33

4-
#include "oatpp/core/macro/codegen.hpp"
5-
#include "oatpp/core/Types.hpp"
4+
#include "oatpp/macro/codegen.hpp"
5+
#include "oatpp/Types.hpp"
66

77
#include OATPP_CODEGEN_BEGIN(DTO)
88

‎src/service/UserService.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "dto/StatusDto.hpp"
88

99
#include "oatpp/web/protocol/http/Http.hpp"
10-
#include "oatpp/core/macro/component.hpp"
10+
#include "oatpp/macro/component.hpp"
1111

1212
class UserService {
1313
private:

‎test/UserControllerTest.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
void UserControllerTest::onRun() {
1414

1515
/* Remove test database file before running the test */
16-
OATPP_LOGI(TAG, "DB-File='%s'", TESTDATABASE_FILE);
16+
OATPP_LOGi(TAG, "DB-File='{}'", TESTDATABASE_FILE);
1717
std::remove(TESTDATABASE_FILE);
1818

1919
/* Register test components */
@@ -32,13 +32,14 @@ void UserControllerTest::onRun() {
3232
OATPP_COMPONENT(std::shared_ptr<oatpp::network::ClientConnectionProvider>, clientConnectionProvider);
3333

3434
/* Get object mapper component */
35-
OATPP_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, objectMapper);
35+
OATPP_COMPONENT(std::shared_ptr<oatpp::web::mime::ContentMappers>, contentMappers);
3636

3737
/* Create http request executor for Api Client */
3838
auto requestExecutor = oatpp::web::client::HttpRequestExecutor::createShared(clientConnectionProvider);
3939

4040
/* Create Test API client */
41-
auto client = TestClient::createShared(requestExecutor, objectMapper);
41+
auto client = TestClient::createShared(requestExecutor,
42+
contentMappers->getMapper("application/json"));
4243

4344
auto dto = UserDto::createShared();
4445

@@ -53,7 +54,9 @@ void UserControllerTest::onRun() {
5354
OATPP_ASSERT(addedUserResponse->getStatusCode() == 200);
5455

5556
/* Read response body as MessageDto */
56-
auto addedUserDto = addedUserResponse->readBodyToDto<oatpp::Object<UserDto>>(objectMapper.get());
57+
auto addedUserDto = addedUserResponse->readBodyToDto<oatpp::Object<UserDto>>(
58+
contentMappers->selectMapperForContent(addedUserResponse->getHeader("Content-Type"))
59+
);
5760

5861
int addedUserId = addedUserDto->id;
5962

@@ -62,7 +65,9 @@ void UserControllerTest::onRun() {
6265

6366
OATPP_ASSERT(newUserResponse->getStatusCode() == 200);
6467

65-
auto newUserDto = newUserResponse->readBodyToDto<oatpp::Object<UserDto>>(objectMapper.get());
68+
auto newUserDto = newUserResponse->readBodyToDto<oatpp::Object<UserDto>>(
69+
contentMappers->selectMapperForContent(addedUserResponse->getHeader("Content-Type"))
70+
);
6671

6772
OATPP_ASSERT(newUserDto->id == addedUserId);
6873

‎test/app/TestClient.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define DatabaseTestClient_hpp
33

44
#include "oatpp/web/client/ApiClient.hpp"
5-
#include "oatpp/core/macro/codegen.hpp"
5+
#include "oatpp/macro/codegen.hpp"
66

77
#include "dto/UserDto.hpp"
88

‎test/app/TestComponent.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#include "oatpp/network/virtual_/server/ConnectionProvider.hpp"
88
#include "oatpp/network/virtual_/Interface.hpp"
99

10-
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
10+
#include "oatpp/json/ObjectMapper.hpp"
1111

12-
#include "oatpp/core/macro/component.hpp"
12+
#include "oatpp/macro/component.hpp"
1313

1414
#include "TestDatabaseComponent.hpp"
1515

@@ -62,8 +62,16 @@ class TestComponent {
6262
/**
6363
* Create ObjectMapper component to serialize/deserialize DTOs in Contoller's API
6464
*/
65-
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::data::mapping::ObjectMapper>, apiObjectMapper)([] {
66-
return oatpp::parser::json::mapping::ObjectMapper::createShared();
65+
OATPP_CREATE_COMPONENT(std::shared_ptr<oatpp::web::mime::ContentMappers>, apiContentMappers)([] {
66+
67+
auto json = std::make_shared<oatpp::json::ObjectMapper>();
68+
json->serializerConfig().json.useBeautifier = true;
69+
70+
auto mappers = std::make_shared<oatpp::web::mime::ContentMappers>();
71+
mappers->putMapper(json);
72+
73+
return mappers;
74+
6775
}());
6876

6977
};

‎test/tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
#include "oatpp-test/UnitTest.hpp"
3-
#include "oatpp/core/base/Environment.hpp"
3+
#include "oatpp/Environment.hpp"
44
#include "UserControllerTest.hpp"
55

66
#include <iostream>
@@ -17,19 +17,19 @@ void runTests() {
1717

1818
int main() {
1919

20-
oatpp::base::Environment::init();
20+
oatpp::Environment::init();
2121

2222
runTests();
2323

2424
/* Print how much objects were created during app running, and what have left-probably leaked */
2525
/* Disable object counting for release builds using '-D OATPP_DISABLE_ENV_OBJECT_COUNTERS' flag for better performance */
2626
std::cout << "\nEnvironment:\n";
27-
std::cout << "objectsCount = " << oatpp::base::Environment::getObjectsCount() << "\n";
28-
std::cout << "objectsCreated = " << oatpp::base::Environment::getObjectsCreated() << "\n\n";
27+
std::cout << "objectsCount = " << oatpp::Environment::getObjectsCount() << "\n";
28+
std::cout << "objectsCreated = " << oatpp::Environment::getObjectsCreated() << "\n\n";
2929

30-
OATPP_ASSERT(oatpp::base::Environment::getObjectsCount() == 0);
30+
OATPP_ASSERT(oatpp::Environment::getObjectsCount() == 0);
3131

32-
oatpp::base::Environment::destroy();
32+
oatpp::Environment::destroy();
3333

3434
return 0;
3535
}

0 commit comments

Comments
 (0)
Please sign in to comment.