-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TODO]OpenAPI client/server generator #331
Comments
Hello @rhard , Thanks for writing!
This is definitely what we want to have. Regards, |
This is great! Thank you! |
Once the OpenAPI/Swagger spec is available, you may want to use OpenAPI Generator to generate API clients, server stubs, documentation, and more. We also welcome contributions to add an Oat++ server stub generator. Let me know if anyone is interested and I can show some good starting points. Disclosure: I'm the top contributor to OpenAPI Generator. |
Hello @wing328 ,
This feature is in the roadmap and already scheduled for development to be out together with the next release of Oat++.
I'm interested, it will be really helpful! |
I'll see what I can do in the coming week. Will keep you posted. |
I've created OpenAPITools/openapi-generator#7903 to start with. |
Got it! |
Hi, any update on this feature? I think this could be very useful I started using oatpp and it's very powerful! |
this feature is wanted in my job. Because many REST-like service providers describe interfaces with OpenAPI |
Hello guys, Thanks for the comments and upvotes! At the moment I have no free time to work on this issue. |
I am working on that issue on my free time. Hope to finish in couple of months (it is not much work, can be done in couple of weeks) Question - would you like cpp-oatpp-server generator to generate normal or async controllers? creating cpp-oatpp-server generator more easy with reference project.i almost finished it. https://github.com/makru86/oatpp-petstore check it and give a comment - i will appreciate. |
@lganzzzo may i ask you to assign me to this issue on github? Or may i assign myself? |
Hello @makru86 ,
These are great news!
The Simple API is definitely a priority as it used much more often. Regards, |
@makru86 , FYI: For DTOs you can use class UserDTO : public oatpp::DTO {
DTO_INIT(UserDTO, DTO)
DTO_FIELD(Int64, id);
DTO_FIELD(String, username);
DTO_FIELD(String, firstName);
DTO_FIELD(String, lastName);
DTO_FIELD(String, email);
DTO_FIELD(String, password);
DTO_FIELD(String, phone);
DTO_FIELD(Int32, userStatus);
DTO_HC_EQ(id, username, firstName, lastName, email, password, phone, userStatus);
}; |
Thanks, @lganzzzo. I started new project with simple controllers instead of async. Thanks for the hint to use DTO_HC_EQ. Currently I don't have any questions. Maybe later. |
@lganzzzo , If you have, please share an example how to implement AuthorizationHandler for API-Key authorization scheme, with custom header name (api_key in case of petstore https://github.com/makru86/oatpp-petstore-2/blob/master/api/petstore.yaml#L623) As I saw from examples, Basic and Bearer both use Authorization header. How to make oatpp to pass api_key header value into handleAuthorization override? |
Hello @makru86 , It might be possible to do it like follows: In swagger-component auto ss = oatpp::swagger::SecurityScheme::createShared();
ss->type = "apiKey";
ss->name = "api_key";
ss->in = "header";
//ss->scheme = ???;
builder.addSecurityScheme("api_key", ss);
return builder.build(); In Endpoint ENDPOINT_INFO(getInventory) {
auto authHandler = ApiController::getDefaultAuthorizationHandler();
if(authHandler) {
info->headers.add<oatpp::String>("api_key").description = authHandler->getScheme();
info->authorization = authHandler->getScheme();
}
}
ENDPOINT("GET", "store/inventory", getInventory,
BUNDLE(String, apiKey)) // pass auth payload as bundle from interceptor
{
...
} In AuthInterceptorFor complete auth example with interceptor see https://github.com/oatpp/example-jwt/blob/master/src/interceptor/AuthInterceptor.cpp auto authHeader = request->getHeader("api_key");
auto authObject = std::static_pointer_cast<MyAuthObject>(m_authHandler.handleAuthorization(authHeader));
if(authObject) {
request->putBundleData("apiKey", authObject->apiKey);
// TODO check API KEY
return nullptr; // Continue - API-KEY is valid.
} Please let me know if this works |
It works! Thanks!
I did it little different - checking API key in |
I think the server is mostly finished and I plan to start working on generator soon.
What do you think - maybe something is missing? Examles:
|
Hello @makru86 , A quick comment (https://github.com/makru86/oatpp-petstore-starter/blob/master/src/auth/ApiKeyAuth.hpp#L90):
As for the petstore example it looks great. The general approach is that you want to have the API part to be generated separately from your business logic and So basically what you want to have is something like this: Generated part/**
Pet service interface
**/
class PetSerivce {
public:
virtual std::shared_ptr<OutgoingResponse> addPet(const Object<PetDTO>& body) = 0;
}
/**
Controller
**/
class PetController : public oatpp::web::server::api::ApiController {
private:
std::shared_ptr<PetService> m_service;
public:
explicit PetController(OATPP_COMPONENT(std::shared_ptr<PetService>, petService),
OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper))
: oatpp::web::server::api::ApiController(objectMapper)
, m_service(petService)
{}
ENDPOINT("POST", "/pet", addPet,
BODY_DTO(Object<PetDTO>, body))
{
return m_serivce->addPet(body);
}
}; User partThen I can easily implement my logic by extending PetService: class MyPetSerivce : public PetService {
public:
std::shared_ptr<OutgoingResponse> addPet(const Object<PetDTO>& body) override;
}
...
router->addController(std::make_shared<PetController>(std::make_shared<MyPetService>())); Also, it's a good idea to have the generated part as a library with versions |
Thanks for comments, @lganzzzo , It is interesting to learn Oat++ from you. Changes:
Project needs to be cleaned:
Please comment if something can be improved. About endpoint list in auth interceptor: given endpoints (https://github.com/makru86/oatpp-petstore-starter/blob/master/generated/auth/ApiKeyAuth.hpp#L90 )
I think it is unavoidable to list the first one as apiKeyAuth==true so that route edit: edit: |
another examples of C++ generators can be viewed here https://github.com/OpenAPITools/openapi-generator/tree/master/samples/server/petstore |
I just started work on the generator for information, @lganzzzo . |
I must pause work on this for couple months - other work. Commented in OpenAPITools/openapi-generator#7903 |
First of all thank you for this great project!
Is there any plans to create REST API client/server genearator for Oatpp from OpenAPI/swagger files?
The text was updated successfully, but these errors were encountered: