swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

jaxrs-jersey jersey2 server stub generated with errors

Open focalize-maciej opened this issue 3 years ago • 1 comments

Description

The server code generated by both swagger-codegen-maven-plugin and swagger hub contains compilation and logic errors with target jaxrs-jersey, library jersey2. The errors are specific to file uploads within a multipart/form-data post.

The generate MediaApi.java source file containts compilation error for mismatched parameters as well as wrong annotations. @FormParam annotations should be @FormDataParam instead AFAIK.

Swagger-codegen version

3.0.33

Swagger declaration file content or url
openapi: 3.0.0
info:
  version: 1.0.0
  title: BackOffice DAM API
  description: ''

paths:

  '/media':
    post:
      operationId: uploadMedia
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                fileName:
                  type: string
                fileContent:
                  type: string
                  format: binary
      responses:
        '200':
          description: successful operation

servers:
  # Added by API Auto Mocking Plugin
  # Added by API Auto Mocking Plugin
  - description: SwaggerHub API Auto Mocking
    url: https://virtserver.swaggerhub.com/FocalizeCloud/BackOffice_DAM_API/1.0.0
  - description: SwaggerHub API Auto Mocking
    url: https://virtserver.swaggerhub.com/FocalizeCloud/BackOffice_lookbook/1.0.0
Command line used for generation

I heven't used the command line. I've used the swaggerhub and swagger-codegen-maven-plugin.

Steps to reproduce

Either create a new maven project with the following configuration and above api declaration, or configure in swaggerhub the codegen target to jaxrs-jersey and library to jersey2.

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>focalize-collection</groupId>
  <artifactId>test-swagger-codegen</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>test-swagger-codegen Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>test-swagger-codegen</finalName>
    <plugins>
	    <plugin>
		    <groupId>io.swagger.codegen.v3</groupId>
		    <artifactId>swagger-codegen-maven-plugin</artifactId>
		    <version>3.0.33</version>
		    <executions>
		        <execution>
		            <goals>
		                <goal>generate</goal>
		            </goals>
		            <configuration>
		                <inputSpec>src/main/resources/api.yaml</inputSpec>
		                <language>jaxrs-jersey</language>
		                <configOptions>
		                   <sourceFolder>src/gen/java/main</sourceFolder>
		                   <library>jersey2</library>
		                   <java8>true</java8>
                           <dateLibrary>java8</dateLibrary>
		                </configOptions>
		            </configuration>
		        </execution>
		    </executions>
		</plugin>
    </plugins>
  </build>
</project>
Related issues/PRs
Suggest a fix/enhancement

The generated code:

@POST
    
    @Consumes({ "multipart/form-data" })
    
    @Operation(summary = "", description = "", tags={  })
    @ApiResponses(value = { 
        @ApiResponse(responseCode = "200", description = "successful operation") })
    public Response uploadMedia(@Parameter(description = "")  @FormParam("fileName")  String fileName
,@FormDataParam("fileContent") InputStream fileContentInputStream,
            @FormDataParam("fileContent") FormDataContentDisposition fileContentDetail
,@Context SecurityContext securityContext)
    throws NotFoundException {
        return delegate.uploadMedia(fileName,fileContent,securityContext);
    }

Should instead be:

    @POST
    
    @Consumes({ "multipart/form-data" })
    
    @Operation(summary = "", description = "", tags={  })
    @ApiResponses(value = { 
        @ApiResponse(responseCode = "200", description = "successful operation") })
    public Response uploadMedia(@Parameter(description = "")  @FormDataParam("fileName")  String fileName
,@FormDataParam("fileContent") InputStream fileContentInputStream,
            @FormDataParam("fileContent") FormDataContentDisposition fileContentDetail
,@Context SecurityContext securityContext)
    throws NotFoundException {
        return delegate.uploadMedia(fileName,fileContentInputStream,fileContentDetail,securityContext);
    }

focalize-maciej avatar Mar 02 '22 09:03 focalize-maciej

After some recent changes, the generated code doesn't result in compilation errors, but still fails at runtime with this error: No injection source found for a parameter of type public javax.ws.rs.core.Response. The generated code now has correct annotations for the file content, but not for any additional parameters as form-data. The problem is that they get annotated with @FormParam. If I change them to @FormDataParam in the generated code, it works. Note that the example above doesn't have such additional parameters. The issue https://github.com/swagger-api/swagger-core/issues/3961 seems to be a duplicate, so it already has an example.

focalize-maciej avatar Sep 18 '23 07:09 focalize-maciej