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

[Spring] duplicated disciminator field in REST response when existing property is used for polimorphism

Open
#459 1 comment 1 reaction 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

I am using swagger-codegen-maven-plugin version: 2.4.8
If discriminator and property exists with the same name. Spring rest response has duplicated fields.

for such definition:
"Pet": { "type": "object", "required": [ "name", "petType" ], "properties": { "name": { "type": "string" }, "petType": { "type": "string" } }, "discriminator": "petType", }

it is generated:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@type", visible = true )

JsonTypeInfo.As.PROPERTY is always generated(it is not parametrized anyway) It would be better to generate in such case:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "@type", visible = true )

So fields won't be duplicated in rest response.

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

Reproduce the generated Spring REST response with swagger-codegen-maven-plugin 2.4.8 and the Pet definition in the issue. Locate the Spring generator or template that emits @JsonTypeInfo, then verify the existing discriminator property is not serialized twice. Done means the response contains the discriminator field only once.

Written by the indexing model from the issue text.

Assessment

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