OpenAPITools / OpenAPITools/openapi-generator
[BUG] [csharp-netcore] new RestClient and OAuthAuthenticator instance for every request
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- [N/A] Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
In csharp-netcore, using restsharp as the "library" option (this is the default), the ApiClient class creates an instance of a RestClient for each call to Exec() and ExecAsync(). This behavior is making it hard (or impossible) to customize the OAuth2 flow implementation for token expiration handling for example because no state can be stored (new OAuthAuthenticator instance is created for every request too). It also causes memory leaks, see related issue.
openapi-generator version
v6.2.1
OpenAPI declaration file content or url
N/A
Generation Details
Create a client using the csharp-dotnet generator, leaving the "library" option set to default (i.e. restsharp).
Steps to reproduce
Check the generated ApiClient class. On line 461, a new RestClient instance is created, the line belongs to the Exec method, which is invoked on every request.
Related issues/PRs
https://github.com/OpenAPITools/openapi-generator/issues/13961
Suggest a fix
A RestClient instance should be used as a 'Singleton', only one instance is needed. Same for OAuthAuthenticator.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by inspecting the generated csharp-netcore ApiClient around line 461 and tracing Exec and ExecAsync, then reproduce the default restsharp generation described in the issue. Done means the generated client reuses one RestClient and one OAuthAuthenticator across requests, preserving state for OAuth token handling and avoiding the reported leaks.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api, tooling
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100