[Bug]: Example value starting with a number is parsed incorrectly
Description
When an example in an OpenAPI annotation (e.g., @Schema(example = "5 lacs per annum")) starts with a number, it appears that Swagger Core interprets the value as a numeric literal because the example begins with a digit. This results in the example being serialized as a number instead of a string.
For instance :
@Schema(example = "5 lacs per annum") private String salary;
In the generated OpenAPI schema, the example becomes:
"example": 5
instead of :
"example": "5 lacs per annum"
Where the issue occurs
The issue originates from the following code in io.swagger.v3.core.util.AnnotationsUtils#getSchemaFromAnnotation:
Because Json31.mapper().readTree(...) interprets any string starting with a digit as JSON numeric content, the portion "lacs per annum" is ignored and only the number 5 is retained.
Affected Version
swagger-core-jakarta: 2.2.36
springdoc-openapi: 2.8.13
For reproducing the issue below I have attached a demo project. Kindly take reference.
Hi! 👋 I'd like to work on this issue if it's not already being handled.
It seems the problem comes from AnnotationsUtils#getSchemaFromAnnotation,
where Json31.mapper().readTree(...) parses a numeric string as a number.
I plan to:
- Add a conditional check to treat examples starting with a digit as strings.
- Add a test case in
AnnotationsUtilsTestto verify this behavior.
Please let me know if it's okay for me to proceed. Thanks!
@zero2top We encourage you to prepare a fix for this issue. Once you have a pull request ready, we will review it and work with you on any feedback and final merge.
Thanks for your contribution!
Hi! 👋
I’ve completed the fix for this issue.
✔️ What I changed
- Updated
AnnotationsUtils#getSchemaFromAnnotationto ensure example values starting with digits are always treated as strings. - Added a test case
testExampleStartingWithNumberShouldBeStringinAnnotationsUtilsTestto validate this behavior.
📌 Status
The fix is implemented and tested locally — I’m preparing the pull request now.
Thanks!