canton7 / RestEase

Easy-to-use typesafe REST API client library for .NET Standard 1.1 and .NET Framework 4.5 and higher, which is simple and customisable. Inspired by Refit
MIT License
1.09k stars 109 forks source link

Strange enum error with SourceGenerator #181

Closed netclectic closed 3 years ago

netclectic commented 3 years ago

I'm seeing a strange build error with an enum query parameter when building with the SourceGenerator. The error is CS0103 The name 'Asc' does not exist in the current context

using RestEase;
using System.Threading.Tasks;

namespace netclectic.api
{
    public enum TestEnum
    {
        Asc,
        Desc
    }

    public interface ITestApi
    {
        [Get("/test")]
        Task<string[]> Test([Query("order")] TestEnum order = TestEnum.Asc);
    }
}

If I remove the SourceGenerator package then it builds fine and strangely if I set it to be a nullable parameter it also builds fine.

This is a netstandard2.1 class library with LangVersion set to preview, with v1.5.2 of ReastEase and RestEase.SourceGenerator.

canton7 commented 3 years ago

Thanks for reporting. I can imagine the source generator failing to emit enum default values correctly: I'll take a look, hopefully tomorrow.

canton7 commented 3 years ago

This is due to some frankly odd behaviour in Roslyn, which pops up when you use a parameter which is an enum and give it a default value. I'll get a fix it for the next release. For now I'm afraid the only workaround is to define a method overload which doesn't take the enum and hard-code its value into the path, or uninstall the source generator for now.

netclectic commented 3 years ago

No worries, thanks for the update. I'm happy to leave it as an optional parameter for the time being.