File: openapiv3-nullable.t

package info (click to toggle)
libjson-validator-perl 5.14%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,160 kB
  • sloc: perl: 3,015; makefile: 14
file content (157 lines) | stat: -rwxr-xr-x 4,408 bytes parent folder | download | duplicates (2)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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";
}

for my $extra ({}, undef) {
  $body   = {exists => 1, value => {extra => $extra, id => 42, name => undef}};
  @errors = $schema->validate_response([get => '/nullable-data'], {body => \&body});
  is "@errors", "", sprintf 'extra %s', $extra ? 'object' : 'null';
}

for my $stuff ([], undef) {
  $body   = {exists => 1, value => {stuff => $stuff, id => 42, name => undef}};
  @errors = $schema->validate_response([get => '/nullable-data'], {body => \&body});
  is "@errors", "", sprintf 'stuff %s', $stuff ? 'array' : 'null';
}

$schema = JSON::Validator->new->schema('data://main/issue-241.json')->schema;
$body   = {exists => 1, value => {name => undef}};
@errors = $schema->validate_response([get => '/test'], {body => \&body});
is "@errors", "", "nullable inside oneOf";

$schema = JSON::Validator->new->schema('data://main/nullable-with-enum.json')->schema;
$body   = {exists => 1, value => {name => undef}};
@errors = $schema->validate_response([get => '/test'], {body => \&body});
is "@errors", "", "nullable string with enum";

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": {
          "extra": { "type": "object", "nullable": true },
          "id": { "type": "integer", "format": "int64" },
          "name": { "type": "string", "nullable": true },
          "stuff": { "type": "array", "nullable": true }
        }
      },
      "WithNullableRef": {
        "required": [ "id", "name" ],
        "properties": {
          "id": { "type": "integer", "format": "int64" },
          "name": { "$ref": "#/components/schemas/WithNullable/properties/name" }
        }
      }
    }
  }
}
@@ issue-241.json
{
  "openapi": "3.0.0",
  "info": { "title": "Nullable", "version": "" },
  "paths": {
    "/test": {
      "get": {
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "name": {
                      "oneOf": [
                        { "$ref": "#/components/schemas/name1" },
                        { "$ref": "#/components/schemas/name2" }
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "name1": { "type": "string", "nullable": "true" },
      "name2": { "type": "integer" } }
  }
}
@@ nullable-with-enum.json
{
  "openapi": "3.0.0",
  "info": { "title": "Nullable", "version": "" },
  "paths": {
    "/test": {
      "get": {
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "name": {
                      "$ref": "#/components/schemas/name"
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "name": { "type": "string", "nullable": "true", "enum": ["foo", "bar"] }
    }
  }
}