Skip to content

[C#] Optional/Required properties #4262

@aleksei-saharov

Description

@aleksei-saharov

Hi, all

I have issue with client C# code generator on http://editor.swagger.io

YAML:

Entity:
    type: object
    required:
      - Values
      - Unit
      - Start
    properties:
      Values:
        description: Array of values.
        type: array
        items:
          type: number
          format: double
      Unit:
        $ref: '#/definitions/MyEnum'
      Start:
        description: Start.
        type: number
        format: double
MyEnum:
    type: string
    enum: [
      val1,
      val2,
      val3]

http://editor.swagger.io generate following code:
C# (unnecessary parts removed):

    public partial class Entity
    {
        protected Entity() { }
        /// <summary>
        /// Initializes a new instance of the <see cref="Entity" /> class.
        /// </summary>
        /// <param name="Values">Array of values. (required).</param>
        /// <param name="Unit">Unit (required).</param>
        /// <param name="Start">Start. (required).</param>
        public Entity(List<double?> Values = null, MyEnum Unit = null, double? Start = null)
        {
            // to ensure "Values" is required (not null)
            if (Values == null)
            {
                throw new InvalidDataException("Values is a required property for Entity and cannot be null");
            }
            else
            {
                this.Values = Values;
            }
            // to ensure "Unit" is required (not null)
            if (Unit == null)
            {
                throw new InvalidDataException("Unit is a required property for Entity and cannot be null");
            }
            else
            {
                this.Unit = Unit;
            }
            // to ensure "Start" is required (not null)
            if (Start == null)
            {
                throw new InvalidDataException("Start is a required property for Entity and cannot be null");
            }
            else
            {
                this.Start = Start;
            }
        }
        
        /// <summary>
        /// Array of values.
        /// </summary>
        /// <value>Array of values.</value>
        [DataMember(Name="Values", EmitDefaultValue=false)]
        public List<double?> Values { get; set; }
        /// <summary>
        /// Gets or Sets Unit
        /// </summary>
        [DataMember(Name="Unit", EmitDefaultValue=false)]
        public MyEnum Unit { get; set; }
        /// <summary>
        /// Start.
        /// </summary>
        /// <value>Start.</value>
        [DataMember(Name="Start", EmitDefaultValue=false)]
        public double? Start { get; set; }
}

Problems:

  1. MyEnum set as nonoptional. Everithing is good with property declaration but in constructor default value is null (not compiled).
    In C# there is operator default. Generator can work in following way:
    public Entity(List<double?> Values = default(List<double?>), MyEnum Unit = default(MyEnum), double? Start = default(double?))
    It will be compiled.
    a) P.S.: If enum not set as required (should be optional) code generator do not generate "?" near property declaration. Enum property has non nullable type even if property should be optional. But constructor validation for NULL are not generated (it is correct).

  2. Start is required but in property declaration it has double? type (optional). Constructor use validation and exception, but it will be better to:
    a) declare Start as double and constructor parameter too;
    b) add RequiredAttribute to property declaration

  3. Values:
    a) should follow 2.b mentioned.
    b) Validation in constructor should be because there is no possibilities to make List<> not null
    c) How configure List items oplionality (no ways I see)?

  4. Overall with following 2.a: If property is require but declared as optional and validation added only in constructor there will be possibility to set it NULL using property setter. Not good 👎 If fix 2.a. this problem will disappear.

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