Description of the problem/issue
PR #5062 (released in v2.2.47) fixed the regression reported in #5061 for number /
integer example values. However, the same regression still affects boolean schemas:
@Schema(examples = {"true"}) on a Boolean field continues to be serialized as the JSON
string "true" instead of the JSON boolean true.
Root cause is in the helper introduced by #5062,
AnnotationsUtils.shouldUseNodeAsExample(JsonNode, Schema):
private static boolean shouldUseNodeAsExample(JsonNode node, Schema schemaObject) {
if (node.isObject() || node.isArray()) {
return true;
}
if (schemaObject != null && SchemaTypeUtils.isNumberSchema(schemaObject)) {
return true;
}
if (schemaObject != null && SchemaTypeUtils.isStringSchema(schemaObject)) {
return false;
}
return node.isNumber();
}
For a boolean schema the method falls through to return node.isNumber(), which is false for
a BooleanNode. As a result parseExamplesArray adds the trimmed string instead of the
parsed JsonNode, and the generated OpenAPI document contains a string example for a boolean
property.
Affected Version
2.2.47 – 2.2.49 (latest as of writing). Also reproduces through springdoc-openapi 2.8.17.
Steps to Reproduce
public class User {
@Schema(description = "Email verified", examples = {"true"})
private Boolean verified;
}
Generate the OAS document.
Expected Behavior
public class User {
@Schema(description = "Email verified", examples = {"true"})
private Boolean verified;
} ```
Generate the OAS document.
## Expected Behavior
```yaml
verified:
type: boolean
examples:
- true
Actual Behavior
type: boolean
examples:
- "true"
Suggested Fix
Extend shouldUseNodeAsExample to also return true when the schema is a boolean schema (or
when node.isBoolean()), e.g.:
if (schemaObject != null && SchemaTypeUtils.isBooleanSchema(schemaObject)) {
return true;
}
// ...
return node.isNumber() || node.isBoolean();
Description of the problem/issue
PR #5062 (released in
v2.2.47) fixed the regression reported in #5061 fornumber/integerexample values. However, the same regression still affects boolean schemas:@Schema(examples = {"true"})on aBooleanfield continues to be serialized as the JSONstring
"true"instead of the JSON booleantrue.Root cause is in the helper introduced by #5062,
AnnotationsUtils.shouldUseNodeAsExample(JsonNode, Schema):For a boolean schema the method falls through to
return node.isNumber(), which isfalsefora
BooleanNode. As a resultparseExamplesArrayadds the trimmed string instead of theparsed
JsonNode, and the generated OpenAPI document contains a string example for a booleanproperty.
Affected Version
2.2.47–2.2.49(latest as of writing). Also reproduces throughspringdoc-openapi 2.8.17.Steps to Reproduce
Generate the OAS document.
Expected Behavior
Actual Behavior
Suggested Fix
Extend
shouldUseNodeAsExampleto also returntruewhen the schema is a boolean schema (orwhen
node.isBoolean()), e.g.: