Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Submission API
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FIT-Connect
Submission API
Commits
7cffa147
Commit
7cffa147
authored
3 years ago
by
David Schwarzmann
Browse files
Options
Downloads
Patches
Plain Diff
build(api#87): Add custom spectral rule for checking for pagination
parent
3db47f59
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.spectral.yml
+13
-0
13 additions, 0 deletions
.spectral.yml
functions/pagination-response.js
+75
-0
75 additions, 0 deletions
functions/pagination-response.js
with
88 additions
and
0 deletions
.spectral.yml
+
13
−
0
View file @
7cffa147
...
@@ -3,6 +3,11 @@ extends:
...
@@ -3,6 +3,11 @@ extends:
# - https://italia.github.io/api-oas-checker/spectral.yml
# - https://italia.github.io/api-oas-checker/spectral.yml
-
https://italia.github.io/api-oas-checker/spectral-generic.yml
-
https://italia.github.io/api-oas-checker/spectral-generic.yml
-
https://italia.github.io/api-oas-checker/spectral-security.yml
-
https://italia.github.io/api-oas-checker/spectral-security.yml
functionsDir
:
'
./functions'
functions
:
-
pagination-response
rules
:
rules
:
no-default-additionalProperties
:
hint
no-default-additionalProperties
:
hint
string-maxlength
:
hint
string-maxlength
:
hint
...
@@ -17,3 +22,11 @@ rules:
...
@@ -17,3 +22,11 @@ rules:
use-semver
:
off
use-semver
:
off
patch-media-type
:
off
patch-media-type
:
off
check-for-pagination
:
description
:
An operation that returns a list and less than 4 properties could perhaps support pagination.
message
:
'
{{error}}'
severity
:
warn
given
:
-
$.paths.*[get]
then
:
function
:
pagination-response
This diff is collapsed.
Click to expand it.
functions/pagination-response.js
0 → 100644
+
75
−
0
View file @
7cffa147
const
RESPONSE_ATTRIBUTES
=
[
'
offset
'
,
'
totalCount
'
,
'
count
'
]
const
QUERY_PARAMS
=
[
'
limit
'
,
'
offset
'
]
const
MINIMUM_ATTR_COUNT
=
4
module
.
exports
=
(
operation
,
_opts
,
paths
)
=>
{
// operation should be a get or post operation
if
(
operation
===
null
||
typeof
operation
!==
'
object
'
)
{
return
[]
}
const
path
=
paths
.
given
||
[]
// responses is required property of an operation in OpenAPI 2.0, so if
// isn't present this will be flagged elsewhere -- just return
if
(
!
operation
.
responses
||
typeof
operation
.
responses
!==
'
object
'
)
{
return
[]
}
// Find success response code
const
resp
=
Object
.
keys
(
operation
.
responses
)
.
find
((
code
)
=>
code
.
startsWith
(
'
2
'
))
// No success response will be flagged elsewhere, just return
if
(
!
resp
)
{
return
[]
}
// available content types
const
content
=
operation
.
responses
[
resp
].
content
// Get the schema of the success response
const
responseSchema
=
content
[
Object
.
keys
(
content
)[
0
]].
schema
||
{}
const
errors
=
[]
const
responseHasArray
=
Object
.
values
(
responseSchema
.
properties
||
{})
.
some
((
prop
)
=>
prop
.
type
===
'
array
'
)
const
operationId
=
operation
.
operationId
?
`'
${
operation
.
operationId
}
'`
:
''
if
(
responseHasArray
&&
Object
.
keys
(
responseSchema
.
properties
).
length
<=
MINIMUM_ATTR_COUNT
)
{
RESPONSE_ATTRIBUTES
.
forEach
((
entry
)
=>
{
if
(
!
Object
.
keys
(
responseSchema
.
properties
).
includes
(
entry
))
{
errors
.
push
({
message
:
`Operation
${
operationId
}
might be pageable. Property '
${
entry
}
' is missing.`
,
path
,
})
}
})
if
(
operation
.
parameters
)
{
const
queryParams
=
operation
.
parameters
.
filter
((
param
)
=>
param
.
in
===
'
query
'
)
if
(
queryParams
)
{
const
names
=
queryParams
.
map
((
param
)
=>
param
.
name
)
QUERY_PARAMS
.
forEach
((
e
)
=>
{
if
(
!
names
.
includes
(
e
))
{
errors
.
push
({
message
:
`Operation
${
operationId
}
might be pageable. Query parameter '
${
e
}
' is missing.`
,
path
,
})
}
})
}
}
else
{
errors
.
push
({
message
:
`Operation
${
operationId
}
might be pageable. Query parameters are missing.`
,
path
,
})
}
}
return
errors
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment