Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
palatam opened this issue Jan 29, 2025 · 1 comment

Comments

@palatam
Copy link

palatam commented Jan 29, 2025

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
@palatam
Copy link
Author

palatam commented Jan 29, 2025

Workaround:

openapi.yaml:

      requestBody:
        required: true
        content:
          application/zip:
            schema:
              $ref: '#/components/schemas/SpringMultipartFile'
components:
  schemas:
    SpringMultipartFile:
      type: multipartFile
      description: 'Workaround for https://github.com/OpenAPITools/openapi-generator/issues/20555 (also see pom.xml for type mappings)'

pom.xml:

<typeMappings>multipartFile=org.springframework.web.multipart.MultipartFile</typeMappings>
<importMappings>
    <importMapping>MultipartFile=org.springframework.web.multipart.MultipartFile</importMapping>
</importMappings>
<languageSpecificPrimitives>MultipartFile</languageSpecificPrimitives>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant