Skip to content

[Bug]: Boolean example values are still serialized as strings (regression follow-up to #5061 / PR #5062) #5168

@jang-namu

Description

@jang-namu

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.472.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();

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions