[BUG][CSHARP] Multiple OAuth Flows generates C# code that doesn't compile

Aperta Adatta ai principianti
#22,189 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
csharp
Ambito
tooling

Direzione di ricerca

Inizia da modules/openapi-generator/src/main/resources/csharp/libraries/generichost/DependencyInjectionTests.mustache intorno alla riga 79, quindi riproduci il comando di generazione utilizzando la specifica OpenAPI collegata. Verifica che il file generato api-csharp/src/Org.OpenAPITools.Test/Api/DependencyInjectionTests.cs venga compilato senza dichiarazioni duplicate di OAuthToken.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Issue: Bug
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.

Lingua principale
Java
Stelle
26.8k
Fork
7.7k
Merge medio
1g 10h
PR unite (30g)
103

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di OpenAPITools/openapi-generator

Tutte le issue di OpenAPITools/openapi-generator

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.