Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inlined user-implemented method missing global alias on method argument #1629

Closed
3 tasks done
Kaskadee opened this issue Dec 6, 2024 · 0 comments · Fixed by #1630
Closed
3 tasks done

Inlined user-implemented method missing global alias on method argument #1629

Kaskadee opened this issue Dec 6, 2024 · 0 comments · Fixed by #1630
Labels
bug Something isn't working

Comments

@Kaskadee
Copy link

Kaskadee commented Dec 6, 2024

Please do the checklist before filing an issue:

  • I have read the documentation, including the FAQ
  • I can reproduce the bug using the latest prerelease version
  • I have searched existing discussion and issue to avoid duplicates

Describe the bug
I created a custom mapping method with the following code:

public DateTimeOffset MapDateTimeToDateTimeOffset(DateTime value) {
    return value != DateTime.MinValue ? new DateTimeOffset(DateTime.SpecifyKind(value, DateTimeKind.Utc)) : DateTimeOffset.MinValue;
}

When inlining this method into a projection method, Mapperly correctly generates the necessary global alias for DateTime.MinValue, new DateTimeOffset and DateTime.SpecifyKind but not for DateTimeKind.Utc causing a CS0103 error.

It works when using global::System.DateTimeKind.Utc in the mapping method itself:

public DateTimeOffset MapDateTimeToDateTimeOffset(DateTime value) {
    return value != DateTime.MinValue ? new DateTimeOffset(DateTime.SpecifyKind(value, global::System.DateTimeKind.Utc)) : DateTimeOffset.MinValue;
}

Declaration code

public sealed class A {
    public required string Name { get; set; }
    public DateTime ChangedOn { get; set; }
}

public sealed class B {
    public required string Name { get; set; }
    public DateTimeOffset ChangedOn { get; set; }
}

[Mapper]
public sealed partial class Mapper {
    public partial B MapAToB(A src);

    public partial IQueryable<B> ProjectAToB(IQueryable<A> query);

    public DateTimeOffset MapDateTimeToDateTimeOffset(DateTime value) {
        return value != DateTime.MinValue ? new DateTimeOffset(DateTime.SpecifyKind(value, DateTimeKind.Utc)) : DateTimeOffset.MinValue;
    }
}

Actual relevant generated code

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.1.0")]
public partial global::System.Linq.IQueryable<global::B> ProjectAToB(global::System.Linq.IQueryable<global::A> query)
{
#nullable disable
    return System.Linq.Queryable.Select(
        query,
        x => new global::B()
        {
            Name = x.Name,
            ChangedOn = x.ChangedOn != global::System.DateTime.MinValue ? new global::System.DateTimeOffset(global::System.DateTime.SpecifyKind(x.ChangedOn, DateTimeKind.Utc)) : global::System.DateTimeOffset.MinValue,
        }
    );
#nullable enable
}

Expected relevant generated code

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.1.0")]
public partial global::System.Linq.IQueryable<global::B> ProjectAToB(global::System.Linq.IQueryable<global::A> query)
{
#nullable disable
    return System.Linq.Queryable.Select(
        query,
        x => new global::B()
        {
            Name = x.Name,
            ChangedOn = x.ChangedOn != global::System.DateTime.MinValue ? new global::System.DateTimeOffset(global::System.DateTime.SpecifyKind(x.ChangedOn, global::System.DateTimeKind.Utc)) : global::System.DateTimeOffset.MinValue,
        }
    );
#nullable enable
}

Notice the change from DateTimeKind.Utc to global::System.DateTimeKind.Utc on line 10.

Reported relevant diagnostics

  • CS0103: The name 'DateTimeKind' does not exist in the current context

Environment (please complete the following information):

  • Mapperly Version: 4.1.1
  • Nullable reference types: enabled
  • .NET Version: .NET 8.0.404
  • Target Framework: net8.0
  • Compiler Version: 4.11.0-3.24510.3 (cfe3bd0a)
  • C# Language Version: 12.0
  • IDE: Rider 2024.3
  • OS: Windows 11 24H2
@Kaskadee Kaskadee added the bug Something isn't working label Dec 6, 2024
TonEnfer added a commit to TonEnfer/mapperly that referenced this issue Dec 7, 2024
TonEnfer added a commit to TonEnfer/mapperly that referenced this issue Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
1 participant