NSwag: C# code generation with /GenerateBaseUrlProperty:false option is broken in v14.0.1

NSwag generates invalid C# code when /GenerateBaseUrlProperty:false option is used. It looks like it is a regression in v14.0.1 possibly caused by #4691 Here’s a simple project which demonstrates the regression: GenerateBaseUrlPropertyFalse.zip. It will download and use example petstore OpenAPI document. Just run dotnet build. It works with NSwag.ApiDescription.Client 13.20.0 and 14.0.0 but fails with 14.0.1

@paulomorgado

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 3
  • Comments: 19 (9 by maintainers)

Commits related to this issue

Most upvoted comments

When will this issue be fixed? The code should use the BaseUrl from the ClientBase instead of hardcoding the original URL in the _baseUrl. Please also revert the logic to trim a trailing ‘/’. As mentioned above, this potentially introduces bugs.

I can confirm the impact on my project as well.

My use case is: useBaseUrl=true generateBaseUrlProperty=false

I have a base class that implements BaseURL and return the urlto be used in urlBuilder but now it’s ignored. The proxy is using directly _baseUrl instead of BaseURL

@RicoSuter The 14.0.3 generated code is getting compiled without errors now but the logic of how /GenerateBaseUrlProperty:false flag works is different now.

  • Up to (and including) 14.0.0 it meant that BaseUrl property was not generated but still was used and it was responsibility of base class to declare and assign a BaseUrl value (usually from configuration).
  • In 14.0.1 and 14.0.2 it was broken and code generated with /GenerateBaseUrlProperty:false flag didn’t compile.
  • In 14.0.3 code is getting compiled again but works differently - BaseUrl property is not generated and is not used at all. Instead private string _baseUrl field is assigned from a host value defined in OpenAPI document. The only workaround I could find to change _baseUrl value is to put some weird logic into override of PrepareRequest method.

It looks like it is still a breaking change between 14.0.0 and 14.0.3.

Any updates on the issue?

@lukewilson2002, do you have any nullable/optional path parameters?

Not sure I understand the question, can you elaborate?

Sorry. I was thinking of another issue.