[BUG] OpenApiEvaluator reports "default value not in enum" for numerically equal values with different scale (e.g. 1.5 and 1.50)
维护者通常 1 天内回复
评估
- 难度
- 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 内容生成。
描述
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));
- 主要语言
- Java
- 星标
- 26.8k
- 派生
- 7.7k
- 平均合并
- 1 天 14 小时
- 30 天内合并 PR
- 166
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 提供 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
OpenAPITools/openapi-generator 的其他 Issue
-
Enhancement: Feature
难度 2/5 1-3 小时 新手友好度 78/100
OpenAPITools/openapi-generator#25364 ·
维护者通常 1 天内回复
-
[BUG][RUST] chrono date-time path/query/header params are not serialized as RFC 3339可能已有人在做 @teovillanueva 于 8 天前认领。 未关闭
难度 2/5 1-3 小时 新手友好度 82/100
OpenAPITools/openapi-generator#25297 ·
维护者通常 1 天内回复
-
[BUG][RUST] Reqwest - encoding of spaces in path parameters可能已有人在做 @GlenWright 于 11 天前认领。 未关闭Issue: Bug
难度 2/5 1-3 小时 新手友好度 68/100
OpenAPITools/openapi-generator#25257 · 3 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
OpenAPITools/openapi-generator#25237 ·
维护者通常 1 天内回复
-
[BUG] OpenApiEvaluator emits "Unused model" warnings even when recommendations are disabled可能已有人在做 @mohitduhan19 于 12 天前认领。 未关闭Issue: Bug
难度 2/5 1-3 小时 新手友好度 82/100
OpenAPITools/openapi-generator#25232 ·
维护者通常 1 天内回复
查看 OpenAPITools/openapi-generator 的全部 Issue
相似的 Issue
-
难度 1/5 1 小时以内 新手友好度 75/100
521xueweihan/HelloGitHub#4105 ·
-
Awaiting further input from submitter
难度 2/5 1-3 小时 新手友好度 76/100
-
bug
难度 2/5 1-3 小时 新手友好度 82/100
-
bug
难度 2/5 1-3 小时 新手友好度 70/100
维护者通常 1 天内回复
-
code-maintenance
难度 2/5 1-3 小时 新手友好度 80/100
维护者通常 1 天内回复