Skip to content

MappingOptionalAttribute should be usable on record parameters? #854

@jpeirson

Description

@jpeirson

Describe the bug
While [MappingSource] and [MappingDefaultValue] can be applied to record constructor params, the [MappingOptional] cannot. Is this intentional? Even its XML docs seem to suggest it was meant for properties or something else: "If a property or is decorated...".

Because of this, I can't just declare this on optional record constructors. Instead, I have to declare a separate property and add [MappingOptional] to it, which seems like unnecessary code.

To Reproduce

public record MyObject(
  string Id,
  [MappingOptional] // <-- produces compiler error
  string? Extra = null
);

or

public record MyObject(
  string Id,
  [property: MappingOptional] // <-- compiles, but default mapper doesn't recognize property as optional
  string? Extra = null
);

In the second example, it compiles, but runtime will fail with "Cannot map record to type Entity because the record does not contain a value for the parameter 'Extra'." if the property isn't present on the record.

Expected behavior
Should be able to just declare:

public record MyObject(
  string Id,
  [MappingOptional]
  string? Extra = null
);

and default mapper will mark the Extra property as optional when reading records.

Version Info

  • .NET Version: net8.0
  • .NET Driver Version: 5.28.2
  • Neo4j Server Version & Edition: Desktop v2.0.1 (Windows)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions