swagger-api / swagger-api/swagger-codegen

Server code generation for Java SpringBoot Jersey does not support polymorphism and inheritence

Open
#11,922 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

The code generated from the swagger does not have the correct annotations for polymorphism and inheritance. The generated code for a type is missing the Json annotations for the discriminator:

@JsonTypeInfo
@JsonSubTypes
@JsonTypeId

The generated code is this:

@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaJerseyServerCodegen", date = "2022-09-26T14:24:11.745-06:00[America/Denver]")
public class AlbumID {
@JsonProperty("objectType")
private String objectType = null;

But should be this:

@Validated
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY,
property = "objectType", visible = true)
@JsonSubTypes([{@JsonSubTypes.Type(value](mailto:%7b@JsonSubTypes.Type(value) = Album.class, name = "Album"),
@JsonSubTypes.Type(value = AlbumDetail.class, name = "AlbumDetail"),})
@javax.annotation.Generated(value = "io.swagger.codegen.v3.generators.java.JavaJerseyServerCodegen",
date = "2022-09-23T10:55:06.626-06:00[America/Denver]")
public class AlbumID {
@JsonTypeId
private String objectType = null;

The generated code for Java SpringBoot spring-web is correct with the annotations.

See the attached swagger. The AlbumID, ArtistID, and SongID types all have a discriminator for polymorphism and inheritance.

Swagger-codegen version

3.0.34

Swagger declaration file content or url
Command line used for generation

Maven build using swagger-codegen-maven-plugin version 3.0.34

Steps to reproduce

[mp3player-model-springboot-jersey.zip](https://github.com/swagger
mp3player-model-springboot-springweb.zip
-api/swagger-codegen/files/9756143/mp3player-model-springboot-jersey.zip)

There are two attached zip files of Maven projects. One is the correctly generated spring-web, the other the incorrectly generated Jersey. See the AlbumID, ArtistID, and SongID Java source files in each to compare.

The swagger yaml file is located in the src/main/resources/swagger directory, file name is ODBCModel-MP3Resource-1.0.0-resolved.yaml

Related issues/PRs
Suggest a fix/enhancement

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

Compare the generated AlbumID, ArtistID, and SongID Java source files in the attached Jersey and spring-web Maven projects, using src/main/resources/swagger/ODBCModel-MP3Resource-1.0.0-resolved.yaml as the input. Start by tracing JavaJerseyServerCodegen and the corresponding SpringBoot generation, then verify that the Jersey output handles the discriminator annotations like the spring-web output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
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.