-
Notifications
You must be signed in to change notification settings - Fork 42
Reject unsupported OpenAPI versions in the lint command
#887
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| #include <sourcemeta/core/json.h> | ||
| #include <sourcemeta/core/jsonpointer.h> | ||
| #include <sourcemeta/core/jsonschema.h> | ||
| #include <sourcemeta/core/openapi.h> | ||
| #include <sourcemeta/core/options.h> | ||
| #include <sourcemeta/core/uri.h> | ||
| #include <sourcemeta/core/yaml.h> | ||
|
|
@@ -116,6 +117,25 @@ inline auto looks_like_test_document(const sourcemeta::core::JSON &document) | |
| document.defines("tests") && document.at("tests").is_array(); | ||
| } | ||
|
|
||
| // The revision an OpenAPI description declares, when it is one we cannot read. | ||
| // A document that declares the field as anything but a string is no OpenAPI | ||
| // description by any reading of the specification, so it goes on being read as | ||
| // a schema rather than being turned down here | ||
| inline auto unsupported_openapi_version(const sourcemeta::core::JSON &document) | ||
| -> const sourcemeta::core::JSON * { | ||
| if (!document.is_object()) { | ||
| return nullptr; | ||
| } | ||
|
|
||
| const auto *version{document.try_at("openapi")}; | ||
| if (version == nullptr || !version->is_string()) { | ||
| return nullptr; | ||
| } | ||
|
|
||
| return sourcemeta::core::openapi_version(document).has_value() ? nullptr | ||
| : version; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: Only classify an unrecognized Prompt for AI agents |
||
| } | ||
|
|
||
| inline auto default_dialect( | ||
| const sourcemeta::core::Options &options, | ||
| const std::optional<sourcemeta::blaze::Configuration> &configuration) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| WRITE openapi.json UNTIL EOF | ||
| { | ||
| "openapi": "3.0.3", | ||
| "info": { "title": "Example", "version": "1.0.0" }, | ||
| "components": { | ||
| "schemas": { | ||
| "Pet": { | ||
| "type": "string", | ||
| "const": "dog" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| EOF | ||
|
|
||
| // Not supported | ||
| RUN lint openapi.json STDIN /dev/null IN . INTO result_0.txt EXPECTING 3 | ||
|
|
||
| REPLACE $CWD WITH '[CWD]' IN result_0.txt | ||
|
|
||
| WRITE expected_0.txt UNTIL EOF | ||
| 2> error: This OpenAPI Specification revision is not supported | ||
| 2> at value 3.0.3 | ||
| 2> at file path [CWD]/openapi.json | ||
| EOF | ||
|
|
||
| COMPARE result_0.txt AGAINST expected_0.txt | ||
|
|
||
| // Not supported | ||
| RUN lint openapi.json --json STDIN /dev/null IN . INTO result_1.txt EXPECTING 3 | ||
|
|
||
| REPLACE $CWD WITH '[CWD]' IN result_1.txt | ||
|
|
||
| WRITE expected_1.txt UNTIL EOF | ||
| 1> { | ||
| 1> "error": "This OpenAPI Specification revision is not supported", | ||
| 1> "value": "3.0.3", | ||
| 1> "filePath": "[CWD]/openapi.json" | ||
| 1> } | ||
| EOF | ||
|
|
||
| COMPARE result_1.txt AGAINST expected_1.txt | ||
|
|
||
| // Not supported | ||
| RUN lint openapi.json --fix STDIN /dev/null IN . INTO result_2.txt EXPECTING 3 | ||
|
|
||
| REPLACE $CWD WITH '[CWD]' IN result_2.txt | ||
|
|
||
| WRITE expected_2.txt UNTIL EOF | ||
| 2> error: This OpenAPI Specification revision is not supported | ||
| 2> at value 3.0.3 | ||
| 2> at file path [CWD]/openapi.json | ||
| EOF | ||
|
|
||
| COMPARE result_2.txt AGAINST expected_2.txt | ||
|
|
||
| WRITE expected_file_0.txt UNTIL EOF | ||
| { | ||
| "openapi": "3.0.3", | ||
| "info": { "title": "Example", "version": "1.0.0" }, | ||
| "components": { | ||
| "schemas": { | ||
| "Pet": { | ||
| "type": "string", | ||
| "const": "dog" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| EOF | ||
|
|
||
| COMPARE openapi.json AGAINST expected_file_0.txt |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| WRITE openapi.json UNTIL EOF | ||
| { | ||
| "openapi": "3.1", | ||
| "info": { "title": "Example", "version": "1.0.0" }, | ||
| "components": { | ||
| "schemas": { | ||
| "Pet": { | ||
| "type": "string", | ||
| "const": "dog" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| EOF | ||
|
|
||
| // Not supported | ||
| RUN lint openapi.json STDIN /dev/null IN . INTO result_0.txt EXPECTING 3 | ||
|
|
||
| REPLACE $CWD WITH '[CWD]' IN result_0.txt | ||
|
|
||
| WRITE expected_0.txt UNTIL EOF | ||
| 2> error: This OpenAPI Specification revision is not supported | ||
| 2> at value 3.1 | ||
| 2> at file path [CWD]/openapi.json | ||
| EOF | ||
|
|
||
| COMPARE result_0.txt AGAINST expected_0.txt | ||
|
|
||
| // Not supported | ||
| RUN lint openapi.json --json STDIN /dev/null IN . INTO result_1.txt EXPECTING 3 | ||
|
|
||
| REPLACE $CWD WITH '[CWD]' IN result_1.txt | ||
|
|
||
| WRITE expected_1.txt UNTIL EOF | ||
| 1> { | ||
| 1> "error": "This OpenAPI Specification revision is not supported", | ||
| 1> "value": "3.1", | ||
| 1> "filePath": "[CWD]/openapi.json" | ||
| 1> } | ||
| EOF | ||
|
|
||
| COMPARE result_1.txt AGAINST expected_1.txt |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| WRITE openapi.json UNTIL EOF | ||
| { | ||
| "openapi": "3.3.0", | ||
| "info": { "title": "Example", "version": "1.0.0" }, | ||
| "components": { | ||
| "schemas": { | ||
| "Pet": { | ||
| "type": "string", | ||
| "const": "dog" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| EOF | ||
|
|
||
| // Not supported | ||
| RUN lint openapi.json STDIN /dev/null IN . INTO result_0.txt EXPECTING 3 | ||
|
|
||
| REPLACE $CWD WITH '[CWD]' IN result_0.txt | ||
|
|
||
| WRITE expected_0.txt UNTIL EOF | ||
| 2> error: This OpenAPI Specification revision is not supported | ||
| 2> at value 3.3.0 | ||
| 2> at file path [CWD]/openapi.json | ||
| EOF | ||
|
|
||
| COMPARE result_0.txt AGAINST expected_0.txt | ||
|
|
||
| // Not supported | ||
| RUN lint openapi.json --json STDIN /dev/null IN . INTO result_1.txt EXPECTING 3 | ||
|
|
||
| REPLACE $CWD WITH '[CWD]' IN result_1.txt | ||
|
|
||
| WRITE expected_1.txt UNTIL EOF | ||
| 1> { | ||
| 1> "error": "This OpenAPI Specification revision is not supported", | ||
| 1> "value": "3.3.0", | ||
| 1> "filePath": "[CWD]/openapi.json" | ||
| 1> } | ||
| EOF | ||
|
|
||
| COMPARE result_1.txt AGAINST expected_1.txt |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,44 @@ | ||||||
| WRITE schema.json UNTIL EOF | ||||||
| { | ||||||
| "$schema": "https://json-schema.org/draft/2020-12/schema", | ||||||
| "title": "Test", | ||||||
| "description": "Test schema", | ||||||
| "examples": [ 1 ], | ||||||
| "openapi": 3 | ||||||
| } | ||||||
| EOF | ||||||
|
|
||||||
| // Validation failure | ||||||
| RUN lint schema.json STDIN /dev/null IN . INTO result_0.txt EXPECTING 2 | ||||||
|
|
||||||
| WRITE expected_0.txt UNTIL EOF | ||||||
| 1> schema.json:6:3: | ||||||
| 1> Future versions of JSON Schema will refuse to evaluate unknown keywords or custom keywords from optional vocabularies that don't have an x- prefix (unknown_keywords_prefix) | ||||||
| 1> at location "/openapi" | ||||||
| EOF | ||||||
|
|
||||||
| COMPARE result_0.txt AGAINST expected_0.txt | ||||||
|
|
||||||
| // Validation failure | ||||||
| RUN lint schema.json --json STDIN /dev/null IN . INTO result_1.txt EXPECTING 2 | ||||||
|
|
||||||
| REPLACE $CWD WITH '[CWD]' IN result_1.txt | ||||||
|
|
||||||
| WRITE expected_1.txt UNTIL EOF | ||||||
| 1> { | ||||||
| 1> "valid": false, | ||||||
| 1> "health": 0, | ||||||
| 1> "errors": [ | ||||||
| 1> { | ||||||
| 1> "path": "[CWD]/schema.json", | ||||||
| 1> "id": "unknown_keywords_prefix", | ||||||
| 1> "message": "Future versions of JSON Schema will refuse to evaluate unknown keywords or custom keywords from optional vocabularies that don't have an x- prefix", | ||||||
| 1> "description": null, | ||||||
| 1> "schemaLocation": [ "openapi" ], | ||||||
| 1> "position": [ 6, 3, 6, 14 ] | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: The expected end column in Prompt for AI agents
Suggested change
|
||||||
| 1> } | ||||||
| 1> ] | ||||||
| 1> } | ||||||
| EOF | ||||||
|
|
||||||
| COMPARE result_1.txt AGAINST expected_1.txt | ||||||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/utils.h:135treats any object with a string-valuedopenapimember as an OpenAPI description even when it lacks required OpenAPI members such asinfo, despite JSON Schema permitting arbitrary/custom keyword names. Consequently, a JSON Schema containing"openapi": "3.0.3"now exits with an unsupported-version error instead of being linted (unlike the analogous non-string case covered by the new test), so this rejects inputs that are not OpenAPI descriptions.Severity: medium
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.