[BUG][CSHARP] Multiple OAuth Flows generates C# code that doesn't compile
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 76/100
Research direction
Start with modules/openapi-generator/src/main/resources/csharp/libraries/generichost/DependencyInjectionTests.mustache around line 79, then reproduce the generation command using the linked OpenAPI specification. Verify the generated api-csharp/src/Org.OpenAPITools.Test/Api/DependencyInjectionTests.cs compiles without duplicate OAuthToken declarations.
Written by the indexing model from the issue text.
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- Have you tested with the latest master to confirm the issue still exists?
- No, but I linked the offending code below and it's still broken in master.
- 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
When you have multiple OAuth flows defined in a specification, the generated C# code fails to compile due to a duplicate variable.
openapi-generator version
7.16.0
OpenAPI declaration file content or url
Example using the stock spec from Swagger Editor. Full spec - https://gist.github.com/scottbusche-usps/9ebaf3038984d40a61a6cf73da87b880
securitySchemes:
petstore_auth:
type: oauth2
flows:
clientCredentials:
tokenUrl: https://petstore3.swagger.io/oauth/token
scopes:
"write:pets": modify pets in your account
"read:pets": read your pets
implicit:
authorizationUrl: https://petstore3.swagger.io/oauth/authorize
scopes:
"write:pets": modify pets in your account
"read:pets": read your pets
Generation Details
java -jar openapi-generator-cli.jar generate -g csharp -o api-csharp -i oas.yaml --additional-properties=targetFramework=net8.0
I do not believe it matters on the targetFrameework version.
Steps to reproduce
Try to compile the code generated from the above command. Inside api-csharp/src/Org.OpenAPITools.Test/Api/DependencyInjectionTests.cs will be this block:
private readonly IHost _hostUsingConfigureWithAClient =
Host.CreateDefaultBuilder([]).ConfigureApi((context, services, options) =>
{
ApiKeyToken apiKeyToken1 = new("<token>", ClientUtils.ApiKeyHeader.Api_key, timeout: TimeSpan.FromSeconds(1));
options.AddTokens(apiKeyToken1);
OAuthToken oauthToken = new("token", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(oauthToken);
// This oauthToken here has no index and duplicates the first one
OAuthToken oauthToken = new("token", timeout: TimeSpan.FromSeconds(1));
options.AddTokens(oauthToken);
options.AddApiHttpClients(client => client.BaseAddress = new Uri(ClientUtils.BASE_ADDRESS));
})
.Build();
Related issues/PRs
Nothing directly related that I could find.
Suggest a fix
This line appears to need to add {{-index}} suffix to the variable name:
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/csharp/libraries/generichost/DependencyInjectionTests.mustache#L79
This looks pretty straight forward and happy to submit a PR.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- Avg merge
- 1d 10h
- Merged PRs (30d)
- 103
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.
More from OpenAPITools/openapi-generator
-
Issue: Bug
Difficulty 1/5 Under an hour Newbie friendliness 91/100
OpenAPITools/openapi-generator#24978 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
OpenAPITools/openapi-generator#24975 ·
-
Issue: Bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
OpenAPITools/openapi-generator#24859 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
OpenAPITools/openapi-generator#24852 · 1 comment ·
-
[BUG][KOTLIN] Fails to compile after updating to 7.25.0 with useJackson3=false useSpringBoot4=true OpenIssue: Bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
OpenAPITools/openapi-generator#24842 · 1 comment ·
All issues in OpenAPITools/openapi-generator
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
infinispan/infinispan#18150 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
-
untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
opensearch-project/k-NN#3597 ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100