jellyfin-sdk-kotlin/openapi-generator
2021-03-25 17:34:12 +01:00
..
src Use an actual const for generated constants 2021-03-25 17:34:12 +01:00
build.gradle.kts Add download and update tasks for OpenAPI spec (#149) 2020-11-22 16:35:48 +01:00
README.md Add ServiceNameHook to README 2021-01-24 14:12:58 +01:00

OpenAPI Code Generator

This module is a tool to generate parts of the jellyfin-api and jellyfin-model modules. It uses the OpenAPI specification generated by the server (since 10.7) and transforms it into a custom structure that will be converted into Kotlin code. It will only generate the models and API methods. The API methods always use an instance of KtorClient which implements the actual HTTP calls. Changes to the http client can thus be made without updating the generator.

The generator uses a special hook system to change the generated code to better suite the Kotlin language. For example, the "ImageBlurHashes" property in BaseItemDto can be simplified by using a Map, and therefor a hook is added for it. All these hooks need to be maintained and updated when the OpenAPI specification changes.

Running

The following Gradle tasks can be used to run the generator:

  • generateSources
    Reads the openapi.json files and generates Kotlin source code.
  • downloadApiSpecStable & downloadApiSpecUnstable
    Downloads the openapi.json file from repo.jellyfin.org.
  • updateApiSpecStable & updateApiSpecUnstable
    Runs the download task followed by the generateSources task.

Updating the repository

When the repository needs to be updated for a new OpenAPI file the following shell commands are used:

gradlew openapi-generator:updateApiSpecStable
git add .
git commit -m "Update generated sources"
git push

Hooks

Hooks are classes that will modify the output of the generator. They should be registered in the HookModule.kt file. The following hooks are available:

  • TypeBuilderHook
    A hook that can intercept the TypeBuilder which converts OpenAPI schemas to Kotlin types. It receives the schema and a type path. The path is unique across the whole document and can be used to identified specific properties. This hook is called when generating types for:

    • model properties
    • api operation parameters
    • api operation bodies
    • api operation return types
  • OperationUrlHook
    A hook that can request a url function to be added for an API operation. It receives the model for a complete api service and a single operation. If any hook returns true the generator will add a special function called [operation]Url (like "GetImageUrl") that will return a string containing the request url.

  • ServiceNameHook
    A hook that can modify the name(s) of an operation. It can add, rename and delete the services for an operation. When all services are removed the operation will never be generated. Can be used to fix spelling, moving operations to different services or adding operations to additional services.

Phases

The conversion happens in multiple phases. The phases in order are:

  1. Parse
    Reads the OpenAPI file using swagger-parser, which also validates the document, and returns the OpenAPI specification POJOs

  2. Transform
    Converts the POJOs to a custom data model optimized for generating code

  3. Generate
    Creates the code for the models and api operations using KotlinPoet

  4. Write
    Writes all new code on top of the current code