Skip to content

[Spring] @JsonTypeInfo property not set to Discriminator propertyName #103

@atgabe

Description

@atgabe

Using Spring codegen on a schema with a discriminator property populates the @JsonTypeInfo annotation with the Discriminator class represented as a string, instead of the propertyName itself.

For example, this schema:

Example:
      type: object
      required:
        - baseType
      discriminator:
        propertyName: baseType
      properties:
        baseType:
          type: string
        otherProp:
          type: string

will generate this class:

/**
 * Example
 */
@Validated

@javax.annotation.Generated(value = "io.swagger.codegen.languages.java.SpringCodegen", date = "2018-06-18T16:55:52.608-04:00[America/New_York]")

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "Discriminator{propertyName='baseType', mapping=null}", visible = true )
@JsonSubTypes({
  
})

public class Example   {

  @JsonProperty("baseType")
  private String baseType = null;
  
  @JsonProperty("otherProp")
  private String otherProp = null;
  
  public Example baseType(String baseType) {
    this.baseType = baseType;
    return this;
  }
 

  /**
   * Get baseType
   * @return baseType
  **/
 
  @ApiModelProperty(required = true, value = "")
  @NotNull
  public String getBaseType() {
    return baseType;
  }

  public void setBaseType(String baseType) {
    this.baseType = baseType;
  }

  public Example otherProp(String otherProp) {
    this.otherProp = otherProp;
    return this;
  }
  
  /**
   * Get otherProp
   * @return otherProp
  **/
 
  @ApiModelProperty(value = "")
  public String getOtherProp() {
    return otherProp;
  }

  public void setOtherProp(String otherProp) {
    this.otherProp = otherProp;
  }

  @Override
  public boolean equals(java.lang.Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Example example = (Example) o;
    return Objects.equals(this.baseType, example.baseType) &&
        Objects.equals(this.otherProp, example.otherProp);
  }

  @Override
  public int hashCode() {
    return Objects.hash(baseType, otherProp);
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class Example {\n");
    
    sb.append("    baseType: ").append(toIndentedString(baseType)).append("\n");
    sb.append("    otherProp: ").append(toIndentedString(otherProp)).append("\n");
    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(java.lang.Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }
}

The point of interest here is with the line:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "Discriminator{propertyName='baseType', mapping=null}", visible = true )

which should read:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "baseType", visible = true )

Metadata

Metadata

Assignees

No one assigned

    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