Skip to content

Commit

Permalink
fix(rulesets): support matrix parameter style for openapi3 (#1864)
Browse files Browse the repository at this point in the history
* support matrix parameter style for openapi3

#1863

* fix(ruleset): added tests for matrix parameters

Co-authored-by: Jakub Rożek <jakub@stoplight.io>
  • Loading branch information
lintaba and P0lip committed Oct 18, 2021
1 parent 2f26ed6 commit 44b59d5
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
100 changes: 100 additions & 0 deletions packages/rulesets/src/oas/__tests__/path-params.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,4 +472,104 @@ testRule('path-params', [
},
errors: [],
},

{
name: 'No error with special characters in path parameter',
document: {
openapi: '3.0.3',
paths: {
'/foo/{;bar}': {
parameters: [
{
name: 'bar',
in: 'path',
required: true,
style: 'matrix',
description: 'Shared common parameter.',
},
],
get: {
parameters: [
{
name: 'bar',
in: 'path',
required: true,
style: 'matrix',
description: 'Operation level parameter.',
},
],
},
},
'/bar/{;?baz*}': {
parameters: [
{
name: 'baz',
in: 'path',
required: true,
style: 'matrix',
description: 'Shared common parameter.',
},
],
get: {
parameters: [
{
name: 'baz',
in: 'path',
required: true,
style: 'matrix',
description: 'Operation level parameter.',
},
],
},
},
},
},
errors: [],
},

{
name: 'No error with multiple path parameters together',
document: {
openapi: '3.0.3',
paths: {
'/foo/{bar}{baz}': {
parameters: [
{
name: 'bar',
in: 'path',
required: true,
style: 'matrix',
description: 'Shared common parameter.',
},
{
name: 'baz',
in: 'path',
required: true,
style: 'matrix',
description: 'Shared common parameter.',
},
],
get: {
parameters: [
{
name: 'bar',
in: 'path',
required: true,
style: 'matrix',
description: 'Operation level parameter.',
},
{
name: 'baz',
in: 'path',
required: true,
style: 'matrix',
description: 'Operation level parameter.',
},
],
},
},
},
},
errors: [],
},
]);
4 changes: 2 additions & 2 deletions packages/rulesets/src/oas/functions/oasPathParam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { JsonPath, Segment } from '@stoplight/types';
import type { IFunction, IFunctionResult } from '@stoplight/spectral-core';
import { isObject } from './utils/isObject';

const pathRegex = /(\{[a-zA-Z0-9_-]+\})+/g;
const pathRegex = /(\{;?\??[a-zA-Z0-9_-]+\*?\})/g;

interface IParam {
in?: string;
Expand Down Expand Up @@ -118,7 +118,7 @@ export const oasPathParam: IFunction = targetVal => {
let match;

while ((match = pathRegex.exec(path))) {
const p = match[0].replace(/[{}]/g, '');
const p = match[0].replace(/[{}?*;]/g, '');
if (pathElements.includes(p)) {
results.push(generateResult(`Path "${path}" must not use parameter "{${p}}" multiple times.`, ['paths', path]));
} else {
Expand Down

0 comments on commit 44b59d5

Please sign in to comment.