OpenAPITools / OpenAPITools/openapi-generator

[BUG][spring][spring-boot] org.springframework.core.io.Resource cannot be converted to org.springframework.web.multipart.MultipartFile

Open
#20,555 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist

This is a dup of #18345 because #18345 is closed but is still present (7.11.0)

Description
  /validate:
    post:
      description: 'Validate Config ZIP'
      operationId: 'validate-config-zip'
      tags:
        - 'config'
      responses:
        200:
          description: 'OK'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ValidateConfigResult'
      requestBody:
        required: true
        content:
          application/zip:
            schema:
              type: string
              format: binary

Generates invalid java code:

    /**
     * POST /validate
     * Validate Config ZIP
     *
     * @param body  (required)
     * @return OK (status code 200)
     */
    @Operation(
        operationId = "validateConfigZip",
        description = "Validate Config ZIP",
        tags = { "config" },
        responses = {
            @ApiResponse(responseCode = "200", description = "OK", content = {
                @Content(mediaType = "application/json", schema = @Schema(implementation = ValidateConfigResultModel.class))
            })
        }
    )
    @RequestMapping(
        method = RequestMethod.POST,
        value = "/validate",
        produces = { "application/json" },
        consumes = { "application/zip" }
    )
    
    default ResponseEntity<ValidateConfigResultModel> _validateConfigZip(
        @Parameter(name = "body", description = "", required = true) @Valid @RequestBody org.springframework.core.io.Resource body
    ) {
        return validateJourneyConfigVersionZip(body);
    }

    // Override this method
    default  ResponseEntity<ValidateConfigResultModel> validateConfigZip(MultipartFile body) {
        getRequest().ifPresent(request -> {
            for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
                if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
                    String exampleString = "{ \"schemaValidationErrors\" : [ \"schemaValidationErrors\", \"schemaValidationErrors\" ], \"resourceInitializationErrors\" : [ \"resourceInitializationErrors\", \"resourceInitializationErrors\" ] }";
                    ApiUtil.setExampleResponse(request, "application/json", exampleString);
                    break;
                }
            }
        });
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }

incompatible types: org.springframework.core.io.Resource cannot be converted to org.springframework.web.multipart.MultipartFile

As seen above, RequestMapping method uses Spring's Resource object but the delegate method expects a MultipartFile object which leads to a compiler error.

openapi-generator version

7.2.0
7.7.0
7.11.0

OpenAPI declaration file content or url
Generation Details
<generatorName>spring</generatorName>
<library>spring-boot</library>
Steps to reproduce
Related issues/PRs

#18345
#12453

Suggest a fix

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 reviewing the Spring generator's spring-boot output for the shown /validate operation, then compare the behavior described in related issues #18345 and #12453. Done means the generated Java code uses compatible parameter types and compiles for an application/zip request body.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.