Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Prevent treating overlapping paths as a circular reference #641

Merged
merged 2 commits into from
Jul 15, 2021
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
9 changes: 9 additions & 0 deletions packages/openapi2-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# API Elements: OpenAPI 2 Parser Changelog

## 0.32.6 (2021-07-21)

### Bug Fixes

- Fixes generating example JSON bodies from Swagger Schema when the schema
contains a reference to a JSON path which prefixes the current path, for
example when handling a path such as `$ref: '#/definitions/User'` from a path
which prefixes it such as `$ref: '#/definitions/UserList'`.

## 0.32.5 (2020-02-24)

### Enhancements
Expand Down
12 changes: 9 additions & 3 deletions packages/openapi2-parser/lib/json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ const pathHasCircularReference = (paths, path, reference) => {
const currentPath = (path || []).join('/');

// Check for direct circular reference
if (currentPath.startsWith(reference)) {
if (currentPath === reference || currentPath.startsWith(`${reference}/`)) {
return true;
}

// Check for indirect circular Reference
if ((paths || []).find(p => p.startsWith(reference))) {
if ((paths || []).find(p => p === reference || p.startsWith(`${reference}/`))) {
return true;
}

Expand Down Expand Up @@ -393,5 +393,11 @@ const convertSchemaDefinitions = (definitions) => {
};

module.exports = {
isExtension, parseReference, lookupReference, dereference, convertSchema, convertSchemaDefinitions,
isExtension,
parseReference,
lookupReference,
dereference,
convertSchema,
convertSchemaDefinitions,
pathHasCircularReference,
};
2 changes: 1 addition & 1 deletion packages/openapi2-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apielements/openapi2-parser",
"version": "0.32.5",
"version": "0.32.6",
"description": "Swagger 2.0 parser for Fury.js",
"author": "Apiary.io <support@apiary.io>",
"license": "MIT",
Expand Down
51 changes: 50 additions & 1 deletion packages/openapi2-parser/test/json-schema-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { expect } = require('chai');
const { convertSchema, convertSchemaDefinitions, dereference } = require('../lib/json-schema');
const {
convertSchema, convertSchemaDefinitions, dereference, pathHasCircularReference,
} = require('../lib/json-schema');

describe('Swagger Schema to JSON Schema', () => {
it('returns compatible schema when given valid JSON Schema', () => {
Expand Down Expand Up @@ -853,3 +855,50 @@ describe('Dereferencing', () => {
});
});
});


describe('#pathHasCircularReference', () => {
it('does not detect circular reference when reference is not circular', () => {
const reference = '#/definitions/User';
const currentPath = ['#', 'definitions', 'ListUsers'];

expect(pathHasCircularReference([], currentPath, reference)).to.be.false;
});

it('detects circular reference when current path is reference path', () => {
const reference = '#/definitions/User';
const currentPath = ['#', 'definitions', 'User'];

expect(pathHasCircularReference([], currentPath, reference)).to.be.true;
});

it('detects circular reference when current path contains reference path', () => {
const reference = '#/definitions/User';
const currentPath = ['#', 'definitions', 'User', 'properties', 'parent'];

expect(pathHasCircularReference([], currentPath, reference)).to.be.true;
});

it('detects incircular reference when current path is in the prior referenced tree', () => {
const reference = '#/definitions/User';
const currentPath = ['#', 'definitions', 'ListUsers'];
const currentTree = ['#/definitions/User/properties/children'];

expect(pathHasCircularReference(currentTree, currentPath, reference)).to.be.true;
});

it('does not detect circular reference when reference last component prefixes current path', () => {
const reference = '#/definitions/User';
const currentPath = ['#', 'definitions', 'UserList'];

expect(pathHasCircularReference([], currentPath, reference)).to.be.false;
});

it('does not detect incircular reference when reference last component prefixes path in prior reference tree', () => {
const reference = '#/definitions/User';
const currentPath = ['#', 'definitions', 'UserDetail'];
const currentTree = ['#/definitions/UserList/items'];

expect(pathHasCircularReference(currentTree, currentPath, reference)).to.be.false;
});
});
2 changes: 1 addition & 1 deletion packages/openapi3-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# API Elements: OpenAPI 3 Parser Changelog

## TBD
## 0.16.1 (2021-07-15)

### Enhancements

Expand Down
2 changes: 1 addition & 1 deletion packages/openapi3-parser/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@apielements/openapi3-parser",
"version": "0.16.0",
"version": "0.16.1",
"description": "Open API Specification 3 API Elements Parser",
"author": "Apiary.io <support@apiary.io>",
"license": "MIT",
Expand Down