Search code examples
javaspring-bootmavenopenapiswagger-maven-plugin

Plugin io.swagger.core.v3 swagger-maven-plugin generating an empty json file


Added swagger-maven-plugin to the project to create OpenApi3 documentation. Here are plugin settings in pom.xml:

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.4.8</version>
        </dependency>

             ...

            <plugin>
                <groupId>io.swagger.core.v3</groupId>
                <artifactId>swagger-maven-plugin</artifactId>
                <version>2.0.10</version>
                <configuration>
                    <resourcePackages>
                        <package>analytics.api</package>
                    </resourcePackages>
                    <outputFileName>generated_swagger_apiDoc</outputFileName>
                    <outputPath>${project.basedir}/generated-swagger</outputPath>
                    <outputFormat>JSONANDYAML</outputFormat>
                    <prettyPrint>true</prettyPrint>
                </configuration>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>resolve</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

I have annotated java class:

{more imports...}
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
{more imports...}

@Tag(
        name = "Needed name",
        description = "API description"
)
@RestController
@RequestMapping("/request")
@RequiredArgsConstructor
public class Example {
    private final Service service;
    private final ObjectMapper objectMapper;

    @Operation(
            summary = "Operation description",
            method = "post"
    )
    @ApiResponse(responseCode = "200", description = "Request successful")
    @ApiResponse(responseCode = "400", description = "Bad request")
    @ApiResponse(responseCode = "500", description = "Internal server error")
    @PostMapping("/request")
    public void method(
            @Parameter(
                    description = "Post request in JSON",
                    required = true,
                    example = "{json request example}"
             )
            @RequestParam("request") String request, HttpServletResponse res) throws IOException {
       //any code...
    }
}

After mvn install i got a file generated_swagger_apiDoc.json, but this is the content:

{
  "openapi" : "3.0.1"
}

I try different versions of springdoc-openapi-ui, on api-doc url there is a good big json file, which have all of my classes. I need export OpenAPI3 specification document to json file, how I can do it?


Solution

  • I was able to find an alternative, but forgot to mention it here. Anyway, that's what I found.

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-ui</artifactId>
        <version>1.3.9</version>
    </dependency>
    

    I created an additional profile and added this to it:

    <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>2.1.8.RELEASE</version>
        <executions>
            <execution>
                <id>pre-integration-test</id>
                <goals>
                    <goal>start</goal>
                </goals>
            </execution>
            <execution>
                <id>post-integration-test</id>
                <goals>
                    <goal>stop</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    <plugin>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-maven-plugin</artifactId>
        <version>1.0</version>
        <executions>
            <execution>
                <phase>integration-test</phase>
                <goals>
                    <goal>generate</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <apiDocsUrl>swaggerAddressHere
            <apiDocsUrl>
            <outputFileName>document-${project.artifactId}.json</outputFileName>
            <outputDir>${project.basedir}/generated-swagger</outputDir>
        </configuration>
    </plugin>
    <plugin>
        <groupId>io.swagger.codegen.v3</groupId>
        <artifactId>swagger-codegen-maven-plugin</artifactId>
        <version>3.0.46</version>
        <executions>
            <execution>
                <phase>post-integration-test</phase>
                <goals>
                    <goal>generate</goal>
                </goals>
                <configuration>
                    <inputSpec>
                        ${project.basedir}/generated-swagger/document-${project.artifactId}.json
                    </inputSpec>
                    <language>html</language>
                    <output>${project.basedir}/generated-swagger</output>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    I think the problem was this:

    <executions>
        <execution>
            <id>pre-integration-test</id>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>post-integration-test</id>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>