OpenAPITools / OpenAPITools/openapi-generator

[BUG] Auto-Generated API interface should have only one method for API

Open
#6,755 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

I am using openapi-generator-maven-plugin 4.3.1 and spring boot 2.3.1.RELEASE for my microservice.

I am facing the issue where OpenAPI auto generated interface is not working with Spring Aspect.

I have a service class which implements two interfaces where one interface SecurityUserApi.java is auto generated by openapi-generator-maven-plugin and second interface UserController.java is manually created.

Here Aspect is working for the API implementation of manually created interface and not working for API implementation of OpenAPI generated interface.

### ISSUE:
As far as I have seen, in auto generated class there are two methods, one is _getUsers2 and other is getUsers2. Where getUsers2 has the comments // Override this method. But this method is called directly in _getUsers2, hence it will not be handled by aspect interceptor. If I override _getUser2 method then Aspect is working fine.

There should be only one method for an API in auto generated interface. Then this issue will be fixed.

RoleAccessAspect.java

@Aspect
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
public class RoleAccessAspect {

@Around(ASPECT_CONDITION)
  public Object before(final ProceedingJoinPoint joinPoint) throws Throwable {
     // Role access check
  }
}

UserService.java

@Service
@Tag(name = "Security User Mamangement", description = "Security User API's")
public class UserService extends BaseService implements UserContoller, SecurityUserApi {

    @Override
    public ResponseEntity<Response> getUsers1(@RoleAccess(role = UserRoleEnum.USER_READ) final UUID authToken,
        final UUID userId) {
        // Business logic
    }

    @Override
    public ResponseEntity<Response> getUsers2(@RoleAccess(role = UserRoleEnum.USER_READ) final UUID authToken,
        final UUID userId) {
        // Business logic
    }
}

SecurityUserApi.java

@javax.annotation.Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2020-06-23T15:32:14.795+05:30[Asia/Kolkata]")
@Validated
@Api(value = "SecurityUser", description = "the SecurityUser API")
public interface SecurityUserApi {

    default Optional<NativeWebRequest> getRequest() {
        return Optional.empty();
    }

    /**
     * GET /user2 : View security users.
     * View an Security User
     *
     * @param authToken Authentication Key for Authentication and Authorization. this Authentication key obtained using Login API (required)
     * @param userId Id of the user to fetch. (optional)
     * @return User created Successfully (status code 201)
     *         or Request Failed (status code 400)
     *         or Unauthorized (status code 401)
     *         or Not Found (status code 404)
     *         or Internal Server Error (status code 500)
     */
    @ApiOperation(value = "View security users.", nickname = "getUsers2", notes = "View an Security User", response = Response.class, tags={ "SecurityUser", })
    @ApiResponses(value = { 
        @ApiResponse(code = 201, message = "User created Successfully", response = Response.class),
        @ApiResponse(code = 400, message = "Request Failed"),
        @ApiResponse(code = 401, message = "Unauthorized"),
        @ApiResponse(code = 404, message = "Not Found"),
        @ApiResponse(code = 500, message = "Internal Server Error") })
    @RequestMapping(value = "/user2",
        produces = { "application/json", "application/xml" }, 
        method = RequestMethod.GET)
    default ResponseEntity<Response> _getUsers2(@ApiParam(value = "Authentication Key for Authentication and Authorization. this Authentication key obtained using Login API" ,required=true) @RequestHeader(value="authToken", required=true) UUID authToken,@ApiParam(value = "Id of the user to fetch.") @Valid @RequestParam(value = "userId", required = false) UUID userId) {
        return getUsers(authToken, userId);
    }

    // Override this method
    default  ResponseEntity<Response> getUsers2(UUID authToken, UUID userId) {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "{ \"message\" : \"message\", \"object\" : \"{}\" }";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString);
                    break;
                }
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) {
                    String exampleString = "<null> <object>UNDEFINED_EXAMPLE_VALUE</object> <message>aeiou</message> </null>";
                    ApiUtil.setExampleResponse(request, "application/xml", exampleString);
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
    }
}

UserContoller.java

@RestController
public interface UserContoller {

    @GetMapping(value = "/user1")
    ResponseEntity<Response> getUsers1(@RequestHeader(SecurityConstants.AUTH_TOKEN) UUID authToken,
        @RequestParam(required = false, name = "id") UUID userId);

}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the generated SecurityUserApi.java and its implementation in UserService.java, then reproduce generation with openapi-generator-maven-plugin 4.3.1. Verify how _getUsers2 delegates to getUsers2 and confirm that the generated API exposes one overridable method whose invocation is intercepted by the aspect.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.