-
Notifications
You must be signed in to change notification settings - Fork 0
test: port conformance tests for kin-openapi issues 201, 639, 707 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
|
|
||
| T.done() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.