swagger-api / swagger-api/swagger-codegen-generators

Issue with generated Java server code, overloads of paths not differentiated by consumes.

Open
#384 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
299
Forks
439
PR merge metrics
No merged PRs in 30d

Description

We have an endpoint which you can use to upload a file to with content type 'multipart/form-data', which worked fine.

But we a requirement to have the option to provide a URL to a file rather than upload it, and so added an alternative 'text/plain' content to the requestBody. The code generated then emits two methods - the existing one taking a File one and a new taking a String.

However both of these endpoints have the same annotation:
@RequestMapping(value = "/", consumes = { "multipart/form-data", "text/plain" }
where as we'd expect one to have "multipart/form-data" and the other "text/plain".

This understandably breaks the app on startup, as it is ambiguous which should handle which request.

java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'defaultApiController' method 
public default org.springframework.http.ResponseEntity<java.lang.Void> com.ghyston.deg.springfoxerror.api.DefaultApi.upload(java.lang.String)
to {POST /, consumes [multipart/form-data || text/plain]}: There is already 'defaultApiController' bean method
public default org.springframework.http.ResponseEntity<java.lang.Void> com.ghyston.deg.springfoxerror.api.DefaultApi.upload(java.io.File) mapped.

Full OpenAPI spec:


openapi: 3.0.0
info:
  version: 1.0.0
  title: Sample API
tags:
  - name: Upload
paths:
  /:
    post:
      tags:
        - Upload
      summary: |
        Upload a zip
      operationId: upload
      requestBody:
        required: true
        content:
          'multipart/form-data':
            schema:
              type: object
              properties:
                content:
                  type: string
                  format: binary
            encoding:
              content:
                contentType: 'application/zip'
          'text/plain':
            schema:
              type: string
              format: uri
              example: https://example.com/article.zip
      responses:
        '200':
          description: Success!

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 reproducing the supplied OpenAPI specification with the Java Spring server generator and inspect the generated DefaultApi methods and their @RequestMapping annotations. Trace how the requestBody content types are passed into the generated methods; done means the multipart/form-data and text/plain overloads have distinct mappings and the application starts without an ambiguous mapping error.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.