2

I have swagger generated by plugin from yaml definition

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <version>4.3.1</version>
            <configuration>
                <logToStderr>false</logToStderr>
                <generatorName>spring</generatorName>
                <generateSupportingFiles>true</generateSupportingFiles>
                <supportingFilesToGenerate>ApiUtil.java</supportingFilesToGenerate>
                <ignoreFileOverride>${project.basedir}/src/main/resources/.openapi-generator-ignore
                </ignoreFileOverride>
                <configOptions>
                    <dateLibrary>java8-localdatetime</dateLibrary>
                    <serializableModel>true</serializableModel>
                    <delegatePattern>true</delegatePattern>
                    <useBeanValidation>true</useBeanValidation>
                    <performBeanValidation>true</performBeanValidation>
...

This generate swagger documentation with empty controller groups

enter image description here

I want to remove them

I tried to modify

@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            //.ignoredParameterTypes(DokumentApiController.class)
            .select()
            //.apis(RequestHandlerSelectors.basePackage("....dokument.api"))//.apis(GroupNameFilter()) //
            //.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
            .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
            .paths(PathSelectors.any()) //.paths(restApiPaths()) //.paths(PathSelectors.any())
            .build()
            .apiInfo(apiInfo());

You can see what I tried in commented code. The result is allways the same. I either have the empty groups there or I do not have anything on the documentation page http://127.0.0.1:8080/.../swagger-ui.html#/

How can I remove those empty "-controller" groups?

P.S.: I seen How to remove controller list from Swagger UI I tried to use it in class which implements Delegate but a lot of classes in my solution are generated by plugin and I can't change them directly.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
boucekv
  • 1,220
  • 2
  • 20
  • 47
  • Does this answer your question? [How to remove controller list from Swagger UI](https://stackoverflow.com/questions/50229661/how-to-remove-controller-list-from-swagger-ui) – Aris Mar 31 '21 at 08:39
  • I seen that post I tried it in class which implements the Delegate but it does not help me. My problem is that a lot of classes are generated I can not change them directly. – boucekv Mar 31 '21 at 09:28
  • Alright, have you tried any of these approaches? [Check Here](https://www.baeldung.com/spring-swagger-hiding-endpoints) – Aris Mar 31 '21 at 09:53
  • I can't add anotation to generated class. It will be overwriten during next build. I would have to find a way how to make plugin to insert those anotations to generated class for me and I do not know how to do it now. – boucekv Mar 31 '21 at 10:03

1 Answers1

1

The solution is to modify the mustache file that is responsible for shaping the API classes. This requires creating a custom openapi-generator artifact. In the official site they give instructions about it, but I have preferred to build it from a fork, updating the groupid and the version.

The change to make is:

  1. Do a fork and clone the code.

  2. Locate the file:

    modules / openapi-generator / src / main / resources / JavaSpring / api.mustache

  3. Modify the line:

@Api (value = "{{{baseName}}}", description = "the {{{baseName}}} API")

for this other:

@Api (value = "{{{baseName}}}", description = "the {{{baseName}}} API", tags = {"{{{baseName}}}",})
  1. Update the groupid and the version of the openapi-generator artifact, to do this modify the pom of the route /openapi-generator/pom.xml, lines 12 to 14:
<groupId> org.openapitools.xxxxx </groupId>
<artifactId> openapi-generator </artifactId>
<version> 5.2.1 </version>

After this change, build a new openapi-generator artifact, compile and install locally: mvn clean install

Once in our application, in the open api pluging add the following block, just after the executions block:

<dependencies>
<dependency>
<groupId> org.openapitools.xxxxx </groupId>
<artifactId> openapi-generator </artifactId>
<version> 5.2.1 </version>
</dependency>
</dependencies>

After that, execute maven so that the openapi-generator plugin is executed and after that check that the classes of type API, have in the annotation @Api, the tag section.

Now, when launching the application, in the swagger documentation, the empty endpoints should have disappeared. For this to work, in the yaml definition all paths must be correctly labeled.

pd: this is my forked repo:

https://github.com/leoncio44/swagger-codegen

develop branch.

leon cio
  • 366
  • 4
  • 11