|
| 1 | +#!/usr/bin/env resty |
| 2 | +--- Conformance test ported from kin-openapi issue639_test.go |
| 3 | +-- Tests request body decode edge cases: empty objects, optional bodies, |
| 4 | +-- additional properties, and nested object validation. |
| 5 | +dofile("t/lib/test_bootstrap.lua") |
| 6 | + |
| 7 | +local T = require("test_helper") |
| 8 | +local cjson = require("cjson.safe") |
| 9 | +local ov = require("resty.openapi_validator") |
| 10 | + |
| 11 | +local spec = cjson.encode({ |
| 12 | + openapi = "3.0.0", |
| 13 | + info = { title = "Sample API", version = "1.0.0" }, |
| 14 | + paths = { |
| 15 | + ["/items"] = { |
| 16 | + post = { |
| 17 | + requestBody = { |
| 18 | + required = false, |
| 19 | + content = { |
| 20 | + ["application/json"] = { |
| 21 | + schema = { |
| 22 | + type = "object", |
| 23 | + properties = { |
| 24 | + name = { type = "string" }, |
| 25 | + count = { type = "integer" }, |
| 26 | + metadata = { |
| 27 | + type = "object", |
| 28 | + properties = { |
| 29 | + tags = { |
| 30 | + type = "array", |
| 31 | + items = { type = "string" }, |
| 32 | + }, |
| 33 | + nested = { |
| 34 | + type = "object", |
| 35 | + properties = { |
| 36 | + level = { type = "integer" }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + responses = { ["200"] = { description = "OK" } }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + ["/strict"] = { |
| 50 | + post = { |
| 51 | + requestBody = { |
| 52 | + required = true, |
| 53 | + content = { |
| 54 | + ["application/json"] = { |
| 55 | + schema = { |
| 56 | + type = "object", |
| 57 | + required = { "id" }, |
| 58 | + properties = { |
| 59 | + id = { type = "integer" }, |
| 60 | + label = { type = "string" }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + responses = { ["200"] = { description = "OK" } }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | +}) |
| 71 | + |
| 72 | +local v = ov.compile(spec) |
| 73 | +assert(v, "compile failed") |
| 74 | + |
| 75 | +T.describe("issue639: empty object with only optional properties (valid)", function() |
| 76 | + local ok, err = v:validate_request({ |
| 77 | + method = "POST", |
| 78 | + path = "/items", |
| 79 | + body = "{}", |
| 80 | + content_type = "application/json", |
| 81 | + headers = { ["content-type"] = "application/json" }, |
| 82 | + }) |
| 83 | + T.ok(ok, "should pass: " .. tostring(err)) |
| 84 | +end) |
| 85 | + |
| 86 | +T.describe("issue639: no body when body is not required (valid)", function() |
| 87 | + local ok, err = v:validate_request({ |
| 88 | + method = "POST", |
| 89 | + path = "/items", |
| 90 | + }) |
| 91 | + T.ok(ok, "should pass: " .. tostring(err)) |
| 92 | +end) |
| 93 | + |
| 94 | +T.describe("issue639: object with all fields (valid)", function() |
| 95 | + local ok, err = v:validate_request({ |
| 96 | + method = "POST", |
| 97 | + path = "/items", |
| 98 | + body = '{"name": "widget", "count": 5}', |
| 99 | + content_type = "application/json", |
| 100 | + headers = { ["content-type"] = "application/json" }, |
| 101 | + }) |
| 102 | + T.ok(ok, "should pass: " .. tostring(err)) |
| 103 | +end) |
| 104 | + |
| 105 | +T.describe("issue639: object with additional properties (valid - no restriction)", function() |
| 106 | + local ok, err = v:validate_request({ |
| 107 | + method = "POST", |
| 108 | + path = "/items", |
| 109 | + body = '{"name": "widget", "extra_field": "hello", "another": 42}', |
| 110 | + content_type = "application/json", |
| 111 | + headers = { ["content-type"] = "application/json" }, |
| 112 | + }) |
| 113 | + T.ok(ok, "should pass: " .. tostring(err)) |
| 114 | +end) |
| 115 | + |
| 116 | +T.describe("issue639: deeply nested valid object (valid)", function() |
| 117 | + local ok, err = v:validate_request({ |
| 118 | + method = "POST", |
| 119 | + path = "/items", |
| 120 | + body = '{"name": "widget", "metadata": {"tags": ["a", "b"], "nested": {"level": 3}}}', |
| 121 | + content_type = "application/json", |
| 122 | + headers = { ["content-type"] = "application/json" }, |
| 123 | + }) |
| 124 | + T.ok(ok, "should pass: " .. tostring(err)) |
| 125 | +end) |
| 126 | + |
| 127 | +T.describe("issue639: missing required field in /strict (fail)", function() |
| 128 | + local ok, err = v:validate_request({ |
| 129 | + method = "POST", |
| 130 | + path = "/strict", |
| 131 | + body = '{"label": "test"}', |
| 132 | + content_type = "application/json", |
| 133 | + headers = { ["content-type"] = "application/json" }, |
| 134 | + }) |
| 135 | + T.ok(not ok, "should fail - missing required field 'id'") |
| 136 | +end) |
| 137 | + |
| 138 | +T.describe("issue639: valid required field in /strict (valid)", function() |
| 139 | + local ok, err = v:validate_request({ |
| 140 | + method = "POST", |
| 141 | + path = "/strict", |
| 142 | + body = '{"id": 1, "label": "test"}', |
| 143 | + content_type = "application/json", |
| 144 | + headers = { ["content-type"] = "application/json" }, |
| 145 | + }) |
| 146 | + T.ok(ok, "should pass: " .. tostring(err)) |
| 147 | +end) |
| 148 | + |
| 149 | +T.done() |
0 commit comments