Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions t/conformance/test_issue201.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env resty
--- Conformance test ported from kin-openapi issue201_test.go
-- Tests duplicate path templates with different methods and overlapping path segments.
dofile("t/lib/test_bootstrap.lua")

local T = require("test_helper")
local cjson = require("cjson.safe")
local ov = require("resty.openapi_validator")

local spec = cjson.encode({
openapi = "3.0.0",
info = { title = "Sample API", version = "1.0.0" },
paths = {
["/users/{id}"] = {
get = {
parameters = {
{ name = "id", ["in"] = "path", required = true,
schema = { type = "string" } },
},
responses = { ["200"] = { description = "OK" } },
},
post = {
parameters = {
{ name = "id", ["in"] = "path", required = true,
schema = { type = "string" } },
},
requestBody = {
required = true,
content = {
["application/json"] = {
schema = {
type = "object",
required = { "name" },
properties = {
name = { type = "string" },
},
},
},
},
},
responses = { ["200"] = { description = "OK" } },
},
},
},
})

local v = ov.compile(spec)
assert(v, "compile failed")

T.describe("issue201: GET /users/123 (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/users/123",
})
T.ok(ok, "should pass: " .. tostring(err))
end)

T.describe("issue201: POST /users/123 with valid body (valid)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/users/123",
body = '{"name": "alice"}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(ok, "should pass: " .. tostring(err))
end)

T.describe("issue201: POST /users/123 missing required body field (fail)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/users/123",
body = '{}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(not ok, "should fail - missing required field 'name'")
end)

T.describe("issue201: GET /users/abc string id (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/users/abc",
})
T.ok(ok, "should pass: " .. tostring(err))
end)
Comment thread
jarvis9443 marked this conversation as resolved.

T.done()
149 changes: 149 additions & 0 deletions t/conformance/test_issue639.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#!/usr/bin/env resty
--- Conformance test ported from kin-openapi issue639_test.go
-- Tests request body decode edge cases: empty objects, optional bodies,
-- additional properties, and nested object validation.
dofile("t/lib/test_bootstrap.lua")

local T = require("test_helper")
local cjson = require("cjson.safe")
local ov = require("resty.openapi_validator")

local spec = cjson.encode({
openapi = "3.0.0",
info = { title = "Sample API", version = "1.0.0" },
paths = {
["/items"] = {
post = {
requestBody = {
required = false,
content = {
["application/json"] = {
schema = {
type = "object",
properties = {
name = { type = "string" },
count = { type = "integer" },
metadata = {
type = "object",
properties = {
tags = {
type = "array",
items = { type = "string" },
},
nested = {
type = "object",
properties = {
level = { type = "integer" },
},
},
},
},
},
},
},
},
},
responses = { ["200"] = { description = "OK" } },
},
},
["/strict"] = {
post = {
requestBody = {
required = true,
content = {
["application/json"] = {
schema = {
type = "object",
required = { "id" },
properties = {
id = { type = "integer" },
label = { type = "string" },
},
},
},
},
},
responses = { ["200"] = { description = "OK" } },
},
},
},
})

local v = ov.compile(spec)
assert(v, "compile failed")

T.describe("issue639: empty object with only optional properties (valid)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/items",
body = "{}",
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(ok, "should pass: " .. tostring(err))
end)

T.describe("issue639: no body when body is not required (valid)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/items",
})
T.ok(ok, "should pass: " .. tostring(err))
end)

T.describe("issue639: object with all fields (valid)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/items",
body = '{"name": "widget", "count": 5}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(ok, "should pass: " .. tostring(err))
end)

T.describe("issue639: object with additional properties (valid - no restriction)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/items",
body = '{"name": "widget", "extra_field": "hello", "another": 42}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(ok, "should pass: " .. tostring(err))
end)

T.describe("issue639: deeply nested valid object (valid)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/items",
body = '{"name": "widget", "metadata": {"tags": ["a", "b"], "nested": {"level": 3}}}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(ok, "should pass: " .. tostring(err))
end)

