question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mapping string[] to string[] throws CS8081

See original GitHub issue

Describe the bug Mapping string array to a string array throws a CS8081: “Expression does not have a name”.

The picture describes the error: afbeelding

To Reproduce Steps to reproduce the behavior:

  1. Use the code snippets and check the generated code

Expected behavior No CS8081 exception in the generated code. Also the generated code is different with no generated code header comment which is strange behavior?

Code snippets A.cs:

namespace Namespace;

using System;

public class A
{
	public string[] Items { get; set; }

	public A()
	{
		Items = Array.Empty<string>();
	}

	public A(string[] array)
	{
		Items = array;
	}
}

B.cs:

// //------------------------------------------------------------------------------
// // <auto-generated>
// //     This code was generated by a tool.
// //
// //     Changes to this file may cause incorrect behavior and will be lost if
// //     the code is regenerated.
// // </auto-generated>
// //------------------------------------------------------------------------------
namespace AnotherNamespace
{
	using System;

	public class A
	{
		public string[] Items { get; set; }

		public A()
		{
			Items = Array.Empty<string>();
		}

		public A(string[] array)
		{
			Items = array;
		}
	}
}

Mapper.cs:

namespace Namespace;

using Riok.Mapperly.Abstractions;

[Mapper]
public partial class Mapper
{
	public partial A AtoA(AnotherNamespace.A abcde);
}

Environment:

  • Mapperly Version: 2.8.0
  • .NET Version: .NET SDK 7.0.203
  • Target Framework: net6.0
  • IDE: Visual Studio v17.5.5
  • OS: Windows 11 22H2

Issue Analytics

  • State:closed
  • Created 4 months ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
TimothyMakkisoncommented, May 8, 2023

Minimal example

[Mapper]
public static partial class Mapper
{
    public static partial int[] Map(int?[] value);
}

Simplified issue

[Mapper]
public static partial class Mapper
{
    public static partial A BtoA(B value);
}
public class A
{
    public string[] Items { get; set; } = Array.Empty<string>();
}
public class B
{
    public string?[] Items { get; set; } = Array.Empty<string>();
}

It also looks like a lot of tests are broken, Dictionary and Enumerable tests both generate invalid code.

1reaction
Sjoerdsjoerdcommented, May 8, 2023

I think beside the nullable problem, the main problem here is the nameof(source[i]) part, as an indexer is not valid inside a nameof expression.

@Sjoerdsjoerd I cannot reproduce the issue with the code you provided with v2.8.0/main and neither with enabled/disabled nullable reference types. Do you have UseDeepCloning, ThrowOnMappingNullMismatch, or ThrowOnPropertyMappingNullMismatch enabled? Do both properties have an accessible getter/setter? What nullable reference types settings do the classes Namespace.A and AnotherNamespace.A have?

@latonz No, the mapper I used was the default one. Same as in the snippet. I think the problem is when having both enabled and disabled nullable references. As @TimothyMakkison also acknowledge. Both properties have setters (see snippet). Nullable in csprojects is enabled for all classes. But I think that the header for generated code makes disables nullable.

After a bit of research, I came upon this doc that explains why it disables nullable for generate code: https://github.com/dotnet/roslyn/blob/main/docs/features/nullable-reference-types.md#generated-code

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mapping with automapper system.strin to string exception
I'm trying to create mapping, everything works fine but app throws exception. {"LINQ to Entities does not recognize the method 'System.
Read more >
Case Statements for string to string mapping [duplicate]
If you have multiple different mappings for different objects across multiple different projects, that is fine.
Read more >
Converting Map to Map in Java
Let's test it with our MAP1: Map<String, String> result = checkAndTransform(MAP1); assertEquals(EXPECTED_MAP1, result); Moreover, once the input map holds some ...
Read more >
Mapping String Data
The purpose of the NONE map on the Server canvas is to turn off mapping of string data in any stage in which...
Read more >
Transforming data with map() – Hacking with Swift+
This will accept a single Any parameter and return the string ... Swift's own map() is able to deal with a throwing transformation....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found