Skip to content

Commit

Permalink
feat(oas3): warn on and ignore non-unique operationId
Browse files Browse the repository at this point in the history
  • Loading branch information
tjanc authored and kylef committed Jan 22, 2019
1 parent b46e9ca commit fed0668
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/fury-adapter-oas3-parser/lib/parser/annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ function createInvalidMemberWarning(namespace, path, member) {
return createWarning(namespace, message, member.key);
}

function createIdentifierNotUniqueWarning(namespace, path, member) {
return createWarning(namespace, `'${path}' '${member.key.toValue()}' is not a unique identifier: '${member.value.toValue()}'`, member.value);
}

function createMemberValueNotStringWarning(namespace, path, member) {
return createWarning(namespace, `'${path}' '${member.key.toValue()}' is not a string`, member.value);
}
Expand Down Expand Up @@ -66,6 +70,7 @@ module.exports = {
createWarning,
createUnsupportedMemberWarning: R.curry(createUnsupportedMemberWarning),
createInvalidMemberWarning: R.curry(createInvalidMemberWarning),
createIdentifierNotUniqueWarning: R.curry(createIdentifierNotUniqueWarning),
createMemberValueNotStringWarning: R.curry(createMemberValueNotStringWarning),
createMemberValueNotStringError: R.curry(createMemberValueNotStringError),
createMemberValueNotBooleanWarning: R.curry(createMemberValueNotBooleanWarning),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
createUnsupportedMemberWarning,
createInvalidMemberWarning,
validateObjectContainsRequiredKeys,
createIdentifierNotUniqueWarning,
} = require('../annotations');
const parseCopy = require('../parseCopy');
const pipeParseResult = require('../../pipeParseResult');
Expand Down Expand Up @@ -44,10 +45,20 @@ function createTransactions(namespace, member, operation) {
function parseOperationObject(context, member) {
const { namespace } = context;

const isUnique = element => context.registerId(element.toValue());

const parseOperationId = R.curry(member => new namespace.elements.ParseResult([
R.unless(
R.compose(isUnique, getValue),
createIdentifierNotUniqueWarning(namespace, name),
member
),
]));

const parseMember = R.cond([
[hasKey('summary'), parseString(context, name, false)],
[hasKey('description'), parseCopy(context, name, false)],
[hasKey('operationId'), parseString(context, name, false)],
[hasKey('operationId'), pipeParseResult(namespace, parseString(context, name, false), parseOperationId)],
[hasKey('responses'), R.compose(parseResponsesObject(context), getValue)],

[isUnsupportedKey, createUnsupportedMemberWarning(namespace, name)],
Expand Down
7 changes: 6 additions & 1 deletion packages/fury-adapter-oas3-parser/lib/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ class State {
}

registerId(id) {
return this.registeredIds.add(id);
if (this.registeredIds.has(id)) {
return false;
}

this.registeredIds.add(id);
return true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,5 +259,34 @@ describe('Operation Object', () => {
expect(transition).to.be.instanceof(namespace.elements.Transition);
expect(transition.id.toValue()).to.equal('exampleId');
});

it('warns when operationId is not unique', () => {
const operationA = new namespace.elements.Member('get', {
operationId: 'exampleId',
responses: {},
});

const operationB = new namespace.elements.Member('get', {
operationId: 'exampleId',
responses: {},
});

const resultA = parse(context, operationA);

{
expect(resultA.length).to.equal(1);
const transition = resultA.get(0);
expect(transition).to.be.instanceof(namespace.elements.Transition);
expect(transition.id.toValue()).to.equal('exampleId');
}

const resultB = parse(context, operationB);
{
expect(resultB.length).to.equal(2);
const transition = resultB.get(0);
expect(transition).to.be.instanceof(namespace.elements.Transition);
expect(resultB).to.contain.warning("'Operation Object' 'operationId' is not a unique identifier: 'exampleId'");
}
});
});
});

0 comments on commit fed0668

Please sign in to comment.