swagger-api / swagger-api/swagger-codegen

Ver 3.0.0-rc1 generates code with missing imports when using enum

Open
#8,642 8 comments 5 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

Using Maven, I try to generate an API and classes in Java for my API. I use an enum and the generated code is missing these imports:
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

It has correctly imported:
import com.fasterxml.jackson.annotation.JsonProperty;

Swagger-codegen version

3.0.0-rc1
via Maven

Swagger declaration file content or url
---
swagger: '2.0'

################################################################################
#                              API Information                                 #
################################################################################
info:
  version: 1.0.0
  title: My API
  description: My test API.
  contact:
    url: http://undefined.undefined
    email: undefined@undefined.undefined

################################################################################
#                  Host, Base Path, Schemes and Content Types                  #
################################################################################
host: my-api

basePath: /my/api/v1.0.0
schemes:
  - http
produces:
  - application/json
consumes:
  - application/json

################################################################################
#                                    Paths                                     #
################################################################################

paths:
  '/consenttemplate':
    post:
      parameters:
        - name: body
          in: body
          required: true
          schema:
            $ref: '#/definitions/MyAPI'
      responses:
        200:
          schema:
           $ref: '#/definitions/MyAPI'

################################################################################
#                                 Definitions                                  #
################################################################################
definitions:
 MyAPI:
    type: object
    properties:
      uuid:
        type: string
      MyAPIState:
        type: string
        enum: [ DRAFT, APPROVED, ACTIVE, EXPIRED, CLOSED ]
Command line used for generation

mvn install
using this pom.xml:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>dk.myapi</groupId>
  <artifactId>my-api</artifactId>
  <packaging>jar</packaging>
  <name>my-api</name>
  <version>1.0.0</version>
  <build>
  <plugins>
	<plugin>
		<groupId>io.swagger</groupId>
		<artifactId>swagger-codegen-maven-plugin</artifactId>
		<version>3.0.0-rc1</version>
		<executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
			<phase>generate-sources</phase>
            <configuration>
                <language>jaxrs-spec</language>
				<inputSpec>my-api.yaml</inputSpec>
				<output>generated-sources</output>
                <typeMappings>
					<typeMapping>DateTime=Instant</typeMapping>
                </typeMappings>
                <importMappings>
					<importMapping>Instant=java.time.Instant</importMapping>
                </importMappings>
                <configOptions>
					<apiPackage>dk.myapi.common</apiPackage>
                    <modelPackage>dk.myapi.common.model</modelPackage>
                    <sourceFolder>src/java/main</sourceFolder>
    			    <interfaceOnly>true</interfaceOnly>
                    <generatePom>false</generatePom>
                    <java8>true</java8>
					<DuseBeanValidation>true</DuseBeanValidation>
                </configOptions>
				<configHelp></configHelp>
            </configuration>
        </execution>
		</executions>
	</plugin>	
 
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.9.1</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>
                                src/main/java</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    </plugins>
  </build>
  <dependencies>
   <dependency>
     <groupId>com.fasterxml.jackson.core</groupId>
     <artifactId>jackson-annotations</artifactId>
     <version>2.9.6</version>
     <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>javax.ws.rs</groupId>
      <artifactId>javax.ws.rs-api</artifactId>
      <version>2.0</version>
      <scope>provided</scope>
    </dependency>
    <dependency>
      <groupId>io.swagger</groupId>
      <artifactId>swagger-annotations</artifactId>
      <scope>provided</scope>
      <version>1.5.21</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit-version}</version>
      <scope>test</scope>
    </dependency>
    <!-- Bean Validation API support -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
        <scope>provided</scope>
    </dependency>
  </dependencies>
  <properties>
    <junit-version>4.8.1</junit-version>
  </properties>
</project>
Steps to reproduce

Save the attached yaml file as my-api.yaml and the pom-file as pom.xml in the same directory.
With Maven 3.5.0 on the PATH and Java 1.8.0_102 run:
mvn install

This fails with:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project my-api: Compilation failure: Compilation failure:
[ERROR] /C:/Users/me/generated-sources/src/java/main/io/swagger/model/MyAPI.java:[39,6] cannot find symbol
[ERROR]   symbol:   class JsonValue
[ERROR]   location: class io.swagger.model.MyAPI.MyAPIStateEnum
[ERROR] /C:/Users/me/generated-sources/src/java/main/io/swagger/model/MyAPI.java:[44,6] cannot find symbol
[ERROR]   symbol:   class JsonCreator
[ERROR]   location: class io.swagger.model.MyAPI.MyAPIStateEnum

When inspecting the file referred in the error message, it is possible to see that two annotations are used but not imported. There also is a lot of blank lines in the section which contains imports, which is a bit suspecious.

Workaround
  1. Have a step that generates the sources
  2. Have a script insert the missing imports
  3. Run mvn install without generating new sources.

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

Reproduce the failure with my-api.yaml and pom.xml by running mvn install, then inspect the generated-sources/src/java/main/io/swagger/model/MyAPI.java imports and enum annotations. Done means the generated Java source includes JsonCreator and JsonValue imports and Maven compilation succeeds without the workaround.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.