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

Commit f52cd03

Browse files
author
Marco Friso
committed
fix(oas3): modify formatVersion function
1 parent 729dacb commit f52cd03

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

packages/openapi3-parser/lib/parser.js

+15-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const R = require('ramda');
44
const parseYAML = require('./parser/parseYAML');
55

66
const {
7-
isAnnotation, isWarningAnnotation, isObject, isMember, hasKey,
7+
isAnnotation, isWarningAnnotation, isObject, isMember, hasKey, isString,
88
} = require('./predicates');
99
const { createError } = require('./elements');
1010
const pipeParseResult = require('./pipeParseResult');
@@ -137,24 +137,26 @@ function parse(source, context) {
137137
document
138138
);
139139

140-
if (!isAnnotation(parseResult.content[0])) {
141-
const formatVersion = R.pipe(
140+
const formatVersion = R.tryCatch(
141+
R.pipe(
142142
R.prop('content'),
143143
R.find(isObject),
144144
R.prop('content'),
145-
R.find(R.both(isMember, hasKey('openapi')))
146-
)(document).toValue().value;
145+
R.find(R.both(isMember, hasKey('openapi'))),
146+
R.path(['content', 'value']),
147+
R.and(isString, R.prop('content'))
148+
), R.always('3.0.3')
149+
)(document);
147150

148-
const formatLink = `https://spec.openapis.org/oas/v${formatVersion}`;
149-
const { Link } = context.namespace.elements;
150-
const link = new Link();
151+
const formatLink = `https://spec.openapis.org/oas/v${formatVersion}`;
152+
const { Link } = context.namespace.elements;
153+
const link = new Link();
151154

152-
link.title = `OpenAPI ${formatVersion}`;
153-
link.relation = 'via';
154-
link.href = formatLink;
155+
link.title = `OpenAPI ${formatVersion}`;
156+
link.relation = 'via';
157+
link.href = formatLink;
155158

156-
parseResult.links.push(link);
157-
}
159+
parseResult.links.push(link);
158160

159161
return parseResult;
160162
}

packages/openapi3-parser/test/unit/parser-test.js

+11
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,15 @@ describe('#parse', () => {
5151
expect(link.title.toValue()).to.equal('OpenAPI 3.0.0');
5252
expect(link.href.toValue()).to.equal('https://spec.openapis.org/oas/v3.0.0');
5353
});
54+
55+
it('add OpenAPI 3.0.3 format link when fails to parse an OAS3 document', () => {
56+
const source = '{}{}';
57+
const parseResult = parse(source, context);
58+
59+
const link = parseResult.links.get(0);
60+
expect(link).to.be.instanceof(Link);
61+
expect(link.relation.toValue()).to.equal('via');
62+
expect(link.title.toValue()).to.equal('OpenAPI 3.0.3');
63+
expect(link.href.toValue()).to.equal('https://spec.openapis.org/oas/v3.0.3');
64+
});
5465
});

0 commit comments

Comments
 (0)