openapi generator typescript-angular adds numbers to the end of methods names

2.1k views Asked by At

Description I have an exposed file from spring boot server and I am using this script to generate a typescript angular client

openapi-generator-cli generate -g typescript-angular -i ./src/app/data/open-api.json -o ./src/app/data/build/openapi

there are duplicated api names among the system

simple structure

Controller1 : [get,list,delete,... ....] Controller2 : [get,list,delete,... ....] .....

the generated classes looks like

Controller1Service{
public get1 ....
public list1 .....
}
Controller2Service{
public get2 ....
public list2 .....
}

but the functions are unique in the same controller and the generator still adding numbers to them

openapi-generator version 4.3.1 using npm cli

OpenAPI declaration file content or url Command line used for generation openapi-generator-cli generate -g typescript-angular -i ./src/app/data/open-api.json -o ./src/app/data/build/openapi


So any way to make these numbers disappear ?

when updating the backend the numbers may be changed inside and it will lead to a manual code refactor

1

There are 1 answers

0
Vincent AUDIBERT On

From the template, the numbers come from the 'nickname' attribute.

In the internals of OpenAPI this is produced from the operationId when generating the open-api.json file.

The solution for Spring Boot (and others alike, to adapt...) is to tweak the operationId generation (original answer).

To have generated code still match a common interface (as when using generic Spring controllers), the operationId must match a reference (default: handling method name). Here is what I used (ChangeController.class being generic and making about 80% of the API).

@Bean
public OperationCustomizer operationIdCustomizer() {
    return (operation, handlerMethod) -> {
        Class<?> superClazz = handlerMethod.getBeanType().getSuperclass();
        if (Objects.nonNull(superClazz) && superClazz.isAssignableFrom(ChangeController.class)) {
            operation.setOperationId(String.format("%s", handlerMethod.getMethod().getName()));
        }
        return operation;
    };
}

IMPORTANT NOTE : this will generate error on the spec validation when generating the Angular client, disable validation explicitly (skipValidateSpec).

Also, for generation working with Angular 10+, openapi-generator 5.0.0 is required (for generic ModuleWithProviders).

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>5.0.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/../sic-frontend/src/app/api/openapi.json</inputSpec>
                <generatorName>typescript-angular</generatorName>
                <output>${project.basedir}/../sic-frontend/src/app/api</output>
                <skipValidateSpec>true</skipValidateSpec>
                <configOptions>
                    <ngVersion>10.0.0</ngVersion>
                </configOptions>

            </configuration>
        </execution>
    </executions>
</plugin>