[BUG][CSHARP][generichost] Many-to-one discriminator mapping emits duplicate local variables (CS0128)
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 82/100
Línea de trabajo
Lee csharp/libraries/generichost/JsonConverter.mustache, especialmente el bloque de declaración mappedModels, y compáralo con el bloque composedSchemas.oneOf, que omite x-duplicated-data-type. Reproduce el problema con la especificación y el comando de generación proporcionados; después, compila el proyecto generado. El trabajo estará terminado cuando la asignación many-to-one declare cada variable de esquema distinta una sola vez y el proyecto generado se compile sin CS0128.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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=restsharpbuilds 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
- Save the spec above as
spec.yaml. - Run the generation command (default library, i.e.
generichost). dotnet buildfails with CS0128 at the secondCat? cat = null;.- Add
library=restsharpto--additional-propertiesand 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.
- Lenguaje dominante
- Java
- Estrellas
- 26.9k
- Forks
- 7.8k
- Merge medio
- 2 d 10 h
- PR fusionados (30 d)
- 180
Preparar el entorno
Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.
- Incluye un Dockerfile o un archivo de Docker Compose
- Tiene una plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de OpenAPITools/openapi-generator
-
Issue: Bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 80/100
OpenAPITools/openapi-generator#25643 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
[BUG][JAVA] Deprecated properties only get @Deprecated on the getter (base, feign, native, microprofile)Posiblemente ocupada @Inf1mum la tomó hace 2 días. AbiertoIssue: Bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
OpenAPITools/openapi-generator#25567 ·
Los mantenedores suelen responder en 1 día
-
[BUG][KOTLIN-SERVER] typeInfoAnnotation.mustache uses As.PROPERTY → discriminator property serialized twicePosiblemente ocupada @Inf1mum la tomó hace 9 días. AbiertoIssue: Bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
OpenAPITools/openapi-generator#25524 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
[REQ][JAVA][resteasy] Support for Jackson 3 / useJackson3Posiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. AbiertoEnhancement: Feature
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
OpenAPITools/openapi-generator#25364 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
OpenAPITools/openapi-generator#25237 ·
Los mantenedores suelen responder en 1 día
Todos los issues de OpenAPITools/openapi-generator
Issues similares
-
bug emitter:client:csharp
Dificultad 2/5 1-3 horas Aptitud para principiantes 84/100
microsoft/typespec#12572 · 1 comentario · 1 reacción ·
Los mantenedores suelen responder en 1 día
-
GCC 15.2: -Wcast-qual warning - cast discards const qualifier from pointer target type (H5trace.c)Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 73/100
Los mantenedores suelen responder en 1 día
-
[Bug] RemotingHelper.connect swaps the socket send and receive buffer configurationPosiblemente ocupada @amalsaidov la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
Los mantenedores suelen responder en 1 día
-
needs-area-label
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 2 días
-
area/ai area/backend component/zeebe kind/task
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
Los mantenedores suelen responder en 1 día