简体   繁体   中英

swagger-codegen client: How to include jackson annotations on models

I'm using swagger-codegen to generate a rest client, but I get a problem, the service I'm consuming returns a model with an inheritance, the API model looks like this:

public class Person
{
    private List<Book> books;
    ...
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "typeClass")
@JsonSubTypes({ @JsonSubTypes.Type(value = Magazine.class) })
public class Book 
{
    //some prop
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "typeClass")
public class Magazine extends Book
{
    //some prop
}

The API model is annotated with jackson annotations to deal with inheritance. The API works ok. When I generate the client, the client models don't have the jackson annotations, so when I use the generated client to consume the API, it always deserializate using the Book class. It doesn't "see" the Magazine class. I think it's because the generated model does'nt have the jackson annotations to deal with inheritance.

How can I config the swagger-codegen to add the jackson annotations to the model.

Thanks a lot...

Instead of using

    java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate 
   -i http://petstore.swagger.io/v2/swagger.json   
    -l java
    -o samples/client/petstore/java

you can work with

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate 
-i http://petstore.swagger.io/v2/swagger.json   
-l spring
-o samples/client/petstore/java

Through the language change they also swap out gson to Jackson.

In your case you wwould have something like

import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.model.Category;
import io.swagger.model.Tag;
import java.util.ArrayList;
import java.util.List;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.*;

/**
 * Pet
 */
@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2020-06-04T13:33:18.844+02:00")




public class Pet   {
  @JsonProperty("id")
  private Long id = null;

  @JsonProperty("category")
  private Category category = null;

  @JsonProperty("name")
  private String name = null;

  @JsonProperty("photoUrls")
  @Valid
  private List<String> photoUrls = new ArrayList<String>();

  @JsonProperty("tags")
  @Valid
  private List<Tag> tags = null;
      ...

source: https://github.com/swagger-api/swagger-codegen/issues/5785

When you build the client with Generator.Swagger ( https://generator.swagger.io/ the online swagger Codegen api:/gen/clients/{language}) then you can look through the settings in /gen/clients/{language} or https://openapi-generator.tech/docs/generators/java/ and build your request up to that.

Add the following line of code in your plugin configuration in pom.xml

<'library>$libraryValue<'/library>

$libraryValue can be based on which library you are using in your dependencies. Either jersey one if you are using Jersey 1.xx or jersey2 if you are using Jersey 2.xx

Thanks https://stackoverflow.com/users/3695939/moondaisy for the headstart that you have provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM