Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SchemaValidationException with certain strings #98

Open
cavallium opened this issue Feb 23, 2025 · 1 comment
Open

SchemaValidationException with certain strings #98

cavallium opened this issue Feb 23, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@cavallium
Copy link
Contributor

Version

5.0.0.CR4

Context

I was testing my API, when I noticed that i encounter a SchemaValidationException when using strings that start with "+" in a query field.

Do you have a reproducer?

No

Steps to reproduce

Create an OpenAPI .yaml file with a field like this:

      parameters:
        - in: query
          name: phoneNumber
          required: true
          schema:
            type: string
            nullable: false
            example: "+39 334 192 1829"
            pattern: ^[ ]*\+([0-9\-/(). ]+)$

Then do the following request:

curl -v -X GET "http://127.0.0.1:8080/abcdefg?phoneNumber=+9999" -H  "accept: application/json" -H  "Content-Type: application/json"

It will result in the following error:

2025-02-23 18:29:31.649 ERROR 125971 RoutingContext       : Unhandled exception in router
io.vertx.ext.web.handler.HttpException: Bad Request
Caused by: io.vertx.openapi.validation.SchemaValidationException: The value of query parameter phoneNumber is invalid. Reason: Instance type number is invalid. Expected string
	at [email protected]/io.vertx.openapi.validation.SchemaValidationException.createInvalidValueParameter(SchemaValidationException.java:40)
	at [email protected]/io.vertx.openapi.validation.SchemaValidationException.createErrorFromOutputUnitType(SchemaValidationException.java:65)
	at [email protected]/io.vertx.openapi.validation.impl.RequestValidatorImpl.validateParameter(RequestValidatorImpl.java:135)
	at [email protected]/io.vertx.openapi.validation.impl.RequestValidatorImpl.lambda$validate$2(RequestValidatorImpl.java:101)
	at [email protected]/io.vertx.core.impl.WorkerPool$1.execute(WorkerPool.java:62)
	at [email protected]/io.vertx.core.impl.WorkerTask.run(WorkerTask.java:57)
	at [email protected]/io.vertx.core.impl.TaskQueue.run(TaskQueue.java:81)
	at [email protected]/io.vertx.core.impl.ThreadPerTaskExecutorService.lambda$execute$0(ThreadPerTaskExecutorService.java:82)
	at java.base/java.lang.VirtualThread.run(VirtualThread.java:329)
Caused by: io.vertx.json.schema.JsonSchemaValidationException: Instance type number is invalid. Expected string
	at [#/type].<#>(Unknown Source)

I noticed that the issue is related to the fact that validateParameter calls ParameterTransformer#transform, that calls transformPrimitive, in which there is this piece of code:

    boolean isString = STRING.equals(parameter.getSchemaType());
    if (isString && rawValue.matches("\\w+")) {
      return rawValue;
    }

    try {
      return Json.decodeValue(rawValue);
    } catch (DecodeException de) {
      if (rawValue.isEmpty()) {
        return rawValue;
      } else if ('"' == rawValue.charAt(0)) {
        throw de;
      } else {
        // let's try it as JSON String
        return transformPrimitive(parameter, "\"" + rawValue + "\"");
      }
    }
  }

If you notice, the string "+9999" does not match the regular expression, because "+" is not a "\w" character.
That causes it to fallback to Json.decodeValue("+9999"), that sees it as a Double, causing the error.

@cavallium cavallium added the bug Something isn't working label Feb 23, 2025
@pk-work
Copy link
Contributor

pk-work commented Feb 25, 2025

Hi @CheesyBoy123, it seems to be an issue in vertx-json-schema. Can you have a look?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants