File: openapiv3-nullable.t

package info (click to toggle)
libjson-validator-perl 4.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 828 kB
  • sloc: perl: 2,816; makefile: 14
file content (66 lines) | stat: -rw-r--r-- 1,745 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use Mojo::Base -strict;
use JSON::Validator;
use Test::More;

my $schema = JSON::Validator->new->schema('data://main/spec.json')->schema;
my ($body, @errors);

for my $path (qw(/nullable-data /nullable-ref)) {
  $body   = {exists => 1, value => {id => 42}};
  @errors = $schema->validate_response([get => $path], {body => \&body});
  is "@errors", "/body/name: Missing property.", "$path - missing name";

  $body   = {exists => 1, value => {id => 42, name => undef}};
  @errors = $schema->validate_response([get => $path], {body => \&body});
  is "@errors", "", "$path - name is undef";
}

done_testing;

sub body {$body}

__DATA__
@@ spec.json
{
  "openapi": "3.0.0",
  "info": { "title": "Nullable", "version": "" },
  "paths": {
    "/nullable-data": {
      "get": {
        "responses": {
          "200": {
            "content": { "application/json": { "schema": {"$ref": "#/components/schemas/WithNullable"} } }
          }
        }
      }
    },
    "/nullable-ref": {
      "get": {
        "operationId": "withNullableRef",
        "responses": {
          "200": {
            "content": { "application/json": { "schema": {"$ref": "#/components/schemas/WithNullableRef"} } }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "WithNullable": {
        "required": [ "id", "name" ],
        "properties": {
          "id": { "type": "integer", "format": "int64" },
          "name": { "type": "string", "nullable": true }
        }
      },
      "WithNullableRef": {
        "required": [ "id", "name" ],
        "properties": {
          "id": { "type": "integer", "format": "int64" },
          "name": { "$ref": "#/components/schemas/WithNullable/properties/name" }
        }
      }
    }
  }
}