-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
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
[Server] support interface and implementation classes for API controllers #426
Comments
In my opinion this is called the generation gap pattern I did not check all the java servers, but for jersey2 this is already the case: The Framework classes (jaxrs) are under The content of For me this works as expected and as described here. |
|
For jaxrs-resteay source code generation, jaxrs annotations are set at class level (see api.mustache). As proposed, jaxrs annotations could be set at interface level. Of course we still need the class implementation which would inherit from the interface. |
I seems that having annotation on interface is a jaxrs-resteasy thing. JaxRS-Jersey2 does not seems to support it. See How to annotate JAX-RS on an interface while using Jersey. |
From what I experienced neither standalone JaxRS-Jersey2 nor Spring Web could not get you having annotations only on interfaces. For Jersey2, as mentioned before such approach also does not work. I was able get such separation of generated code and implementations by combining Jersey2 and Spring. I'd like to have Spring Web based only generated code, and not introducing Jersey2 dependencies at all. When Spring team fixes SPR-11055, transition to Spring Web based annotations on interfaces should be easy - almost as just change generator language. Lets say PetApi.java is kept in separate module/jar. It could be manually created or regenerated from OpenApi every build.
PetApiImpl.java could be kept in separate jar/module and is under source version control.
//---------------- JerseyConfiguration.java
|
I would love to implement this approach in PHP server generator. It was the most frustrating thing when I worked with SwaggerCodegen. You always need to be careful to not override your current progress. I've ended up with condition in router: if (class_exists('\MyNamespace\Api\UserApi')) {
// use implementation
} else {
// use generated stub
} I would extend your list with:
With this approach we can generate complete mock API right away. And I think we need to do the same thing with models. Dumb question: Does generator create implementation class for the first time? Or maybe better not to do anyting in |
has to be overridden and that the handlers inherited that is all. |
After discussion with @wing328 I decided that interface UserApi
{
/**
* Operation description
*
* @param ServerRequestInterface $request Request
* @param ResponseInterface $response Response
* @param array|null $args Path arguments
*
* @return ResponseInterface
*/
public function getUserById($request, $response, $arguments);
} Example with abstract class: abstract class AbstractUserApi
{
/**
* Operation description
*
* @param ServerRequestInterface $request Request
* @param ResponseInterface $response Response
* @param array|null $args Path arguments
*
* @return ResponseInterface
*/
public function getUserById($request, $response, $arguments)
{
/// stubs for parsing all request parameters which user can copypaste to implementation
$userId = $arguments['userId'];
/// need to force user to overwrite thit method
throw new Exception('How about extend AbstractUserApi by \OpenAPIServer\Api\UserApi class implementing getUserById as a GET method?');
/// or return 501 Not implemented response
return $request->withStatus(501)->write('How about extend AbstractUserApi by \OpenAPIServer\Api\UserApi class implementing getUserById as a GET method?');
}
} Question to community. |
Can you please provide an example? |
Hi @wing328,
As a result : It overwrote interface and implementation class for spring as well. Query: I hope I am trying correct procedure for reproducing the issue, If yes then this is also not implemented for java spring as well, if yes should I try the enhancement for java spring as part of @Hacktoberfest ? |
Description
For auto-generated server code, we want to generate 2 files for each API controller file:
Java Spring has already implemented this:
https://github.com/openapitools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache
https://github.com/openapitools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/JavaSpring/apiController.mustache
The goal is to avoid application/business logic being overwritten by code generation so that there's less overhead when adding/deleting/updating endpoint definition.
Other server generators should leverage similar design.
openapi-generator version
Latest master
Related issues/PRs
swagger-api/swagger-codegen#5431
Suggest a fix/enhancement
If anyone wants to contribute the enhancement, please reply to let us know.
The text was updated successfully, but these errors were encountered: