Skip to content
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

[dynamicjson dynamicyaml dynamictoml] switch to jsonpath-plus #10551

Merged
merged 1 commit into from
Sep 25, 2024
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
183 changes: 51 additions & 132 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"joi": "17.13.3",
"joi-extension-semver": "5.0.0",
"js-yaml": "^4.1.0",
"jsonpath": "~1.1.1",
"jsonpath-plus": "^9.0.0",
"lodash.countby": "^4.6.0",
"lodash.groupby": "^4.6.0",
"lodash.times": "^4.3.2",
Expand Down
31 changes: 23 additions & 8 deletions services/dynamic/dynamic-json.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ t.create('query with lexical error')
)
.expectBadge({
label: 'custom badge',
message: 'unparseable jsonpath query',
color: 'red',
message: 'no result',
color: 'lightgrey',
})

t.create('query with parse error')
Expand All @@ -155,8 +155,8 @@ t.create('query with parse error')
)
.expectBadge({
label: 'custom badge',
message: 'unparseable jsonpath query',
color: 'red',
message: 'no result',
color: 'lightgrey',
})

// Example from https://stackoverflow.com/q/11670384/893113
Expand All @@ -170,7 +170,23 @@ t.create('query with invalid token')
)
.expectBadge({
label: 'custom badge',
message: 'unparseable jsonpath query',
message: 'query not supported',
color: 'red',
})

/*
Based on https://github.com/JSONPath-Plus/JSONPath/blob/v9.0.0/test/test.errors.js#L53-L68
This functionality is disabled for security reasons.
*/
t.create('query with eval filtering expression')
.get(
`.json?url=https://github.com/badges/shields/raw/master/package.json&query=${encodeURIComponent(
'$..keywords[(@.length-1)]',
)}`,
)
.expectBadge({
label: 'custom badge',
message: 'query not supported',
color: 'red',
})

Expand All @@ -185,12 +201,11 @@ t.create('JSON contains an array')
})

t.create('JSON contains a string')
.get('.json?url=https://example.test/json&query=$.foo,')
.get('.json?url=https://example.test/json&query=$')
.intercept(nock =>
nock('https://example.test').get('/json').reply(200, '"foo"'),
)
.expectBadge({
label: 'custom badge',
message: 'resource must contain an object or array',
color: 'lightgrey',
message: 'foo',
})
5 changes: 2 additions & 3 deletions services/dynamic/dynamic-yaml.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,11 @@ t.create('YAML from url | error color overrides user specified')
})

t.create('YAML contains a string')
.get('.json?url=https://example.test/yaml&query=$.foo,')
.get('.json?url=https://example.test/yaml&query=$,')
.intercept(nock =>
nock('https://example.test').get('/yaml').reply(200, '"foo"'),
)
.expectBadge({
label: 'custom badge',
message: 'resource must contain an object or array',
color: 'lightgrey',
message: 'foo',
})
20 changes: 4 additions & 16 deletions services/dynamic/json-path.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/

import Joi from 'joi'
import jp from 'jsonpath'
import { JSONPath as jp } from 'jsonpath-plus'
import { renderDynamicBadge, httpErrors } from '../dynamic-common.js'
import { InvalidParameter, InvalidResponse } from '../index.js'

Expand Down Expand Up @@ -43,26 +43,14 @@ export default superclass =>
httpErrors,
})

// JSONPath only works on objects and arrays.
// https://github.com/badges/shields/issues/4018
if (typeof data !== 'object') {
throw new InvalidResponse({
prettyMessage: 'resource must contain an object or array',
})
}

let values
try {
values = jp.query(data, pathExpression)
values = jp({ json: data, path: pathExpression, eval: false })
} catch (e) {
const { message } = e
if (
message.startsWith('Lexical error') ||
message.startsWith('Parse error') ||
message.includes('Unexpected token')
) {
if (message.includes('prevented in JSONPath expression')) {
throw new InvalidParameter({
prettyMessage: 'unparseable jsonpath query',
prettyMessage: 'query not supported',
})
} else {
throw e
Expand Down
Loading