[BUG] OpenApiEvaluator reports "default value not in enum" for numerically equal values with different scale (e.g. 1.5 and 1.50)
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 78/100
- Tipo di issue
- Bug
- Chiarezza
- Specificata chiaramente
- Stato di attività
- Attiva
- Stack tecnologico
- java, openapi
- Ambito
- backend-api-design, testing
Direzione di ricerca
Inizia da OpenApiEvaluator#validate(OpenAPI) nel modulo openapi-generator e dal test CT-OE-14 segnalato, OpenApiEvaluatorJUnitTest.ct14NumericDefaultComparedByValue. Leggi il controllo della raccomandazione esistente ed esegui il comando Maven mirato al test indicato nell’issue. Il risultato è raggiunto quando valori BigDecimal numericamente uguali, come 1.5 e 1.50, non producono un avviso, mentre gli altri confronti tra enum mantengono il comportamento attuale.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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));
- Lingua principale
- Java
- Stelle
- 26.8k
- Fork
- 7.7k
- Merge medio
- 1g 15h
- PR unite (30g)
- 146
Preparare l'ambiente
Avvia il container di sviluppo del progetto nel browser, con il tuo account GitHub.
- Include un Dockerfile o un file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di OpenAPITools/openapi-generator
-
[BUG][RUST] Reqwest - encoding of spaces in path parametersForse già presa @GlenWright l’ha presa 1 giorno fa. ApertaIssue: Bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
OpenAPITools/openapi-generator#25257 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
[BUG] ExampleGenerator returns the string "{}" (or null) for `type: object` schemas without `properties`Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
OpenAPITools/openapi-generator#25237 ·
I maintainer di solito rispondono entro 1 giorno
-
[BUG] OpenApiEvaluator emits "Unused model" warnings even when recommendations are disabledForse già presa @mohitduhan19 l’ha presa 2 giorni fa. ApertaIssue: Bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
OpenAPITools/openapi-generator#25232 ·
I maintainer di solito rispondono entro 1 giorno
-
[BUG][KOTLIN] multiplatform/jvm-ktor: `DELETE` without `requestBody` sends `"Content-Type: application/json"` with an empty bodyForse già presa @mohitduhan19 l’ha presa 2 giorni fa. ApertaIssue: Bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
OpenAPITools/openapi-generator#25223 ·
I maintainer di solito rispondono entro 1 giorno
-
Issue: Bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
OpenAPITools/openapi-generator#25218 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di OpenAPITools/openapi-generator
Issue simili
-
agent-java community triage
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
elastic/apm-agent-java#4594 ·
I maintainer di solito rispondono entro 1 giorno
-
area/plugin good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 85/100
kestra-io/plugin-soda#137 ·
-
area/plugin good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
kestra-io/plugin-aws#816 ·
I maintainer di solito rispondono entro 2 giorni
-
area/plugin good first issue
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
kestra-io/plugin-kestra#200 ·
I maintainer di solito rispondono entro 1 giorno
-
bug untriaged
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 92/100
opensearch-project/search-relevance#618 ·
I maintainer di solito rispondono entro 3 giorni