Swagger-codegen: codegen doesn't take into account the default values

Created on 14 Jan 2016  ·  3Comments  ·  Source: swagger-api/swagger-codegen

Hi,

Here is my swagger description:

  inline_response_200:
    properties:
      surname:
        type: "integer"
        default: 1
        format: "int32"
      name:
        type: "string"
        default: "lazy"
        enum:
        - clueless
        - lazy
        - adventurous
        - aggressive

And that's what we receive in the generated nodejs app:

exports.newCustomer = function(request) {

  var examples = {};

  examples['application/json'] = {
  "name" : "aeiou",
  "surname" : 123
};

Is that ok, that default values are not taken into consideration? string is the same each time, the numer also the same. Is that possible to make it work?
As we don't want to use hardcoded examples.

Regards,

Nodejs help wanted

Most helpful comment

This is over 2 years old - has anyone found a way around this to provide default values properly? We're trying to annotate our POJOs to take this into account.

All 3 comments

We can use {{{defaultValue}}} mustache tag to fix it. Here is an example: https://github.com/swagger-api/swagger-codegen/blob/3a2ad9e2e446f43a6cd71a0e03e3a88828311480/modules/swagger-codegen/src/main/resources/python/model.mustache#L52

Is that something you have cycle to contribute?

Here is the contributing guidelines: https://github.com/swagger-api/swagger-codegen/blob/master/CONTRIBUTING.md

It looks like this is also an issue for Java/Android code generation, I'm generating based on this yml file for my class_properties file.

  class_properties:
    type: object
    properties:
      class_id:
        type: object
        properties:
          name:
            type: string
            default: "Class ID"
          value:
            type: string
      class_title:
        type: object
        properties:
          name:
            type: string
            default: "Class Title"
          value:
            type: string
      class_instructor_name:
        type: object
        properties:
          name:
            type: string
            default: "Class Instructor Name"
          value:
            type: string
      class_air_date:
        type: object
        properties:
          name:
            type: string
            default: "Class Air Date"
          value:
            type: string
            format: datestring

When I generate the model though, none of the default fields are populated

@ApiModel(description = "")
public class ClassProperties {

  @SerializedName("class_id")
  private ClassPropertiesClassId classId = null;
  @SerializedName("class_title")
  private ClassPropertiesClassTitle classTitle = null;
  @SerializedName("class_instructor_name")
  private ClassPropertiesClassInstructorName classInstructorName = null;
  @SerializedName("class_air_date")
  private ClassPropertiesClassAirDate classAirDate = null;

  /**
   **/
  @ApiModelProperty(value = "")
  public ClassPropertiesClassId getClassId() {
    return classId;
  }
  public void setClassId(ClassPropertiesClassId classId) {
    this.classId = classId;
  }

  /**
   **/
  @ApiModelProperty(value = "")
  public ClassPropertiesClassTitle getClassTitle() {
    return classTitle;
  }
  public void setClassTitle(ClassPropertiesClassTitle classTitle) {
    this.classTitle = classTitle;
  }

  /**
   **/
  @ApiModelProperty(value = "")
  public ClassPropertiesClassInstructorName getClassInstructorName() {
    return classInstructorName;
  }
  public void setClassInstructorName(ClassPropertiesClassInstructorName classInstructorName) {
    this.classInstructorName = classInstructorName;
  }

  /**
   **/
  @ApiModelProperty(value = "")
  public ClassPropertiesClassAirDate getClassAirDate() {
    return classAirDate;
  }
  public void setClassAirDate(ClassPropertiesClassAirDate classAirDate) {
    this.classAirDate = classAirDate;
  }


  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    ClassProperties classProperties = (ClassProperties) o;
    return (this.classId == null ? classProperties.classId == null : this.classId.equals(classProperties.classId)) &&
        (this.classTitle == null ? classProperties.classTitle == null : this.classTitle.equals(classProperties.classTitle)) &&
        (this.classInstructorName == null ? classProperties.classInstructorName == null : this.classInstructorName.equals(classProperties.classInstructorName)) &&
        (this.classAirDate == null ? classProperties.classAirDate == null : this.classAirDate.equals(classProperties.classAirDate));
  }

  @Override
  public int hashCode() {
    int result = 17;
    result = 31 * result + (this.classId == null ? 0: this.classId.hashCode());
    result = 31 * result + (this.classTitle == null ? 0: this.classTitle.hashCode());
    result = 31 * result + (this.classInstructorName == null ? 0: this.classInstructorName.hashCode());
    result = 31 * result + (this.classAirDate == null ? 0: this.classAirDate.hashCode());
    return result;
  }

  @Override
  public String toString()  {
    StringBuilder sb = new StringBuilder();
    sb.append("class ClassProperties {\n");

    sb.append("  classId: ").append(classId).append("\n");
    sb.append("  classTitle: ").append(classTitle).append("\n");
    sb.append("  classInstructorName: ").append(classInstructorName).append("\n");
    sb.append("  classAirDate: ").append(classAirDate).append("\n");
    sb.append("}\n");
    return sb.toString();
  }
}

Is this also a known issue?

This is over 2 years old - has anyone found a way around this to provide default values properly? We're trying to annotate our POJOs to take this into account.

Was this page helpful?
0 / 5 - 0 ratings