Skip to content

Commit

Permalink
fix(oas3): modify formatVersion function
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Friso committed Aug 18, 2020
1 parent 729dacb commit f52cd03
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
28 changes: 15 additions & 13 deletions packages/openapi3-parser/lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const R = require('ramda');
const parseYAML = require('./parser/parseYAML');

const {
isAnnotation, isWarningAnnotation, isObject, isMember, hasKey,
isAnnotation, isWarningAnnotation, isObject, isMember, hasKey, isString,
} = require('./predicates');
const { createError } = require('./elements');
const pipeParseResult = require('./pipeParseResult');
Expand Down Expand Up @@ -137,24 +137,26 @@ function parse(source, context) {
document
);

if (!isAnnotation(parseResult.content[0])) {
const formatVersion = R.pipe(
const formatVersion = R.tryCatch(
R.pipe(
R.prop('content'),
R.find(isObject),
R.prop('content'),
R.find(R.both(isMember, hasKey('openapi')))
)(document).toValue().value;
R.find(R.both(isMember, hasKey('openapi'))),
R.path(['content', 'value']),
R.and(isString, R.prop('content'))
), R.always('3.0.3')
)(document);

const formatLink = `https://spec.openapis.org/oas/v${formatVersion}`;
const { Link } = context.namespace.elements;
const link = new Link();
const formatLink = `https://spec.openapis.org/oas/v${formatVersion}`;
const { Link } = context.namespace.elements;
const link = new Link();

link.title = `OpenAPI ${formatVersion}`;
link.relation = 'via';
link.href = formatLink;
link.title = `OpenAPI ${formatVersion}`;
link.relation = 'via';
link.href = formatLink;

parseResult.links.push(link);
}
parseResult.links.push(link);

return parseResult;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/openapi3-parser/test/unit/parser-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,15 @@ describe('#parse', () => {
expect(link.title.toValue()).to.equal('OpenAPI 3.0.0');
expect(link.href.toValue()).to.equal('https://spec.openapis.org/oas/v3.0.0');
});

it('add OpenAPI 3.0.3 format link when fails to parse an OAS3 document', () => {
const source = '{}{}';
const parseResult = parse(source, context);

const link = parseResult.links.get(0);
expect(link).to.be.instanceof(Link);
expect(link.relation.toValue()).to.equal('via');
expect(link.title.toValue()).to.equal('OpenAPI 3.0.3');
expect(link.href.toValue()).to.equal('https://spec.openapis.org/oas/v3.0.3');
});
});

0 comments on commit f52cd03

Please sign in to comment.