swagger-api / swagger-api/swagger-codegen

[JavaSpringBoot] Delegate pattern generates invalid code for octet-stream

Open
#10,899 0 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

The maven code generator v.3 generates different types for the API and the delegate. The API gets an Object as the body type and the delegate gets the MultipartFile type.
Both should be of type Resource I think.

Swagger-codegen version

io.swagger.codegen.v3,
swagger-codegen-maven-plugin,
v.3.0.24

Swagger declaration file content or url
openapi: '3.0.2'
info:
  title: Test API
  version: "0.0.1"
servers:
  - url: http://localhost:8080/test  
paths:
  /upload:
    put:
      summary: "Upload content"
      operationId: apiUpload
      requestBody:
        required: true
        content:
          application/octet-stream:
            schema:
              type: string
              format: binary
      responses:
        '200':
          description: OK
          content:
            application/octet-stream:
              schema:
                type: string
                format: binary     

Generated API code. The body is of type Object but should be (I think) of type Resource.

    @Operation(summary = "Upload content", description = "", tags={  })
    @ApiResponses(value = { 
        @ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = Resource.class))) })
    @RequestMapping(value = "/upload",
        produces = { "application/octet-stream" }, 
        consumes = { "application/octet-stream" }, 
        method = RequestMethod.PUT)
    default ResponseEntity<Resource> apiUpload(@Parameter(in = ParameterIn.DEFAULT, description = "", required=true, schema=@Schema()) @Valid @RequestBody Object body) {
        return getDelegate().apiUpload(body);
    }

Generated delegate code. Body is of type MultipartFile but should be of type Resource.

default ResponseEntity<Resource> apiUpload(MultipartFile body) {
Command line used for generation
         <plugin>
             <groupId>io.swagger.codegen.v3</groupId>
             <artifactId>swagger-codegen-maven-plugin</artifactId>
             <version>3.0.24</version>
             <executions>
                 <execution>
                     <goals>
                         <goal>generate</goal>
                     </goals>
                     <configuration>
                         <inputSpec>${project.basedir}/src/main/resources/openapi/testapi.yml</inputSpec>
                         <language>spring</language>
                         <configOptions>
                        <sourceFolder>src/gen3/java/main</sourceFolder>
	                            <modelPackage>${default.package}.swagger.test.model</modelPackage>
	                            <apiPackage>${default.package}.swagger.test.api</apiPackage>
	                            <invokerPackage>${default.package}.swagger.test.invoker</invokerPackage>
                                <withXml>true</withXml>
                                <java8>true</java8>
                                <dateLibrary>java8</dateLibrary>
                                <library>spring-boot</library>
                                <delegatePattern>true</delegatePattern>
                                <interfaceOnly>false</interfaceOnly>
                         </configOptions>
                     </configuration>
                 </execution>
             </executions>
         </plugin>
Steps to reproduce

Generate the code using maven, e.g. mvn clean generate-sources.

Related issues/PRs

https://github.com/swagger-api/swagger-codegen/issues/9537

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 with the OpenAPI declaration and the swagger-codegen-maven-plugin configuration using the spring-boot library and delegatePattern, then reproduce the output with mvn clean generate-sources. Compare the generated API and delegate signatures for the application/octet-stream body; done means both use the intended Resource type consistently.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring-boot
Domain
api, backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.