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

Commit 8cb6eb7

Browse files
author
Marco Friso
committed
feat(oas3): add format link
1 parent 8c8c429 commit 8cb6eb7

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

packages/openapi3-parser/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
This can dramatically increase the performance when dealing with documents
99
which use many unsupported features.
1010

11+
### Enhancements
12+
13+
- added a Link element to the specific format/version in the parse result.
14+
1115
### Bug Fixes
1216

1317
- Return a warning when parsing a document with a 'Media Type Object'

packages/openapi3-parser/lib/parser.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,14 +126,30 @@ function parse(source, context) {
126126
R.unless(isObjectOrAnnotation, createError(context.namespace, 'Source document is not an object')),
127127
R.unless(isAnnotation, parseOpenAPIObject(context)));
128128

129-
return R.chain(
129+
const parseResult = R.chain(
130130
R.pipe(
131131
parseDocument,
132132
deduplicateUnsupportedAnnotations(context.namespace),
133133
context.options.generateSourceMap ? filterColumnLine : filterSourceMaps
134134
),
135135
document
136136
);
137+
138+
if (!isAnnotation(parseResult.content[0])) {
139+
const formatVersion = (/3\.\d+\.\d+/).exec(source)[0];
140+
const formatLink = `https://spec.openapis.org/oas/v${formatVersion}`;
141+
142+
const { Link } = context.namespace.elements;
143+
const link = new Link();
144+
145+
link.title = `OpenAPI ${formatVersion}`;
146+
link.relation = 'via';
147+
link.href = formatLink;
148+
149+
parseResult.links.push(link);
150+
}
151+
152+
return parseResult;
137153
}
138154

139155
module.exports = parse;

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ const { Fury } = require('@apielements/core');
22
const { expect } = require('./chai');
33

44
const parse = require('../../lib/parser');
5+
const Context = require('../../lib/context');
56

67
const { minim: namespace } = new Fury();
7-
8-
const Context = require('../../lib/context');
8+
const { Link } = namespace.elements;
99

1010
describe('#parse', () => {
1111
let context;
@@ -40,4 +40,15 @@ describe('#parse', () => {
4040
expect(parseResult.length).to.equal(1);
4141
expect(parseResult.api.title.toValue()).to.equal('My API');
4242
});
43+
44+
it('add the format link', () => {
45+
const source = 'openapi: "3.0.0"\ninfo: {title: My API, version: 1.0.0}\npaths: {}\n';
46+
const parseResult = parse(source, context);
47+
48+
const link = parseResult.links.get(0);
49+
expect(link).to.be.instanceof(Link);
50+
expect(link.relation.toValue()).to.equal('via');
51+
expect(link.title.toValue()).to.equal('OpenAPI 3.0.0');
52+
expect(link.href.toValue()).to.equal('https://spec.openapis.org/oas/v3.0.0');
53+
});
4354
});

0 commit comments

Comments
 (0)