Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

[BUG] OpenApiEvaluator reports "default value not in enum" for numerically equal values with different scale (e.g. 1.5 and 1.50)

未关闭 适合新手
#25,233 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@mohitduhan19 已经在做这个了。

开始于 2026年10月13日。

  • #25241 来自 @mohitduhan19 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
78/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
java, openapi

调研方向

从 openapi-generator 模块中的 OpenApiEvaluator#validate(OpenAPI) 和报告中提到的 CT-OE-14 测试 OpenApiEvaluatorJUnitTest.ct14NumericDefaultComparedByValue 开始。阅读现有的推荐检查,并运行 issue 中指定的针对性 Maven 测试命令。完成标准是:数值相等的 BigDecimal 值(例如 1.5 和 1.50)不会产生警告,同时其他 enum 比较保留当前行为。

由索引模型根据 Issue 内容生成。

描述

Issue: Bug
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
  1. Create an OpenAPI 3.0 document with a schema Rate that has a number property factor with enum: [1.5] and default: 1.50 (a NumberSchema with new BigDecimal("1.5") in the enum and new BigDecimal("1.50") as the default).
  2. Create a RuleConfiguration with isEnableRecommendations() = true and isEnableDefaultNotInEnumRecommendation() = true.
  3. Call new OpenApiEvaluator(config).validate(openAPI).
  4. 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));
主要语言
Java
星标
26.8k
派生
7.7k
平均合并
1 天 14 小时
30 天内合并 PR
166

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

OpenAPITools/openapi-generator 的其他 Issue

查看 OpenAPITools/openapi-generator 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。