OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.35k stars 6.46k forks source link

[BUG][Java][SPRING] Binary Post Requests Generates Delegates With Wrong Parameters - Compilation Error #12453

Open d-j-kendall opened 2 years ago

d-j-kendall commented 2 years ago

Bug Report Checklist

Description

When using the delegate pattern, delegates are generated with incorrect types for requests which accept file parameters.

You can see the following below, the delegate is generated accepting a MultipartFile while the controller passes a Resource.

Controller Code:

    default ResponseEntity<Void> uploadFile(
        @Parameter(name = "body", description = "", schema = @Schema(description = "")) @Valid @RequestBody(required = false) org.springframework.core.io.Resource body
    ) {
        return getDelegate().uploadFile(body);
    }

Delegate Code:

    default ResponseEntity<Void> uploadFile(MultipartFile body) {
        return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);

    }
openapi-generator version

I have found related issues, but every version I have tried contains this bug: 6.0.0-beta 5.4.0 5.1.0 <-- found similar tickets that said it should not exists here (Seems like the GET version of this issue has been fixed)

OpenAPI declaration file content or url

You can find the issue reproduced in this project here spring resource bug

Generation Details

I am simply using mvn clean compile here is the pom.xml

Steps to reproduce
  1. Use the delegate generator config option
  2. Create a request which accept the following as a request body:
      requestBody:
        content:
          application/octet-stream:
            schema:
              type: string
              format: binary
  3. Watch your builds fail running mvn clean compile due to type mismatches between delegates and controllers
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/9462 https://github.com/OpenAPITools/openapi-generator/issues/3905 <-- this issue states it was fixed in v4.0.0-beta3 but maybe it was reintroduced or the beta never became a stable release?

Suggest a fix

I have found two work around both of which do not conform to OAS3, but I have found to at least get past build errors:

Solution 1 Custom Type:

  1. Declare this in your spec -
      requestBody:
        content:
          application/octet-stream:
            schema:
              type: custom-type-name
  1. Add this to your type mappings in pom.xml
    <typeMappings>custom-type-name=org.springframework.core.io.Resource</typeMappings>

Solution 2 Codegen Type: Use this in your spec

      requestBody:
        content:
          application/octet-stream:
            schema:
              type: file
              format: binary

This works because this line in SpringCodegen.java typeMapping.put("file", "org.springframework.core.io.Resource");

I suspect the solution would be to fix the code generating delegate interfaces.

jrpedrianes commented 2 months ago

It seems that this bug is already fixed in the release https://github.com/OpenAPITools/openapi-generator/releases/tag/v7.7.0 thanks to the pull https://github.com/OpenAPITools/openapi-generator/pull/18509