Skip to content
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

Names of derived methods are ending with numbers (Java + Spring) #6173

Open
illidan80 opened this issue May 5, 2020 · 2 comments
Open

Names of derived methods are ending with numbers (Java + Spring) #6173

illidan80 opened this issue May 5, 2020 · 2 comments

Comments

@illidan80
Copy link

Let's assume I have the following controller of version 1 (Java + Spring):

@RestController
@RequestMapping("/v1")
public class MyControllerV1 {

    @GetMapping("/count")
    public MyResponseV1 getCount() throws Exception {

        // calculate and return count
    }
}

Which generated into something like this:

public MyResponseV1 getCountUsingGET() throws RestClientException {
        ...
    }

Now, I decided to expose new API which extends the existing one:

@RestController
@RequestMapping("/v2")
public class MyControllerV2 extends MyControllerV1 {

    @GetMapping("/something-else")
    public MyOtherResponseV1 doSomethingElse() throws Exception {

        // ...
    }
}

When I generating code based on those two controllers I got generated both MyControllerV1 and MyControllerV2 (as expected). MyControllerV1 is the same as previously, but MyControllerV2 looks like this:

public MyResponseV1 getCountUsingGET1() throws RestClientException {
        ...
    }

public MyOtherResponseV1 doSomethingElseUsingGET() throws RestClientException {
        ...
    }

The expected: derived class will have exactly same method names as the base one.
The actual: derived class has its derived methods ending with numbers.

The whole idea of deriving approach is to extend existing API with less breaking changes as possible. Now everyone who migrates to MyControllerV2 has to rename all existing methods.

This is very annoying. I not sure if is this bug or feature, but is there anything I can do to get desired behavior? Or this is indeed bug?

@illidan80
Copy link
Author

illidan80 commented May 7, 2020

Update:

The situation even worse than I though initially. It not related to polymorphism, but rather to multiple defined Dockets.

Let's assume I have API v1 and API v2. I want to show documentation for both versions, so I defining two Dockets, each one with different group name:

@Bean
    public Docket generateApiV1() {
        return new Docket(DocumentationType.SWAGGER_2)
               groupName("v1")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxx"))
                .paths(ant("/v1/**"))
                .build()
                .apiInfo(buildAppInfo());
    }

@Bean
    public Docket generateApiV2() {
        return new Docket(DocumentationType.SWAGGER_2)
               groupName("v2")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxx"))
                .paths(ant("/v2/**"))
                .build()
                .apiInfo(buildAppInfo());
    }

Obviously, since its two versions of same API, there could be methods with same names in both Dockets.

So, when I generating code based on "v1" using this command:

java -jar openapi-generator-cli-4.3.0.jar generate -g java -phideGenerationTimestamp=true -pio.swagger.parser.util.RemoteUrl.trustAll=true -o ./sdk -i http://localhost:8080/v2/api-docs?group=v1 -c ./config.json

you producing incorrect method names ending with numbers, probably because in you think there is collision in names. But there isn't! You should analyze only selected APIs for generation, not all injected Dockets.

P.S. reproduced this issue in versions 4.3.0, 4.2.3
P.P.S for documentation I using springfox-swagger2

@pascalwild
Copy link

Update:

The situation even worse than I though initially. It not related to polymorphism, but rather to multiple defined Dockets.

Let's assume I have API v1 and API v2. I want to show documentation for both versions, so I defining two Dockets, each one with different group name:

@Bean
    public Docket generateApiV1() {
        return new Docket(DocumentationType.SWAGGER_2)
               groupName("v1")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxx"))
                .paths(ant("/v1/**"))
                .build()
                .apiInfo(buildAppInfo());
    }

@Bean
    public Docket generateApiV2() {
        return new Docket(DocumentationType.SWAGGER_2)
               groupName("v2")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.xxx"))
                .paths(ant("/v2/**"))
                .build()
                .apiInfo(buildAppInfo());
    }

Obviously, since its two versions of same API, there could be methods with same names in both Dockets.

So, when I generating code based on "v1" using this command:

java -jar openapi-generator-cli-4.3.0.jar generate -g java -phideGenerationTimestamp=true -pio.swagger.parser.util.RemoteUrl.trustAll=true -o ./sdk -i http://localhost:8080/v2/api-docs?group=v1 -c ./config.json

you producing incorrect method names ending with numbers, probably because in you think there is collision in names. But there isn't! You should analyze only selected APIs for generation, not all injected Dockets.

P.S. reproduced this issue in versions 4.3.0, 4.2.3
P.P.S for documentation I using springfox-swagger2

I fixed it with overriding OperationNameGenerator:

@Bean
@Primary
public OperationNameGenerator operationNameGenerator() {
    return name -> name;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants