Fix default value initialization for nullable properties#345
Merged
habbes merged 6 commits intoOData:masterfrom May 4, 2023
Merged
Fix default value initialization for nullable properties#345habbes merged 6 commits intoOData:masterfrom
habbes merged 6 commits intoOData:masterfrom
Conversation
| /// </summary> | ||
| /// <param name="typeName">The type name.</param> | ||
| /// <returns>The underlying type name.</returns> | ||
| internal abstract string StripNullableFromTypeName(string typeName); |
Contributor
There was a problem hiding this comment.
so the type name here will be in form of a string? Exa Nullable<DateTime> then the method returns DateTime but in string form?
Contributor
There was a problem hiding this comment.
Maybe I do not understand but why can't we check if a type is nullable without the conversions to string?
Contributor
Author
There was a problem hiding this comment.
Because by the time we get to use this method, the input type name is already in string form. I do agree that it would be more robust to use actual reflection type metadata to detect nullability, but we don't have that information in the method where this method is used. To use types would require refactoring a lot more methods and a lot more work than what I "budgeted" to fix this bug.
73a5b2e to
a67e18d
Compare
ElizabethOkerio
approved these changes
May 2, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Fix #342
When a property definition in the CSDL document specifies a default value like follows:
The generated code (for both C# and VB) includes a corresponding initialization for the backing value of the CLR property:
However, when the CSDL property is nullable, the initializer is not properly generated:
Depending on the property type and initialization, this may lead to errors. For example, the above expression will evaluate to an integer instead of a date, causing a compile-time error.
Solution
The code generator generates different initialization code based on the data type. However, its data-type detection logic does not take into Nullable types into account. A boolean EDM property with
Nullableset to true will be translated toNullable<bool>in .NET instead ofbool. This PR makes adjustments to this logic such thatNullable<T>andTare treated the same way for the purpose of property value initialization. I added aStripNullableFromTypeNamehelper method for both VB and C# and use it to transformNullable<T>intoTso that the property initializer can detect the underlying type correctly and generate the correct initialization code.