[BUG][CSHARP][generichost] Many-to-one discriminator mapping emits duplicate local variables (CS0128)

Ouverte Adaptée aux débutants
#25,640 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

Personne n'a encore pris cette issue.

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
82/100
Type d'issue
Bug
Clarté
Clairement spécifiée
Activité
Active
Stack technique
csharp, java, openapi
Domaine
tooling

Piste de recherche

Lisez csharp/libraries/generichost/JsonConverter.mustache, en particulier le bloc de déclaration mappedModels, et comparez-le au bloc composedSchemas.oneOf, qui ignore x-duplicated-data-type. Reproduisez le problème avec la spécification et la commande de génération fournies, puis compilez le projet généré. Le travail est terminé lorsque le mappage many-to-one déclare chaque variable de schéma distincte une seule fois et que le projet généré se compile sans CS0128.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

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? (Reproduced on 7.26.0, 7.27.0 and master df804ac9802fba0c51a64f8c3cb03191ed3dde2d (openapitools/openapi-generator-cli:latest, 7.28.0-SNAPSHOT); library=restsharp builds on all three.)
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
Description

When a oneOf discriminator mapping sends two or more values to the same schema, the csharp generichost library emits one local variable per mapping entry instead of one per distinct schema, all in the same scope of the converter's Read method. The generated client does not compile:

error CS0128: A local variable or function named 'cat' is already defined in this scope

A many-to-one mapping is valid: discriminator.mapping maps values to schemas and the specification does not require it to be injective. The restsharp library compiles the same spec without errors.

openapi-generator version

7.26.0 (CLI), 7.27.0 and master df804ac980 (7.28.0-SNAPSHOT), all with the same CS0128. JsonConverter.mustache changed after 7.27.0 (#25486), but the block that declares the variables ({{#mappedModels}} ... {{classname}}{{nrt?}} ... = null;) did not.

OpenAPI declaration file content
openapi: 3.0.3
info: {title: repro, version: "1"}
paths:
  /pet:
    get:
      operationId: getPet
      responses:
        "200":
          description: ok
          content:
            application/json:
              schema: {$ref: "#/components/schemas/Pet"}
components:
  schemas:
    Pet:
      oneOf:
        - $ref: "#/components/schemas/Cat"
        - $ref: "#/components/schemas/Dog"
      discriminator:
        propertyName: kind
        mapping:
          cat: "#/components/schemas/Cat"
          kitten: "#/components/schemas/Cat"
          dog: "#/components/schemas/Dog"
    Cat:
      type: object
      required: [kind]
      properties:
        kind: {type: string}
        meows: {type: boolean}
    Dog:
      type: object
      required: [kind]
      properties:
        kind: {type: string}
        barks: {type: boolean}
Generation Details
openapi-generator-cli generate -i spec.yaml -g csharp -o out \
  --additional-properties=targetFramework=net8.0,packageName=Repro
dotnet build out/src/Repro/Repro.csproj
Actual output

out/src/Repro/Model/Pet.cs, PetJsonConverter.Read:

Cat? cat = null;
Dog? dog = null;
Cat? cat = null;

The per-value branches further down are fine: kitten deserializes into the same cat variable as cat does, so only the declarations need deduplicating.

Expected output

One declaration per distinct mapped schema:

Cat? cat = null;
Dog? dog = null;
Steps to reproduce
  1. Save the spec above as spec.yaml.
  2. Run the generation command (default library, i.e. generichost).
  3. dotnet build fails with CS0128 at the second Cat? cat = null;.
  4. Add library=restsharp to --additional-properties and the build succeeds.
Related issues/PRs

Searched for "generichost discriminator", "CS0128" and "discriminator mapping same schema". #20059 and #22284 are about generichost discriminator mappings too, but they report a different symptom (mapping names ignored), not duplicate declarations.

Suggest a fix

Deduplicate mappedModels by model name before rendering the declaration block in csharp/libraries/generichost/JsonConverter.mustache, or declare the variables from composedSchemas.oneOf instead of mappedModels. The template already skips x-duplicated-data-type there, but that flag is not set for repeated mapping targets.

Langage dominant
Java
Étoiles
26.9k
Forks
7.8k
Merge moyen
2 j 10 h
PR mergées (30 j)
180

Préparer son environnement

Ouvrir dans Codespaces

Lance le conteneur de développement du projet dans votre navigateur, avec votre propre compte GitHub.

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de OpenAPITools/openapi-generator

Toutes les issues de OpenAPITools/openapi-generator

Issues similaires

Plus d'issues Java

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.