feat: allow empty string as defaultValue in @Schema
Pull Request
Thank you for contributing to swagger-core!
Please fill out the following information to help us review your PR efficiently.
Description
This PR addresses the behavior where a defaultValue set to an empty string ("") on a @Schema annotation is ignored and not included in the generated OpenAPI specification.
- What problem does it solve? This resolves an inconsistency where
defaultValue = ""is treated the same as having nodefaultValueat all. This can cause problems for code generation tools (like for Dart or other languages) that rely on the presence of thedefaultValueproperty to differentiate optional fields from required ones. - Is it a bug fix, new feature, or refactor? This is best described as a feature enhancement (
feat). It improves an existing behavior to better align with developer expectations and broader tool compatibility. - Link to any related issues. This PR is related to several community discussions and issues regarding the handling of empty strings as default values.
Closes: #4838 (This is the issue you provided in the previous prompt)
Type of Change
- [ ] 🐛 Bug fix
- [x] ✨ New feature
- [ ] ♻️ Refactor (non-breaking change)
- [x] 🧪 Tests
- [ ] 📝 Documentation
- [ ] 🧹 Chore (build or tooling)
Checklist
- [x] I have added/updated tests as needed
- [ ] I have added/updated documentation where applicable
- [x] The PR title is descriptive
- [x] The code builds and passes tests locally
- [x] I have linked related issues (if any)
Screenshots / Additional Context
- This change modifies
AnnotationsUtils.javato processdefaultValueproperties even when they contain a blank or empty string. The conditionif (StringUtils.isNotBlank(schema.defaultValue()))is updated to a less restrictive check, such asif (schema.defaultValue() != null). - I have included a new test case (
emptyDefaultValue) that verifies the correct behavior. - The change is non-breaking and improves compatibility with downstream tools.
@juntae6942 thank you for your contribution. There are errors in your PR. Are you able to fix them?
daniel-kmiecik I’m working on the fix, but I’m running into some difficulties. Could you please provide me with some help?
@Schema(implementation = Integer.class)
@Schema(type = “string”)
when no defaultValue is specified, I believe the defaultValue is treated as "". However, the current test code does not include default: "", which leads to errors. I’d like to ask whether it would be acceptable to update the test code accordingly.
This causes issues in several tests.
Also, I'm facing an issue with the hasSchemaAnnotation method. For a schema where only the defaultValue is specified, simply removing the StringUtils.isBlank(schema.defaultValue()) check isn't enough. The method still incorrectly flags it as an empty annotation because all the other conditions are met. I'm trying to figure out the best way to solve this
Hi @juntae6942, Thanks a lot for working on this enhancement! :raised_hands: We'd like to hold off on reviewing/merging this for now, as we need to check how it may impact other repositories in the Swagger ecosystem. We'll get back to you once we've completed that assessment. Your contribution is appreciated, and we hope you'll continue to share improvements with the project!
I’ve also looked into that part, and since many changes would likely affect the test code, I agree with MichakrawSB’s opinion. I’ll proceed with additional work once you get in touch.