Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

codegen doesn't take into account the default values #1892

Open
PavelPolyakov opened this issue Jan 14, 2016 · 3 comments
Open

codegen doesn't take into account the default values #1892

PavelPolyakov opened this issue Jan 14, 2016 · 3 comments

Comments

@PavelPolyakov
Copy link

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,

@wing328
Copy link
Contributor

wing328 commented Jan 15, 2016

We can use {{{defaultValue}}} mustache tag to fix it. Here is an example:

self._{{name}} = {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}None{{/defaultValue}}

Is that something you have cycle to contribute?

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

@andreathacker
Copy link

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?

@mustafashabib
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants