How to specify base interface with net5 OpenAPI support
Asked Answered
B

0

8

I am trying to adapt a project to .net5 and want to use the new feature of adding an OpenAPI service reference. As far as I understand it, NSwag will be used to generate the client. I already used NSwag for this purpose in the past and I could use the clientBaseInterface option to specify the base interface of the client interface, i.e.

interface IClient: **IClientBase** {}
class Client: IClient {}

see also https://github.com/RicoSuter/NSwag/issues/2772.

I tried to use the following configuration for my .net5 project but no base interface will be specified for the generated types:

  <ItemGroup>
    <OpenApiReference Include="openapi.json" CodeGenerator="NSwagCSharp" Namespace="MyNamespace">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      <ClassName>MyOpenApiClient</ClassName>
      <OutputPath>MyOpenApiClient.cs</OutputPath>
      <Options>/GenerateClientInterfaces:true /UseBaseUrl:false /ExceptionClass:OpenApiException /ClientBaseInterface:MyBaseInterface</Options>
    </OpenApiReference>
  </ItemGroup>

Does anybody know how this option can be used out of the .net project file?

Update: Since NSwag is creating partial classes and interfaces, a simple workaround is to just add the IClientBase interface in the partial interface, i.e.

// partial extension
partial interface IClient : **IClientBase** {}

// generated code
partial interface IClient {}
partial class Client: IClient {}

However, it still would be interesting to find out if we can manage this directly in the .net project file.

Brana answered 30/11, 2020 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.