Skip to content

Commit

Permalink
fix: allow path parameters for b3 headers
Browse files Browse the repository at this point in the history
  • Loading branch information
dweber019 committed Feb 14, 2024
1 parent 273b16f commit 5226f55
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion baloise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ rules:
description: MUST use b3 tracing [233a]
documentationUrl: https://github.com/baloise-incubator/spectral-ruleset/blob/main/doc/rules/requests-must-use-b3-tracing.md
severity: error
given: $.paths.*.*.parameters
given: $.paths.*
then:
function: validate-b3-tracing

Expand Down
19 changes: 17 additions & 2 deletions functions/validate-b3-tracing.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

export default (targetValue) => {
function isB3Valid(targetValue) {
if (!Array.isArray(targetValue)) {
return [
{
Expand All @@ -14,7 +14,22 @@ export default (targetValue) => {
param.name && (param.name.toLowerCase() === 'x-b3-traceid' || param.name.toLowerCase() === 'x-b3-spanid'),
);

if (b3Params.length !== 2 || !b3Params.every((param) => param.in === 'header')) {
return !(b3Params.length !== 2 || !b3Params.every((param) => param.in === 'header'));
}

export default (targetValue) => {
if (targetValue.parameters && isB3Valid(targetValue.parameters)) {
return [];
}

const hasInvalidEntry = Object.keys(targetValue)
.filter((verb) =>
['get', 'put', 'post', 'delete', 'options', 'head', 'patch', 'trace'].includes(verb.toLowerCase()),
)
.map((verb) => targetValue[verb])
.some((operation) => operation.parameters && !isB3Valid(operation.parameters));

if (hasInvalidEntry) {
return [
{
message: `B3 header X-B3-Traceid or X-B3-Spanid missing`,
Expand Down
20 changes: 20 additions & 0 deletions tests/233a-MUST-request-must-provide-b3-tracing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,24 @@ describe('MUST request must provide b3 tracing [233a]', () => {
const result = await lint(openApi, 'baloise');
expect(result).toEqual([]);
});

test('Assert b3 tracing headers valid when using path parameters', async () => {
const openApi = await loadOpenApiSpec('base-openapi.yml');
openApi.paths['/example'].get.parameters = openApi.paths['/example'].get.parameters.filter(
(param: { $ref: string }) =>
!param['$ref'] &&
param['$ref'] !== '#/components/parameters/HeaderB3Traceid' &&
param['$ref'] !== '#/components/parameters/HeaderB3Spanid',
);
openApi.paths['/example'].parameters = [];
openApi.paths['/example'].parameters.push({
$ref: '#/components/parameters/HeaderB3Traceid',
});
openApi.paths['/example'].parameters.push({
$ref: '#/components/parameters/HeaderB3Spanid',
});

const result = await lint(openApi, 'baloise');
expect(result).toEqual([]);
});
});

0 comments on commit 5226f55

Please sign in to comment.