C
C#3mo ago
LifeOfVio

✅ OpenApi generation

I have following class generated from swagger
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.18.2.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v11.0.0.0))")]
public partial class RandomSearchDto
{
[Newtonsoft.Json.JsonProperty("city", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string City { get; set; }

[Newtonsoft.Json.JsonProperty("country", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Country { get; set; }

[Newtonsoft.Json.JsonProperty("createdAfter", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.DateTimeOffset CreatedAfter { get; set; }

[Newtonsoft.Json.JsonProperty("createdBefore", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.DateTimeOffset CreatedBefore { get; set; }
...
}
[System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "13.18.2.0 (NJsonSchema v10.8.0.0 (Newtonsoft.Json v11.0.0.0))")]
public partial class RandomSearchDto
{
[Newtonsoft.Json.JsonProperty("city", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string City { get; set; }

[Newtonsoft.Json.JsonProperty("country", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Country { get; set; }

[Newtonsoft.Json.JsonProperty("createdAfter", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.DateTimeOffset CreatedAfter { get; set; }

[Newtonsoft.Json.JsonProperty("createdBefore", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.DateTimeOffset CreatedBefore { get; set; }
...
}
My issue is that when I create an instance of RandomSearchDto like this -> var searchBody = new RandomSearchDto();, the CreatedAfter and CreatedBefore properties have a default value (because they are not nullable). How can I generate this so that the DateTimeOffsets are generated as nullable?
16 Replies
LifeOfVio
LifeOfVioOP3mo ago
RandomSearchDto is sent as a body to a post endpoint. I don't wan't to specify them in this example but the default value gets send obviously
Bailey
Bailey3mo ago
Hi, There are multiple options - you could make the property nullable usin a ? - you could configure the database (read EF to see how). and tell it is nullable
LifeOfVio
LifeOfVioOP3mo ago
I think you misunderstood something. I am consuming an API. I do not have access to any DB or something. I have a openapi definition that generates a c# client for me /GenerateOptionalPropertiesAsNullable:true was the solution
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
LifeOfVio
LifeOfVioOP3mo ago
I can't, right? it is done via the service reference stuff that uses Newtonsoft.Json
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
LifeOfVio
LifeOfVioOP3mo ago
@TeBeCo I am using NSwag that only supports Newtonsoft.Json to my knowledge
LifeOfVio
LifeOfVioOP3mo ago
GitHub
Epic: Support System.Text.Json · Issue #2243 · RicoSuter/NSwag
A: Use System.Text.Json in generated C# models (DTOs): NJsonSchema 90%: RicoSuter/NJsonSchema#1013 NSwag DONE: Use System.Text.Json serializer in ReadObjectResponseAsync (client generator) B: Use S...
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
reacher
reacher3mo ago
Hmm I'm not sure because I never generated C# models, only TS
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
LifeOfVio
LifeOfVioOP3mo ago
I dont own the code
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
reacher
reacher3mo ago
You can edit the client templates to eliminate newtonsoft Everything is templated
Unknown User
Unknown User3mo ago
Message Not Public
Sign In & Join Server To View
reacher
reacher3mo ago
Genius!
Want results from more Discord servers?
Add your server