Skip to content

fix: treat number example as number and not string#5062

Open
Mattias-Sehlstedt wants to merge 2 commits intoswagger-api:masterfrom
Mattias-Sehlstedt:treat-number-example-as-number-and-not-string
Open

fix: treat number example as number and not string#5062
Mattias-Sehlstedt wants to merge 2 commits intoswagger-api:masterfrom
Mattias-Sehlstedt:treat-number-example-as-number-and-not-string

Conversation

@Mattias-Sehlstedt
Copy link
Contributor

@Mattias-Sehlstedt Mattias-Sehlstedt commented Feb 23, 2026

Pull Request

Thank you for contributing to swagger-core!

Please fill out the following information to help us review your PR efficiently.


Description

Restores part of the schema example construction that was lost with PR #5046 and the change of this line.

The behavior that has been reintroduced is that an example for a number schema (number/integer) retains the old behavior of using the JsonNode as the example value, rather than using its String-representation. This will in turn lead to the example being a number in the specification.

This is of importance since the example value should align with the type defined in the schema.

Fixes: #5061

Type of Change

  • 🐛 Bug fix
  • ✨ New feature
  • ♻️ Refactor (non-breaking change)
  • 🧪 Tests
  • 📝 Documentation
  • 🧹 Chore (build or tooling)

Checklist

  • I have added/updated tests as needed
  • I have added/updated documentation where applicable
  • The PR title is descriptive
  • The code builds and passes tests locally
  • I have linked related issues (if any)

Screenshots / Additional Context

@Mattias-Sehlstedt Mattias-Sehlstedt force-pushed the treat-number-example-as-number-and-not-string branch from cf98b96 to 4e11031 Compare February 27, 2026 17:13
@Mattias-Sehlstedt
Copy link
Contributor Author

Mattias-Sehlstedt commented Feb 27, 2026

Edit: I have added test Issue5061Test to showcase that the issue presented below is managed by the fact that the field type is introspected and passed on to the AnnotationUtils as existingSchema. This in turn leads to the AnnotationUtils having the possibility to derive whether example="5" should become the number representation 5 or the string representation "5".


As raised in a comment on the issue this does not entirely corrects for the issue. This fix only corrects the behavior when the user explicitly states a type in the Schema annotation. If the type is not explicitly defined then it will for example still incorrectly generate an example for

private record ExampleDto(
  @Schema(example = "10.00") BigDecimal value) {
})
"ExampleDto": {
  "type": "object",
    "properties": {
      "exampleDec": {
        "type": "number",
        "example": "10.00"  <--
      }
    }
  }

The issue is that the example calculator currently does not have access to the field type. So if it sees "5" as an example, it can not properly derive whether it is a number and should be 5, or if it is a string and should be "5".

@Mattias-Sehlstedt Mattias-Sehlstedt force-pushed the treat-number-example-as-number-and-not-string branch from f47f42e to 6075b4f Compare February 27, 2026 18:31
@Mattias-Sehlstedt Mattias-Sehlstedt force-pushed the treat-number-example-as-number-and-not-string branch from 6075b4f to ec2b7a9 Compare February 27, 2026 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Number example treated as a string when generating swagger file.

1 participant