1

I have an API that I'm trying to rewrite that uses the old swagger-maven-codegen 2.3.1 with a legacy version of Spring 1.5.9. I can see that when I run mvn package or mvn install , the plugin generates the interfaces that all the Controllers implement. All of this is of course defined in swagger.yaml file.

I followed the swagger codegen instructions per their github repo (https://github.com/swagger-api/swagger-codegen/tree/master/modules/swagger-codegen-maven-plugin) and it simply states to add it to the build -> plugins section of the pom.xml. So, this works fine with the codegen version 2.3.1 that they use in their example, but does NOT work with version >= 3.0.0. Now, you might tell me "hey just use 2.3.1 then" but I want to use openapi spec 3, which upon my research has to have swagger codegen version >= 3.0.0. Reference:

How to fix java.lang.RuntimeException: missing swagger input or config?

Now, normally you just update the plugin version in the pom, but for some reason, my code never compiles or runs when I use a plugin version of >= 3.0.0. I get this error message in intelliJ:

The POM for io.swagger:swagger-codegen-maven-plugin:jar:3.0.0 is missing, no dependency information available

So they led me to maybe believe my group, artifact or repo was wrong, so I started trying to find on the mavencentral URL the exact path of this JAR, and I did notice that I didn't see any version for 3.X.X except these two-variants of 3.0.0-rc0 and 3.0.0-rc1, which you can see here: https://repo1.maven.org/maven2/io/swagger/swagger-codegen-maven-plugin/

Lo and behold, when I tried 3.0.0-rc1 in my pom version, my code started compiling. I do have sample swagger api.yaml file that I am reading in (very basic just for testing), and then I started seeing now okhttp packages missing or that I missing that in my pom. However, I do see it attempting to generate the server stub in the /target output direct that I defined in my pom.xml configuration for the plugin, so that is good: enter image description here

So I guess my questions at this point to summarize aka TL;DR are:

  1. what is the openapi I should be using - 2 or 3? 3 is newer, so I figured this was obvious
  2. if it is openapi 3, how do I use this with swagger codegen plugin version >= 3 in my pom? why can't I resolve the dependency? is my lacking understanding of dependencies vs plugin dependencies here, hurting me?
  3. when I do put in this 3.0.0-rc1 swagger codegen version, why am I getting all these okhttp errors? Is there a guide somewhere to show the END-TO-END complete setup needed to use this plugin?
  4. even in my old code, The @Controllers are implementing Interfaces that get generated in the /target directory. I don't see their interface names defined in the swagger.yaml so I'm wondering what design pattern this is OR where in the swagger documentation are these instructions?

Anyway, so yes, if someone could please help me out or explain some of these things or point me to an article that actually shows how to get an API up using swagger codegen from beginning to end , I would greatly appreciate it!

My 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.mydomain.me</groupId>
    <artifactId>my-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>My API</name>
    <description>Version 2.0 of my API</description>
    <properties>
        <java.version>11</java.version>
    </properties>

    <pluginRepositories>
        <pluginRepository>
            <id>central</id>
            <name>Maven Central</name>
            <layout>default</layout>
            <url>https://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.swagger.codegen.v3</groupId>
            <artifactId>swagger-codegen-cli</artifactId>
            <version>3.0.25</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>io.swagger</groupId>
                <artifactId>swagger-codegen-maven-plugin</artifactId>
                <version>3.0.0-rc1</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/api.yaml</inputSpec>
                            <output>${project.basedir}/target/generated-sources</output>
                            <language>java</language>
                            <configOptions>
                                <sourceFolder>src/gen/java/main</sourceFolder>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

I can also share my swagger.yaml if you would like to see that.

andrewJames
  • 19,570
  • 8
  • 19
  • 51
ennth
  • 1,698
  • 5
  • 31
  • 63

0 Answers0