autorest icon indicating copy to clipboard operation
autorest copied to clipboard

Derived class constructors are missing parameters for base class properties

Open bitsforthought opened this issue 11 years ago • 2 comments

Given this Swagger:

"ScoringFunction": {
  "discriminator": "type",
  "properties": {
    "type": {
      "type": "string"
    },
    "fieldName": {
      "type": "string"
    },
    "boost": {
      "type": "number",
      "format": "double"
    },
    "interpolation": {
      "$ref": "#/definitions/ScoringFunctionInterpolation"
    }
  },
  "required": [
    "type"
  ]
},
"DistanceScoringFunction": {
  "allOf": [
    {
      "$ref": "#/definitions/ScoringFunction"
    },
    {
      "properties": {
        "distance": {
          "$ref": "#/definitions/DistanceScoringParameters"
        }
      }
    }
  ]
},

AutoRest generates the following non-default constructors in C#.

For ScoringFunction:

    public ScoringFunction(string fieldName = default(string), double? boost = default(double?), ScoringFunctionInterpolation? interpolation = default(ScoringFunctionInterpolation?))
    {
        FieldName = fieldName;
        Boost = boost;
        Interpolation = interpolation;
    }

For DistanceScoringFunction:

    public DistanceScoringFunction(DistanceScoringParameters distance = default(DistanceScoringParameters))
    {
        Distance = distance;
    }

The DistanceScoringFunction constructor ought to include the parameters from its base class counterpart in ScoringFunction.

bitsforthought avatar Nov 11 '15 00:11 bitsforthought

Further to this, if you mark the derived class properties as required in the Swagger spec, AutoRest does not emit non-defaulted parameters for those properties in the constructor. It does for required base class properties.

EDIT

This particular symptom turned out to be my fault. The "requires" element was misplaced in the Swagger spec. If you do this:

"shark": {
  "allOf": [
    {
      "$ref": "#/definitions/Fish"
    }
  ],
  "required": [ "birthday" ],
  "properties": {
    "birthday": {
      "type": "string",
      "format": "date-time"
    }
  }
}

You get correct generated code. But, if you do this:

"shark": {
  "allOf": [
    {
      "$ref": "#/definitions/Fish"
    },
    {
      "required": [ "birthday" ],
      "properties": {
        "birthday": {
          "type": "string",
          "format": "date-time"
        }
      }
    }
  ]
}

You get an optional parameter for "birthday" instead of a required one.

bitsforthought avatar Nov 11 '15 18:11 bitsforthought

I'm going to try to fix this.

bitsforthought avatar Nov 17 '15 00:11 bitsforthought