T.describe("issue639: missing required field in /strict (fail)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/strict",
body = '{"label": "test"}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(not ok, "should fail - missing required field 'id'")
end)

T.describe("issue639: valid required field in /strict (valid)", function()
local ok, err = v:validate_request({
method = "POST",
path = "/strict",
body = '{"id": 1, "label": "test"}',
content_type = "application/json",
headers = { ["content-type"] = "application/json" },
})
T.ok(ok, "should pass: " .. tostring(err))
end)

T.done()
143 changes: 143 additions & 0 deletions t/conformance/test_issue707.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#!/usr/bin/env resty
--- Conformance test ported from kin-openapi issue707_test.go
-- Tests path parameter edge cases: type coercion, multiple params,
-- and min/max constraints.
dofile("t/lib/test_bootstrap.lua")

local T = require("test_helper")
local cjson = require("cjson.safe")
local ov = require("resty.openapi_validator")

local spec = cjson.encode({
openapi = "3.0.0",
info = { title = "Sample API", version = "1.0.0" },
paths = {
["/items/{itemId}"] = {
get = {
parameters = {
{ name = "itemId", ["in"] = "path", required = true,
schema = { type = "string" } },
},
responses = { ["200"] = { description = "OK" } },
},
},
["/orders/{orderId}"] = {
get = {
parameters = {
{ name = "orderId", ["in"] = "path", required = true,
schema = { type = "integer" } },
},
responses = { ["200"] = { description = "OK" } },
},
},
["/bounded/{val}"] = {
get = {
parameters = {
{ name = "val", ["in"] = "path", required = true,
schema = { type = "integer", minimum = 1, maximum = 100 } },
},
responses = { ["200"] = { description = "OK" } },
},
},
["/multi/{region}/{id}"] = {
get = {
parameters = {
{ name = "region", ["in"] = "path", required = true,
schema = { type = "string" } },
{ name = "id", ["in"] = "path", required = true,
schema = { type = "integer" } },
},
responses = { ["200"] = { description = "OK" } },
},
},
},
})

local v = ov.compile(spec)
assert(v, "compile failed")

-- String path param accepts anything
T.describe("issue707: string path param with numeric-looking value (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/items/42",
})
T.ok(ok, "should pass: " .. tostring(err))
end)

T.describe("issue707: string path param with alpha value (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/items/hello-world",
})
T.ok(ok, "should pass: " .. tostring(err))
end)

-- Integer path param rejects non-integer
T.describe("issue707: integer path param with valid integer (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/orders/999",
})
T.ok(ok, "should pass: " .. tostring(err))
end)

T.describe("issue707: integer path param with non-integer (fail)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/orders/not-a-number",
})
T.ok(not ok, "should fail - 'not-a-number' is not an integer")
end)

-- Bounded integer path param
T.describe("issue707: bounded path param within range (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/bounded/50",
})
T.ok(ok, "should pass: " .. tostring(err))
end)

T.describe("issue707: bounded path param at minimum (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/bounded/1",
})
T.ok(ok, "should pass: " .. tostring(err))
end)

T.describe("issue707: bounded path param below minimum (fail)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/bounded/0",
})
T.ok(not ok, "should fail - 0 is below minimum 1")
end)

T.describe("issue707: bounded path param above maximum (fail)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/bounded/101",
})
T.ok(not ok, "should fail - 101 is above maximum 100")
end)

-- Multiple path params
T.describe("issue707: multiple path params both valid (valid)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/multi/us-east/42",
})
T.ok(ok, "should pass: " .. tostring(err))
end)

T.describe("issue707: multiple path params, integer param invalid (fail)", function()
local ok, err = v:validate_request({
method = "GET",
path = "/multi/eu-west/abc",
})
T.ok(not ok, "should fail - 'abc' is not a valid integer for id param")
end)

T.done()
Loading