swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

[Bug]: Example value starting with a number is parsed incorrectly

Open Sagr-gupta opened this issue 2 months ago • 3 comments

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:

Image

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.

openapi.zip

Sagr-gupta avatar Oct 18 '25 19:10 Sagr-gupta

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 AnnotationsUtilsTest to verify this behavior.

Please let me know if it's okay for me to proceed. Thanks!

zero2top avatar Oct 31 '25 07:10 zero2top

@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!

daniel-kmiecik avatar Nov 03 '25 11:11 daniel-kmiecik

Hi! 👋

I’ve completed the fix for this issue.

✔️ What I changed

  • Updated AnnotationsUtils#getSchemaFromAnnotation to ensure example values starting with digits are always treated as strings.
  • Added a test case testExampleStartingWithNumberShouldBeString in AnnotationsUtilsTest to validate this behavior.

📌 Status

The fix is implemented and tested locally — I’m preparing the pull request now.

Thanks!

zero2top avatar Nov 21 '25 08:11 zero2top