Inconsistent validaton of boolean parameters

Issue #373 new
Jan Calta created an issue

Handling of invalid parameters of boolean type is inconsistent - if a parameter is passed with a string value (other than “true“ or “false“, e.g. “something“), the validaiton fails on JSON parsing with misleading error message, instead of recognizing a type mismatch.

Example with value "something”

Actual:
"Unable to parse JSON - Unrecognized token 'something': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')\n at [Source: (String)"something"

Expected:

"Instance type (string) does not match any allowed primitive type (allowed: ["boolean"])"

Note: Validation works as expected for number values, e.g. 1.2

My guess is code in readContent in SchemaValidator doesn’t cover the boolean case, which results in a failure on interpreting the param value as JSON, which only works for numbers, but not for strings:

private static JsonNode readContent(@Nonnull final String value, @Nonnull final Schema schema) throws IOException {
        if ("null".equalsIgnoreCase(value)) {
            return Json.mapper().readTree("null");
        }
        if (schema instanceof DateTimeSchema) {
            return createStringNode(normaliseDateTime(value));
        }
        if ("string".equalsIgnoreCase(schema.getType())) {
            return createStringNode(value);
        }
        if ("number".equalsIgnoreCase(schema.getType()) ||
                "integer".equalsIgnoreCase(schema.getType())) {
            return createNumericNode(value);
        }
        return Json.mapper().readTree(value);
    }

Minimal example:

Schema:

openapi: 3.0.2
info:
  title: Test API v1
  version: 1.0.0

paths:
  /resources/{ResourceId}:
    get:
      operationId: getResourceById
      tags:
        - Query
      parameters:
        - $ref: "#/components/parameters/Boolparam"
      responses:
        "200":
          $ref: "#/components/responses/ResourceDetailsResponse"


components:
  parameters:
    Boolparam:
      in: query
      name: boolparam
      schema:
        type: boolean

Request:

localhost/resources/someid?boolparam=something

Comments (0)

  1. Log in to comment