OpenAPITools / OpenAPITools/openapi-generator

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

Open
#12,453 2 comments 2 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
  • [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

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.

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 linked min_spec.yml and SpringCodegen.java, then reproduce the delegate generation using the delegate option and the binary request body. Compare the generated controller and delegate parameter types, and run mvn clean compile from the reproduction project. Done means the generated signatures agree and compilation succeeds without custom type mappings.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.