Skip to content

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

@aparna-gh

Description

@aparna-gh
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?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions