Change required behavoir in swagger generation

I have…

  • [x] Checked the logs and have provided uploaded a log file and provided a link because I found something suspicious there.

I’m submitting a…

  • [ ] Regression (a behavior that stopped working in a new release)
  • [x] Bug report
  • [ ] Performance issue
  • [ ] Documentation issue or request

Current behavior

I tried generating the c# client from the swagger client cloud version and I found a strange behavoir. After the last generation, that has been perfomed some months ago, the properties of my schema were flagged as “Required = Newtonsoft.Json.Required.Defaul”, now instead with “Required = Newtonsoft.Json.Required.DisallowNull”. With this modification when I try to deserialize some content that do not have some property valorized I get a DeserilizationException. Since I do not changed the schemas, this must be some bug in the swagger.json definition.

Here a comparison between the two version of the generated code of a variable of one of my classes:

#OLD
[Newtonsoft.Json.JsonProperty(“FooterContentsMobile”, Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public ChannelconfigFooterContentsMobileProperty FooterContentsMobile { get; set; }

#NEW
[Newtonsoft.Json.JsonProperty(“CustomerModelMandatoryFields”, Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public ChannelconfigCustomerModelMandatoryFieldsProperty CustomerModelMandatoryFields { get; set; }

Expected behavior

If in the schema configuration the field is not mandatory, the generated client with swagger must handle the null value

Environment

  • [ ] Self hosted with docker
  • [ ] Self hosted with IIS
  • [ ] Self hosted with other version
  • [X] Cloud version

Browser:

  • [X] Chrome (desktop)
  • [ ] Chrome (Android)
  • [ ] Chrome (iOS)
  • [ ] Firefox
  • [ ] Safari (desktop)
  • [ ] Safari (iOS)
  • [ ] IE
  • [ ] Edge

Hi,

the json has been changed from Swagger to OpenAPI. Which generator do you use?

In general I recommend to configure to ignore validation properties such as Required, because even though your field might be required now, it can be null when it was not required before or has been added after some content has been inserted.

I am not sure if this is really a bug.

Hi Sebastian,
Thank you for your explaination. I will try to solve this issue directly in my generator.
You can close this issue

1 Like