OpenAPITools / OpenAPITools/openapi-generator

[REQ] C# - Provide Dependency Injection support

Open
#20,642 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Problem

I need to use the generated Client classes through Dependency Injection, including configuration. To achieve this, I would write something like the below for each client, when configuring the service container and repeat the same few lines for each client.

        builder.Services.Configure<Configuration>(builder.Configuration.GetSection(nameof(Configuration)));
        builder.Services.AddSingleton<WeatherForecastApi>((sp) =>
            new WeatherForecastApi(sp.GetRequiredService<IOptions<Configuration>>().Value));

I would prefer to use a simpler way to configure the client, such as

        builder.AddSingletonApiClient<WeatherForecastApi>();
Solution

I propose to introduce a set of generic extensions as part of the Api directory of the C# client that look like this.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the generated C# client's Api directory and the dependency-injection extension proposal in the linked gist. Determine how configuration and generated Client classes should be registered, then verify that the proposed helper can configure each client through the service container.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend-api-design, developer-experience
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.