forked from 1-Platform/api-catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(1-Platform#13): added support for openapiv2
- Loading branch information
Showing
13 changed files
with
1,428 additions
and
1,200 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,53 @@ | ||
package cli | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/getkin/kin-openapi/openapi2" | ||
"github.com/getkin/kin-openapi/openapi2conv" | ||
"github.com/getkin/kin-openapi/openapi3" | ||
"github.com/invopop/yaml" | ||
) | ||
|
||
func OpenAPIValidator(raw []byte, apiSchemaFile map[string]any) error { | ||
if val, ok := apiSchemaFile["swagger"]; ok && (strings.HasPrefix(val.(string), "2.") || val.(string) == "2") { | ||
// convert to OpenAPIv3 | ||
var docv2 openapi2.T | ||
if err := yaml.Unmarshal(raw, &docv2); err != nil { | ||
return err | ||
} | ||
docv3, err := openapi2conv.ToV3(&docv2) | ||
if err != nil { | ||
return nil | ||
} | ||
|
||
// loader := openapi3.NewLoader() | ||
// if err = docv3.Validate(loader.Context); err != nil { | ||
// return err | ||
// } | ||
|
||
// now we need the openapi v3 version of map[strings] | ||
if raw, err = docv3.MarshalJSON(); err != nil { | ||
return err | ||
} | ||
|
||
if err = yaml.Unmarshal(raw, &apiSchemaFile); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
loader := openapi3.NewLoader() | ||
docv3, err := loader.LoadFromData(raw) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := docv3.Validate(loader.Context); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
|
||
} |
This file contains 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
File renamed without changes.
This file contains 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
This file contains 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
This file contains 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,66 @@ | ||
// Kudos: https://github.com/aceakash/string-similarity | ||
function compareTwoStrings(first, second) { | ||
first = first.replace(/\s+/g, ""); | ||
second = second.replace(/\s+/g, ""); | ||
|
||
if (first === second) return 1; // identical or empty | ||
if (first.length < 2 || second.length < 2) return 0; // if either is a 0-letter or 1-letter string | ||
|
||
let firstBigrams = new Map(); | ||
for (let i = 0; i < first.length - 1; i++) { | ||
const bigram = first.substring(i, i + 2); | ||
const count = firstBigrams.has(bigram) ? firstBigrams.get(bigram) + 1 : 1; | ||
|
||
firstBigrams.set(bigram, count); | ||
} | ||
|
||
let intersectionSize = 0; | ||
for (let i = 0; i < second.length - 1; i++) { | ||
const bigram = second.substring(i, i + 2); | ||
const count = firstBigrams.has(bigram) ? firstBigrams.get(bigram) : 0; | ||
|
||
if (count > 0) { | ||
firstBigrams.set(bigram, count - 1); | ||
intersectionSize++; | ||
} | ||
} | ||
|
||
return (2.0 * intersectionSize) / (first.length + second.length - 2); | ||
} | ||
|
||
export default function (config, options) { | ||
let numberOfResponses = 0; | ||
let numbnerOfFalseResponses = 0; | ||
const weight = options?.weight || 0.8; | ||
|
||
const paths = Object.keys(config.schema.paths); | ||
for (let i = 0; i < paths.length; i++) { | ||
for (let j = paths.length - 1; j >= i; j--) { | ||
numberOfResponses++; | ||
if (j !== i) { | ||
const similiarity = compareTwoStrings(paths[i], paths[j]); | ||
if (similiarity > weight) { | ||
numbnerOfFalseResponses++; | ||
|
||
// get all methods | ||
const methods = Object.keys(config.schema.paths[paths[i]]) | ||
.join(", ") | ||
.toUpperCase(); | ||
|
||
config.report({ | ||
message: `URL ${paths[i]} similiar to ${paths[j]}, similiarity: ${similiarity}`, | ||
path: paths[i], | ||
method: methods, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
const score = | ||
(Math.max(numberOfResponses - numbnerOfFalseResponses, 0) / | ||
numberOfResponses) * | ||
100; | ||
|
||
config.setScore("quality", score); | ||
} |
This file contains 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 |
---|---|---|
@@ -1,13 +1,19 @@ | ||
title = "hello world" | ||
|
||
[rules.url_length] | ||
disable = true | ||
|
||
[rules.req_body_case_checker.options] | ||
casing = "snakecase" | ||
# [rules.url_length] | ||
# disable = true | ||
# | ||
[rules.url_similiarity_check.options] | ||
weight = 0.90 | ||
|
||
[plugins.rules.test_plugin] | ||
file = "./test/test_plugin.js" | ||
|
||
[plugins.rules.test_plugin.options] | ||
test_data = "hello" | ||
|
||
[rules.url_length.options] | ||
weight = 8 | ||
|
||
# [rules.status_code_check.options] | ||
# allowed_status_codes = [200, 201] |
Oops, something went wrong.