swagger-api / swagger-api/swagger-codegen

[SPRING] Multiple Content Types in APIs cause ambiguous mapping.

Open
#10,331 0 comments 0 reactions 1 assignee View on GitHub

@gracekarina is already working on this.

Since Jun 19, 2020.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

When two content types are provided for an operation, the swagger generated API code has two different APIs and they have both content types mentioned under the consumes section. This creates a problem of ambiguous mapping of methods during deployment.

The generated signature has consumes = { "application/merge-patch+json", "application/json-patch+json" } for both the methods. The expected method signatures is to have each method mapped to a single content type as follows:

@RequestMapping(value = "/pet",
    produces = { "application/json" }, 
    consumes = { "application/merge-patch+json" },
    method = RequestMethod.PATCH)
ResponseEntity<Dog> findPet(@ApiParam(value = "" ,required=true )  @Valid @RequestBody Dog body

);

@RequestMapping(value = "/pet",
    produces = { "application/json" }, 
    consumes = { "application/json-patch+json" },
    method = RequestMethod.PATCH)
ResponseEntity<Dog> findPet(@ApiParam(value = "" ,required=true )  @Valid @RequestBody Cat body

);

Swagger-codegen version

3.0.20

Swagger declaration file content or url

openapi: 3.0.0
info:
title: Sample Application
version: 1.0.0

paths:
/pet:
patch:
operationId: findPet
requestBody:
required: true
content:
application/merge-patch+json:
schema:
$ref: '#/components/schemas/Dog'
application/json-patch+json:
schema:
$ref: '#/components/schemas/Cat'
responses:
'200':
description: Found Pet
content:
application/json:
schema:
$ref: '#/components/schemas/Dog'

components:
schemas:
Dog:
allOf:
- type: object
properties:
dogName:
type: string
Cat:
allOf:
- type: object
properties:
catName:
type: string

Command line used for generation
Steps to reproduce

Generated in Swagger Editor Code Generator: Generate Server -> Spring

Generated method has consumes defined as:
consumes = { "application/merge-patch+json", "application/json-patch+json" }

Related issues/PRs
Suggest a fix/enhancement

Probably there is a way to ensure that each overloaded API is mapped only to a single content-type?

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.