swagger-api / swagger-api/swagger-codegen

[Spring] Improve CI and maven integration: regenerate all files depending on swagger specs at every build

Open
#5,542 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

As a best practice for code generation with a CI pipeline driven by maven, it should be able to regenerate every file that depends on Swagger spec file.

Speaking about Spring Boot, these files are:

  • API Interfaces
  • Model classes
  • SwaggerDocumentationConfig.java (defines API info based on the Swagger specs)

Using Maven, these files should be regenerated at every build, into ./target/generated-sources (usually ignored in VCS).

Any other file might be optionally generated, on demand (as opposed as automatically at every build), only on the developer's machine and into ./src.

Currently, this only partially works using the swagger-codegen-maven-plugin and spring language.

Specifyng interfaceOnly=true, the following files are generated:

  • API Interfaces
  • Model classes
  • pom.xml
  • README.md
  • .swagger-codegen-ignore

By default, swagger-codegen-maven-plugin correctly generates into ./target/generated-sources

I see two distinct problems here:

  1. pom.xml, README.md, .swagger-codegen-ignore should not be generated when interfaceOnly=true (and it also doen't make sense generating them into ./target/generated-sources)
  2. It should be possible to regnerate also SwaggerDocumentationConfig.java

The .swagger-codegen-ignore file is not useful, as you only may ignore files, not selectively activate their generation.

Swagger-codegen version

Verified using Swagger Codegen Maven plugin 2.2.2 (release) and CLI 2.2.2

Swagger declaration file content or url

It does not depend on Swagger specs. I used this for testing: https://github.com/OAI/OpenAPI-Specification/blob/master/examples/v2.0/json/petstore-minimal.json

Command line used for generation

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>example</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>sample</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <application.package>com.example</application.package>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.13</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>2.2.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>src/main/resources/swagger.json</inputSpec>
                            <language>spring</language>
                            <configOptions>
                                <interfaceOnly>true</interfaceOnly>
                                <dateLibrary>java8</dateLibrary>
                                <java8>true</java8>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

The effect is reproducible with the following CLI, but as the build is controlled by Maven, only the Maven plugin should be used.

swagger-codegen -l spring -i src/main/resources/swagger.json -o src/ -c config.json

using the following config.json:

{
  "dateLibrary" : "java8",
  "java8" : true,
  "interfaceOnly" : true  
}
Steps to reproduce
  1. Create a maven project using the pom.xml file above
  2. Copy a specs file into ./src/main/resources/ project subdirectory
  3. run mvn clean generate-sources
Related issues

None AFAIK

Suggest a Fix

The current SpringCodegen.java implementation might be easily modified not to generate pom.xml, README.md, .swagger-codegen-ignore when interfaceOnly=true

Selectively regenerating SwaggerDocumentationConfig.java might need a separage, language specific option.

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 by reproducing the issue with the provided pom.xml, swagger.json, config.json, and mvn clean generate-sources. Read SpringCodegen.java to trace the interfaceOnly=true path and generated files. Done means Maven generation produces the requested API interfaces, model classes, and SwaggerDocumentationConfig.java under target/generated-sources without the unwanted project files.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, build-system
Issue type
Feature
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.