Forum Discussion

alexossisos's avatar
alexossisos
New Member
3 years ago

Required date-time Properties Represented As Nullable DateTime In C# By swagger-codegen

I'm a first-time swagger-codegen user, and I have a swagger file from which I would love to simply create proxy data structure C# classes with which to create serialized JSON.  The model classes that got generated by swagger-codegen-cli-3.0.31.jar will certainly save me a bunch of time, but I've noticed that properties with "format: date-time" or "format: date" which are included in the "required" list get represented as nullable DateTime, like this one:

 

[DataMember(Name="departureTime", EmitDefaultValue=false)]
public DateTime? DepartureTime { get; set; }

 

The generated constructor checks to make sure that the corresponding constructor parameter is not null, which is good, but this still leaves open the possibility that the property could be subsequently inadvertently modified to be null.  And all consuming code will not know that this is a required field.  Is the use of DateTime? intentional, or is it possibly a bug?

 

Bonus questions (and please let me know if I should create separate questions for these):

  • How can I eliminate the generation of IEquatable<T> and IValidatableObject inheritance and their corresponding methods?
  • How can I prevent model classes from being "partial"?
  • How can I prevent default constructor parameter values from being specified for required properties?
  • How can other constraints specified in the swagger file (particularly maxLength, pattern, and minItems) be enforced?

Thanks very much!

1 Reply

  • chichepo's avatar
    chichepo
    Champion Level 3

    Hi alexossisos 

     


    ...

    The generated constructor checks to make sure that the corresponding constructor parameter is not null, which is good,

    ....


    indeed, for format "date" or"date-time", null will be always checked, whenever you specify nullable true or false.

     

     


    ...

    , but this still leaves open the possibility that the property could be subsequently inadvertently modified to be null.  And all consuming code will not know that this is a required field.

    ...


    I may not understand your statement but, a required property, can be both null and required.

    I'll try later to answer to some of the "bonus" questions if you are still keeping it in the same thread. (I would suggest you to create a separate one anyway)
    😉