takttusur / takt-media

Check our news and participate to competitions and other events
MIT License
0 stars 0 forks source link

Support VK API classes generation using json-schema #14

Closed andreygolubkow closed 5 months ago

andreygolubkow commented 7 months ago

Support VK API classes generation using json-schema

As a backend developer I want to update VK API client using json-schema So that I don't have to write/rewrite C# classes

Acceptance criteria

Project structure

  1. This issue has a list of several libraries for .NET which provides code generation from json-schema
  2. TaktTusur.Media.Clients.VkApi.csproj has new folders/files:
    1. Don't touch: GroupInfoResponse, Models, Requests, WallByIdResponse, IVkApiClient.cs, VkApiClient.cs
    2. New dir VkSchema
  3. VkSchema contains files from vk-api-schema :
    1. schema.json
    2. errors.json
    3. Groups directory and Wall directory with:
      1. methods.json
      2. objects.json
      3. responses.json

Build actions

  1. When build is started for TaktTusur.Media.Clients.VkApi.csproj, VkSchema directory gets C# classes with generated code based on json-schema
  2. All classes should be in one namespace
  3. Naming for classes follows this pattern: *.generated.cs
    1. for example wall.generated.cs
  4. Generated classes should not be indexed by Git
  5. The code in generated classes is correct and available for build

Technical details

  1. About VK Json-schema
  2. Implementations for .NET
  3. VK API JSON Schema

Possible solution, adaptation needed

Add to TaktTusur.Media.Clients.VkApi.csproj

<ItemGroup>
  <PackageReference Include="NJsonSchema" Version="10.6.6" />
</ItemGroup>

Create console app TaktTusur.Media.Clients.VkApi.Generator.csproj

static void Main(string[] args)
    {
        var directoryPath = "path_to_your_json_schemas";
        var outputPath = "path_where_you_want_to_place_generated_classes";

        foreach (var filePath in Directory.GetFiles(directoryPath, "*.json"))
        {
            var document = JsonSchema.FromFileAsync(filePath).Result;
            var generator = new CSharpGenerator(document);
            var file = generator.GenerateFile();

            var outputFilePath = Path.Combine(outputPath, Path.GetFileNameWithoutExtension(filePath) + ".cs");

            File.WriteAllText(outputFilePath, file);
        }
    }

Add to TaktTusur.Media.Clients.VkApi.csproj

<Target Name="GenerateFromJsonSchema" BeforeTargets="Build">
    <MSBuild Projects="path_to_Generator_csproj\Generator.csproj" Targets="Build" />
</Target>
andreygolubkow commented 5 months ago

Cannot be implemented, because of we don't have tools to generate classes from vk schema