jhthorsen / mojolicious-plugin-openapi

OpenAPI / Swagger plugin for Mojolicious
53 stars 41 forks source link

Nullable attribute ignored for objects #206

Closed yoe closed 3 years ago

yoe commented 3 years ago

Running this test case:

#!/usr/bin/perl -w

use strict;
use warnings;

use Mojolicious::Lite;

post "/object" => sub {
    my $c = shift->openapi->valid_input or return;

    my $data = $c->req->json;
    $data->{id} = 1;
    $c->render(openapi => $data);
}, "object";

plugin OpenAPI => {url => "data:///spec.yaml", schema => "v3"};
app->start;

__DATA__
@@ spec.yaml
openapi: 3.0.1
info:
  title: example
  description: example
  version: 1.0.0
servers:
- url: https://localhost/api/v1
tags:
- name: object
  description: managing objects
paths:
  /object:
    post:
      x-mojo-name: object
      tags:
      - object
      summary: Add a new object
      operationId: add_object
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/Object'
      responses:
        200:
          description: successful operation
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Object'
components:
  schemas:
    Object:
      type: object
      properties:
        id:
          type: integer
          format: int64
        name:
          type: string
          nullable: true
        flags:
          type: object
          nullable: true

... produces the following output when testing a few cURL commands:

$ curl -X POST -d '{"name":null}' -H 'Content-Type: application/json' http://localhost:3000/api/v1/object; echo
{"id":1,"name":null}
$ curl -X POST -d '{"flags":{}}' -H 'Content-Type: application/json' http://localhost:3000/api/v1/object; echo
{"flags":{},"id":1}
$ curl -X POST -d '{"flags":null}' -H 'Content-Type: application/json' http://localhost:3000/api/v1/object; echo
{"errors":[{"message":"Expected object - got null.","path":"\/body\/flags"}],"status":400}

Yet the flags object has nullable: true, so it should not fail.

This does not happen with version 3.41, but it does happen with 4.02, so that seems like a regression?

jhthorsen commented 3 years ago

This has been fixed in https://github.com/jhthorsen/json-validator/commit/0bdda95e46d495a5c1fea8e4ba468dc18cdd8618