Not nullable enum and object members.
Closed this issue · 9 comments
I am using odata2poco with SAP service layer. Some of the enum or object members are null from the api. I am using the -b, --nullable option. It works for C# primitives, but enums and objects from the api are not nullable. The newtonsoft JSON deserialization library can't deserialize the response object because there are fields with null value which are not nullable in the generated file.
If i put the "?" after the type definitions everything works. Is it possible to make every member variable nullable with the nullable option?
Hi @kustanbruno
Can you show me sample of xml enum/objects from the meta data so I can use it for test. You can generate the xml meta data using -x option.
Hi @moh-hassan
Here is the xml file. Thank you for your quick response.
metafile.xml.txt
Thanks for response.
I checked the xml metaData file and find only the primitive data types has nullable attribute
<Property Name="RowNo" Nullable="false" Type="Edm.Int32" />
All enum type or objects has no Nullable attribute
It seems that the SAP XML file doesn't support nullable reference types of c# 8
OData2Poco apply nullable to only properties that have Nullable attribute with -b option.
Can you try to add the next line at start of cs file:
#nullable disable
If this is applicable, i can add a new feature to disable reference data types.
Also i can add a new feature to add ? to all reference data types (has no Nullable attribute).
Have a look to James Newton-King tweet
What do you think to manage this nullable reference type objects?
@kustanbruno
I tried deserialize using nullable reference type with Newtonsoft.Json version 13.0.1 and succeeded in deserializing nullable object without error and without adding ? to the type, even with enabling nullable in c# 8.
have a look to this attached demo.
JsonDemo.zip
What is special for your case that fire exception when deserialization of null objects?
Kindly, Can you feedback ?
@moh-hassan Sorry for the slow reactions. I tried the adding the line you suggested to the poco file. The error still remains. We are using microsofts builtin System.Net.Http.HttpClient to make requests with the sap api. We get the following exception when trying to get from the api without the question marks.
Newtonsoft.Json.JsonSerializationException: Error converting value {null} to type 'cool_sap_api.DTO.SAP.Responses.BoYesNoEnum'. Path 'value[0].ReopenOriginalDocument'
We are using .net6, I think it uses C#10. I don't know if anything has changed in regards of nullable types.
I think the fastest solution to our problem would be adding the feature to add ? to all reference data types, even if they don't have the Nullable attribute.
Thanks for your help.
@kustanbruno,
Thanks for your suggestion.
I think the fastest solution to our problem would be adding the feature to add ? to all reference data types, even if they don't have the Nullable attribute.
I agree with you and I'll add this feature in the next release.
@kustanbruno
I added the feature of enabling nullable reference type.
Have a look to the generated file
The new option is -B (upper case) or --enable-nullable-reference
The attached file include o2pgen.exe and sample of command with sap.cs generated cs
Kindly, Can you try it and feedback of the new feature.
O2Pgen.zip
The new feature works perfectly. I tried out the file and the generation too.
Thank you for your quick and professional work.
Hi @kustanbruno
A new version 4.3.1 is released with the new feature.
You can download from nuget.