Skip to content

SetHasOneForeignKeyValue detects string as non-nullable type and throws exception #528

Closed
@PiotrBrzezianski

Description

@PiotrBrzezianski

Description

I have a 0..1 to 1 relationship using foreign key of type string. As string can be null so it should be fine to use it as a foreign key in this scenario but I'm getting an exception thrown from SetHasOneForeignKeyValue method:

"Cannot set required relationship identifier '{hasOneAttr.IdentifiablePropertyName}' to null because it is a non-nullable type."

I believe this is because the method uses this code for the check: Nullable.GetUnderlyingType(foreignKeyProperty.PropertyType)
which returns null for type string.

That is because GetUnderlyingType only works as expected here for types derived from Nullable which string is not.
https://referencesource.microsoft.com/#mscorlib/system/nullable.cs,132
...

Environment

  • JsonApiDotNetCore Version: 4.0.0-alpha3

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions