oatpp / example-postgresql

A complete example of a "CRUD" service (UserService) built with Oat++ and using oatpp ORM with PostgreSQL.
https://oatpp.io/
Apache License 2.0
38 stars 19 forks source link

Having Role enum name in createUser from swagger fails with error. #8

Open NoWhereMan1979 opened 6 months ago

NoWhereMan1979 commented 6 months ago

Hi dear Leo. I have problem with enum in DTO_FIELD in createUser api execution. If i remove the role from request, it works. couldn't figure it out why. Also my db encoding is UTF8 Thanks in advance.

Request body:

{
  "username": "string2",
  "email": "string2",
  "password": "string2",
  "role": "ROLE_GUEST"
}

Response:

{
  "status": "ERROR",
  "code": 500,
  "message": "ERROR:  invalid byte sequence for encoding \"UTF8\": 0xdd 0xdd\nCONTEXT:  unnamed portal parameter $4\n"
}

And this is my DTO:

#include OATPP_CODEGEN_BEGIN(DTO)

ENUM(Role, v_int32,
     VALUE(GUEST, 0, "ROLE_GUEST"),
     VALUE(ADMIN, 1, "ROLE_ADMIN")
)

class UserDto : public oatpp::DTO {

  DTO_INIT(UserDto, DTO)

  DTO_FIELD(String, id);
  DTO_FIELD(String, userName, "username");
  DTO_FIELD(String, email, "email");
  DTO_FIELD(String, password, "password");
  DTO_FIELD(Enum<Role>::AsString, role, "role");

};

#include OATPP_CODEGEN_END(DTO)

UserDb:

  QUERY(createUser,
        "INSERT INTO AppUser"
        "(id, username, email, password, role) VALUES "
        "(uuid_generate_v4(), :user.username, :user.email, :user.password, :user.role)"
        "RETURNING *;",
        PREPARE(true), // user prepared statement!
        PARAM(oatpp::Object<UserDto>, user))