'C# make class from swagger.json as part of build

In ViaulStudio I clicked some buttons via Add Connected Service and have ended up with a C# class that I can use to call a HTTP API.

It looks like the HTTP API produces a json metadata document that describes the HTTP API, and then this is used to generate the C# class.

In order to fix the build I copied the generated .cs file out of the obj directory and added it to the soure control -- I presume this is wrong?

It seems that it should be possible to add the .json file to the repository instead and have the C# class generated as a pre-build event.

Is this the right idea? How are you supposed to get this to work with source control?



Solution 1:[1]

    <ItemGroup>
        <!-- NSwagSharp is buggy!
        GenerateExceptionClasses forces it off but not on.
        Therefore we have to generate ApiException, move it to a separate file, and then reference it.
        -->
        <OpenApiReference Include="some_api.json" Namespace="CompanyName.SomeProduct.SomeApiProxy" ClassName="SomeApiProxy" OutputPath="$(ProjectDir)SomeApiProxy.cs">
            <CodeGenerator>NSwagCSharp</CodeGenerator>
            <Options>/AdditionalNamespaceUsages:CompanyName.SomeProduct.Exceptions /GenerateExceptionClasses:false</Options>    
        </OpenApiReference>

    </ItemGroup>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Richard Barraclough