Vertx-web: OpenAPI3 Validation problem with nullable property

Created on 22 Mar 2018  路  12Comments  路  Source: vert-x3/vertx-web

Version

  • vert.x core: 3.5.1
  • vert.x web: 3.5.1
  • vertx-web-api-contract: 3.5.1

Context

Given a spec that contains a model with a nullable property, and a request body that attempts to pass null, a validation error occurs.

Do you have a reproducer?

yaml:

openapi: 3.0.0
info:
  description: bug repro
  version: 1.0.0
  title: bug repro

paths:
  /v1/bug:
    post:
      operationId: bug
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Bug'
      responses:
        '200':
          description: Bug


components:

  responses:
    200_Bug:
      description: Bug
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/Bug'

  schemas:
    Bug:
      type: object
      properties:
        id:
          type: string
          readOnly: true
        name:
          type: string
        value:
          type: string
          nullable: true
      required:
        - name

Verticle

public class MainVerticle extends AbstractVerticle {

    private HttpServer httpServer;

    public MainVerticle() {

    }

    @Override
    public void stop(Future<Void> stopFuture) throws Exception {
        httpServer.close(ar -> {
            if (ar.succeeded()) {
                stopFuture.complete();
            } else {
                stopFuture.fail(ar.cause());
            }
        });
    }

    @Override
    public void start(Future<Void> startFuture) throws Exception {
        OpenAPI3RouterFactory.create(vertx, "src/main/resources/bugrepro.yaml", ar -> {
           if (ar.succeeded()) {
               final OpenAPI3RouterFactory routerFactory = ar.result();
               routerFactory.setOptions(new RouterFactoryOptions().setMountNotImplementedHandler(true).setMountValidationFailureHandler(true));
               final Router router = routerFactory.getRouter();
               httpServer = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
               httpServer.requestHandler(router::accept);
               httpServer.listen();
               startFuture.complete();
           } else {
               startFuture.fail(ar.cause());
           }
        });
    }
}

curl request

curl -X POST \
  http://localhost:8080/v1/bug \
  -H 'content-type: application/json' \
  -d '{
    "name": "Asdf",
    "value": null
}'

Expected Response:

501 not implemented

Actual Response

400 Bad Request
$.value: null found, string expected

Planned componenapi-contracbug

All 12 comments

The validator behind the scenes for json bodies is a draft-4 compatible validator, so for now we can't do nothing for it. give a look at this issues that explain the actual status of validation: https://github.com/vert-x3/vertx-web/issues/848

What sort of workaround is possible? I need to support nullable properties in my model, and this bug prevents me from using OpenAPI3RouterFactory

For now I haven't workaround, sorry :cry: In future releases we will rethink about validation totally

A possible workeround:

  schemas:
    Bug:
      type: object
      properties:
        id:
          type: string
          readOnly: true
        name:
          type: string
        value:
          oneOf:
            - type: string
            - type: null
      required:
        - name

But it's not compliant with OpenApi 3 specification

Sorry for the duplicate
Just to let you know, upgrading to the latest version of json-schema-validator did fix my problem. But I don't know if there are corner issues with this solution

Just saw it! Thank you for the advice! I update it for 3.6 release

Please check latest 3.6.0-SNAPSHOT, it should contain the bug fix

please set the milestone when you fix a bug.

@slinkydeveloper I tested the 3.6.0-SNAPSHOT and it works thank you

while working with 3.6.0. , during startup im getting warning

"[vert.x-eventloop-thread-0] WARN com.networknt.schema.JsonMetaSchema - Unknown keyword nullable - you should define your own Meta Schema. If the keyword is irrelevant for validation, just use a NonValidationKeyword
"

The same error on 4.0.0-milestone4
com.networknt.schema.JsonMetaSchema newValidator - Unknown keyword nullable - you should define your own Meta Schema. If the keyword is irrelevant for validation, just use a NonValidationKeyword

@ahoora08 did you tried using the new vertx-web-openapi? https://vertx-web-site.github.io/docs/vertx-web-openapi/java/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

LLfam picture LLfam  路  4Comments

elR1co picture elR1co  路  4Comments

shiprabehera picture shiprabehera  路  3Comments

AshtonKem picture AshtonKem  路  6Comments

DanielJoyce picture DanielJoyce  路  10Comments