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

feat(oas3): add format link #533

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
6 changes: 6 additions & 0 deletions packages/openapi3-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# API Elements: OpenAPI 3 Parser Changelog

## Unreleased

### Enhancements

- added a Link element to the specific format/version in the parse result.

## 0.15.0 (2020-08-06)

### Enhancements
Expand Down
27 changes: 25 additions & 2 deletions packages/openapi3-parser/lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
const R = require('ramda');
const parseYAML = require('./parser/parseYAML');

const { isAnnotation, isWarningAnnotation, isObject } = require('./predicates');
const {
isAnnotation, isWarningAnnotation, isObject, isMember, hasKey,
} = require('./predicates');
const { createError } = require('./elements');
const pipeParseResult = require('./pipeParseResult');

Expand Down Expand Up @@ -126,14 +128,35 @@ function parse(source, context) {
R.unless(isObjectOrAnnotation, createError(context.namespace, 'Source document is not an object')),
R.unless(isAnnotation, parseOpenAPIObject(context)));

return R.chain(
const parseResult = R.chain(
R.pipe(
parseDocument,
deduplicateUnsupportedAnnotations(context.namespace),
context.options.generateSourceMap ? filterColumnLine : filterSourceMaps
),
document
);

if (!isAnnotation(parseResult.content[0])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this check can be removed like in the API Blueprint version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

const formatVersion = R.pipe(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest adding a check here for the value of the openapi member element being a string element, and if any of these checks fail I think we should default to the latest OpenAPI 3 version we know about which is 3.0.3. I think there may be cases where we could get here with an invalid document which cannot be parsed correctly.

Making a test similar to it('add the format link', () => { but instead the source is invalid document should verify this, if you set source to be an unexpected value such as const source = '{}' or const source = '[]' etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed here f52cd03

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check if the value of the OpenAPi member element is a string element 88bcb56

R.prop('content'),
R.find(isObject),
R.prop('content'),
R.find(R.both(isMember, hasKey('openapi')))
)(document).toValue().value;

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;

parseResult.links.push(link);
}

return parseResult;
}

module.exports = parse;
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
{
"element": "parseResult",
"meta": {
"links": {
"element": "array",
"content": [
{
"element": "link",
"meta": {
"title": {
"element": "string",
"content": "OpenAPI 3.0.0"
}
},
"attributes": {
"relation": {
"element": "string",
"content": "via"
},
"href": {
"element": "string",
"content": "https://spec.openapis.org/oas/v3.0.0"
}
}
}
]
}
},
"content": [
{
"element": "category",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
{
"element": "parseResult",
"meta": {
"links": {
"element": "array",
"content": [
{
"element": "link",
"meta": {
"title": {
"element": "string",
"content": "OpenAPI 3.0.0"
}
},
"attributes": {
"relation": {
"element": "string",
"content": "via"
},
"href": {
"element": "string",
"content": "https://spec.openapis.org/oas/v3.0.0"
}
}
}
]
}
},
"content": [
{
"element": "category",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
{
"element": "parseResult",
"meta": {
"links": {
"element": "array",
"content": [
{
"element": "link",
"meta": {
"title": {
"element": "string",
"content": "OpenAPI 3.0.0"
}
},
"attributes": {
"relation": {
"element": "string",
"content": "via"
},
"href": {
"element": "string",
"content": "https://spec.openapis.org/oas/v3.0.0"
}
}
}
]
}
},
"content": [
{
"element": "category",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
{
"element": "parseResult",
"meta": {
"links": {
"element": "array",
"content": [
{
"element": "link",
"meta": {
"title": {
"element": "string",
"content": "OpenAPI 3.0.0"
}
},
"attributes": {
"relation": {
"element": "string",
"content": "via"
},
"href": {
"element": "string",
"content": "https://spec.openapis.org/oas/v3.0.0"
}
}
}
]
}
},
"content": [
{
"element": "category",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
{
"element": "parseResult",
"meta": {
"links": {
"element": "array",
"content": [
{
"element": "link",
"meta": {
"title": {
"element": "string",
"content": "OpenAPI 3.0.0"
}
},
"attributes": {
"relation": {
"element": "string",
"content": "via"
},
"href": {
"element": "string",
"content": "https://spec.openapis.org/oas/v3.0.0"
}
}
}
]
}
},
"content": [
{
"element": "category",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
{
"element": "parseResult",
"meta": {
"links": {
"element": "array",
"content": [
{
"element": "link",
"meta": {
"title": {
"element": "string",
"content": "OpenAPI 3.0.0"
}
},
"attributes": {
"relation": {
"element": "string",
"content": "via"
},
"href": {
"element": "string",
"content": "https://spec.openapis.org/oas/v3.0.0"
}
}
}
]
}
},
"content": [
{
"element": "category",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
{
"element": "parseResult",
"meta": {
"links": {
"element": "array",
"content": [
{
"element": "link",
"meta": {
"title": {
"element": "string",
"content": "OpenAPI 3.0.0"
}
},
"attributes": {
"relation": {
"element": "string",
"content": "via"
},
"href": {
"element": "string",
"content": "https://spec.openapis.org/oas/v3.0.0"
}
}
}
]
}
},
"content": [
{
"element": "category",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
{
"element": "parseResult",
"meta": {
"links": {
"element": "array",
"content": [
{
"element": "link",
"meta": {
"title": {
"element": "string",
"content": "OpenAPI 3.0.0"
}
},
"attributes": {
"relation": {
"element": "string",
"content": "via"
},
"href": {
"element": "string",
"content": "https://spec.openapis.org/oas/v3.0.0"
}
}
}
]
}
},
"content": [
{
"element": "category",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
{
"element": "parseResult",
"meta": {
"links": {
"element": "array",
"content": [
{
"element": "link",
"meta": {
"title": {
"element": "string",
"content": "OpenAPI 3.0.0"
}
},
"attributes": {
"relation": {
"element": "string",
"content": "via"
},
"href": {
"element": "string",
"content": "https://spec.openapis.org/oas/v3.0.0"
}
}
}
]
}
},
"content": [
{
"element": "category",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
{
"element": "parseResult",
"meta": {
"links": {
"element": "array",
"content": [
{
"element": "link",
"meta": {
"title": {
"element": "string",
"content": "OpenAPI 3.0.0"
}
},
"attributes": {
"relation": {
"element": "string",
"content": "via"
},
"href": {
"element": "string",
"content": "https://spec.openapis.org/oas/v3.0.0"
}
}
}
]
}
},
"content": [
{
"element": "category",
Expand Down
Loading