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][Java][SPRING] Binary Post Requests Generates Delegates With Wrong Parameters - Compilation Error #12453

Open
1 task
d-j-kendall opened this issue May 25, 2022 · 1 comment

Comments

@d-j-kendall
Copy link

d-j-kendall commented May 25, 2022

Bug Report Checklist

  • [x ] Have you provided a full/minimal spec to reproduce the issue?
  • [x ] Have you validated the input using an OpenAPI validator (example)?
  • [ x] Have you tested with the latest master to confirm the issue still exists?
  • [x ] Have you searched for related issues/PRs?
  • [x ] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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
  1. Watch your builds fail running mvn clean compile due to type mismatches between delegates and controllers
Related issues/PRs

#9462
#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
Copy link

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 #18509

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

2 participants