Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 657
satellite satellite Avatar asked Jan 12 '17 15:01

satellite satellite


People also ask

What is the use of Jackson annotations?

This annotation is used to specify a custom deserializer in order to unmarshall a JSON object. Using this annotation, we use a default value for deserializing an unknown enum value. Using this annotation, you can mark a property or a group of properties to be ignored. This is done at the class level.

What is codegen in swagger?

The Swagger Codegen is an open source code-generator to build server stubs and client SDKs directly from a Swagger defined RESTful API. The source code for the Swagger Codegen can be found in GitHub.

How does swagger generate client code?

Method 1: Use the swagger editor Select file , import URL and type in the URL of the swagger endpoint you want to load. Alternatively you can select File , Import File and upload the downloaded swagger. json file. Next select Generate client and choose the language of your choice.


1 Answers

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.

like image 200
dino Avatar answered Sep 18 '22 02:09

dino