[BUG] OpenApiEvaluator reports "default value not in enum" for numerically equal values with different scale (e.g. 1.5 and 1.50)
Los mantenedores suelen responder en 1 día
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 78/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- java, openapi
- Área
- backend-api-design, testing
Línea de trabajo
Empieza con OpenApiEvaluator#validate(OpenAPI) en el módulo openapi-generator y la prueba CT-OE-14 indicada, OpenApiEvaluatorJUnitTest.ct14NumericDefaultComparedByValue. Lee la comprobación existente de la recomendación y ejecuta el comando de Maven para la prueba específica del issue. El objetivo se cumple cuando valores BigDecimal numéricamente iguales, como 1.5 y 1.50, no producen una advertencia, mientras que las demás comparaciones de enum conservan su comportamiento actual.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Summary
The "Schema has default value not in enum" recommendation compares the default against the enum items with List.contains. In numeric schemas the values are BigDecimal, and BigDecimal.equals takes the scale into account. As a result, 1.50 is not recognized as equal to 1.5, and the evaluator emits a false warning.
Environment
- Project: OpenAPI Generator v7.25.0 (fork), commit
ef964b04480889ef86b56cfae84ade8ad4c91c41 - Module:
modules/openapi-generator - Class/method:
org.openapitools.codegen.validations.oas.OpenApiEvaluator#validate(OpenAPI) - OS: Linux 7.0.0-38-generic (amd64)
- Java: OpenJDK 25 (compiled with source/target 11)
- Tests: JUnit 5.10.2 + Mockito 4.10.0, run with the Maven Wrapper (Surefire 3.2.5)
Steps to reproduce
- Create an OpenAPI 3.0 document with a schema
Ratethat has anumberpropertyfactorwithenum: [1.5]anddefault: 1.50(aNumberSchemawithnew BigDecimal("1.5")in the enum andnew BigDecimal("1.50")as the default). - Create a
RuleConfigurationwithisEnableRecommendations() = trueandisEnableDefaultNotInEnumRecommendation() = true. - Call
new OpenApiEvaluator(config).validate(openAPI). - Inspect
getWarnings()on the result.
Automated test case: CT-OE-14 (OpenApiEvaluatorJUnitTest.ct14NumericDefaultComparedByValue).
./mvnw -pl modules/openapi-generator -am test -Dtest=OpenApiEvaluatorJUnitTest -Dsurefire.failIfNoSpecifiedTests=false
Expected result
No warning: 1.50 and 1.5 are the same number, so the default belongs to the enum.
Actual result
1.50 reported as outside enum [1.5]: [Schema has default value '1.50' not in enum [1.5]]
==> expected: <true> but was: <false>
The failure occurred in all 3 Surefire attempts (rerunFailingTestsCount = 2), so it is not flaky.
Root cause
In OpenApiEvaluator.java, line 73:
&& !enumList.contains(defaultValue)) {
List.contains relies on equals. For BigDecimal, new BigDecimal("1.5").equals(new BigDecimal("1.50")) returns false, while compareTo returns 0.
Impact
Users get a false recommendation and may "fix" a default that is already correct. The defect was reproduced with a model built in memory (unit test). It was not verified whether swagger-parser preserves the scale when reading a YAML/JSON file written with a different number of decimal places (e.g. 1.50).
Suggested fix
Compare numeric values by value and keep equals for every other type. For example:
boolean inEnum = enumList.stream().anyMatch(item ->
(item instanceof BigDecimal && defaultValue instanceof BigDecimal)
? ((BigDecimal) item).compareTo((BigDecimal) defaultValue) == 0
: Objects.equals(item, defaultValue));
- Lenguaje dominante
- Java
- Estrellas
- 26.8k
- Forks
- 7.7k
- Merge medio
- 1 d 15 h
- PR fusionados (30 d)
- 146
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
-
[BUG][RUST] Reqwest - encoding of spaces in path parametersPosiblemente ocupada @GlenWright la tomó hoy. AbiertoIssue: Bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
OpenAPITools/openapi-generator#25257 · 3 comentarios ·
Los mantenedores suelen responder en 1 día
-
[BUG] ExampleGenerator returns the string "{}" (or null) for `type: object` schemas without `properties`Posiblemente ocupada Un pull request vinculado a esta issue está abierto o ya se fusionó. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
OpenAPITools/openapi-generator#25237 ·
Los mantenedores suelen responder en 1 día
-
[BUG] OpenApiEvaluator emits "Unused model" warnings even when recommendations are disabledPosiblemente ocupada @mohitduhan19 la tomó hace 1 día. AbiertoIssue: Bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
OpenAPITools/openapi-generator#25232 ·
Los mantenedores suelen responder en 1 día
-
[BUG][KOTLIN] multiplatform/jvm-ktor: `DELETE` without `requestBody` sends `"Content-Type: application/json"` with an empty bodyPosiblemente ocupada @mohitduhan19 la tomó hace 1 día. AbiertoIssue: Bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
OpenAPITools/openapi-generator#25223 ·
Los mantenedores suelen responder en 1 día
-
Issue: Bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
OpenAPITools/openapi-generator#25218 ·
Los mantenedores suelen responder en 1 día
Todos los issues de OpenAPITools/openapi-generator
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Los mantenedores suelen responder en 1 día
-
bug new windows
Dificultad 2/5 1-3 horas Aptitud para principiantes 62/100
Los mantenedores suelen responder en 1 día
-
iOS podspec depends on `React-Codegen`, which resolves to an unrelated pod on CocoaPods trunkAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
GantMan/jail-monkey#380 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
objectionary/jucs#145 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
objectionary/lints#1559 ·
Los mantenedores suelen responder en 2 días