Skip to content

Commit

Permalink
feat: 132 ignore standard tracing headers
Browse files Browse the repository at this point in the history
  • Loading branch information
dweber019 committed Feb 14, 2024
1 parent 6961989 commit 273b16f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
30 changes: 30 additions & 0 deletions functions/rule-132.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const ignoredHeaders = [
'x-b3-traceid',
'x-b3-spanid',
'x-b3-sampled',
'x-b3-parentspanid',
'b3',
'traceparent',
'tracestate',
];

export default (targetValue) => {
if (ignoredHeaders.includes(targetValue.toLowerCase())) {
return [];
}

const regex = /^([A-Z][a-z]*)(-[A-Z0-9][a-z0-9]*)*$/g;
const found = targetValue.match(regex);

if (!found) {
return [
{
message: `Header parameters should be Hyphenated-Pascal-Case`,
},
];
}

return [];
};
4 changes: 2 additions & 2 deletions tests/233a-MUST-request-must-provide-b3-tracing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('MUST request must provide b3 tracing [233a]', () => {
test('Assert missing X-B3-Traceid', 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: string }) => !param['$ref'] || param['$ref'] !== '#/components/parameters/HeaderB3TraceId',
);

const result = await lint(openApi, 'baloise');
Expand All @@ -21,7 +21,7 @@ describe('MUST request must provide b3 tracing [233a]', () => {
test('Assert missing X-B3-Spanid', 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/HeaderB3Spanid',
(param: { $ref: string }) => !param['$ref'] || param['$ref'] !== '#/components/parameters/HeaderB3SpanId',
);

const result = await lint(openApi, 'baloise');
Expand Down
12 changes: 6 additions & 6 deletions tests/fixtures/base-openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ paths:
type: integer
format: int32
- $ref: '#/components/parameters/HeaderParamComponent'
- $ref: '#/components/parameters/HeaderB3Traceid'
- $ref: '#/components/parameters/HeaderB3Spanid'
- $ref: '#/components/parameters/HeaderB3TraceId'
- $ref: '#/components/parameters/HeaderB3SpanId'
responses:
'200':
description: ok
Expand Down Expand Up @@ -108,15 +108,15 @@ components:
name: Header-Param-From-Component
schema:
type: integer
HeaderB3Traceid:
HeaderB3TraceId:
in: header
name: X-B3-Traceid
name: X-B3-TraceId
required: true
schema:
type: string
HeaderB3Spanid:
HeaderB3SpanId:
in: header
name: X-B3-Spanid
name: X-B3-SpanId
required: false
schema:
type: string
Expand Down
7 changes: 3 additions & 4 deletions zalando.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ functions:
- count-resource-types
- is-object-schema
- is-problem-json-schema
- rule-132

rules:
# https://meta.stoplight.io/docs/spectral/docs/reference/openapi-rules.md#oas3-schema
Expand Down Expand Up @@ -116,11 +117,9 @@ rules:
description: SHOULD prefer hyphenated-pascal-case for HTTP header fields [132]
documentationUrl: https://opensource.zalando.com/restful-api-guidelines/#132
severity: warn
given: $.paths.*.*.parameters[?(@ && @.in=='header')].name
given: $.paths..parameters[?(@ && @.in=='header')].name
then:
function: pattern
functionOptions:
match: ^([A-Z][a-z]*)(-[A-Z0-9][a-z0-9]*)*$
function: rule-132

# SHOULD not use /api as base path [135]
# => https://opensource.zalando.com/restful-api-guidelines/#135
Expand Down

0 comments on commit 273b16f

Please sign in to comment.