Skip to content

Commit

Permalink
feat(oas3): only emit a single warning per warning type
Browse files Browse the repository at this point in the history
  • Loading branch information
kylef committed Jul 30, 2020
1 parent aed02c9 commit 75d2b97
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 495 deletions.
43 changes: 38 additions & 5 deletions packages/openapi3-parser/lib/parser/oas/parseOpenAPIObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const R = require('ramda');

const {
isAnnotation, isExtension, hasKey, getValue,
isAnnotation, isExtension, hasKey, getValue, isWarningAnnotation,
} = require('../../predicates');
const {
createUnsupportedMemberWarning,
Expand Down Expand Up @@ -99,6 +99,36 @@ function removeColumnLine(result) {
const filterColumnLine = recurseSkippingAnnotations(removeColumnLine);
const filterSourceMaps = recurseSkippingAnnotations(removeSourceMap);

const deduplicateAnnotations = R.curry((namespace, parseResult) => {
const warnings = {};
const counts = {};

const result = new namespace.elements.ParseResult(parseResult.filter((element) => {
if (isWarningAnnotation(element)) {
const originalAnnotation = warnings[element.content];
if (originalAnnotation !== undefined) {
counts[originalAnnotation] += 1;
return false;
}

warnings[element.content] = element;
counts[element] = 1;
}

return true;
}));

Object.values(warnings).forEach((warning) => {
const count = counts[warning];
if (count > 1) {
// eslint-disable-next-line no-param-reassign
warning.content = `${warning.content} (${count} occurances)`;
}
});

return result;
});

function parseOASObject(context, object) {
const { namespace } = context;

Expand Down Expand Up @@ -183,10 +213,13 @@ function parseOASObject(context, object) {
return api;
});

if (context.options.generateSourceMap) {
return filterColumnLine(parseOASObject(object));
}
return filterSourceMaps(parseOASObject(object));
const parse = R.pipe(
parseOASObject,
deduplicateAnnotations(namespace),
context.options.generateSourceMap ? filterColumnLine : filterSourceMaps
);

return parse(object);
}

module.exports = R.curry(parseOASObject);
19 changes: 19 additions & 0 deletions packages/openapi3-parser/lib/predicates.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
const R = require('ramda');

const hasClass = R.curry((cls, element) => {
// eslint-disable-next-line no-underscore-dangle
if (element._meta === undefined) {
// accessing meta will create and attach empty ObjectElement
// which we can avoid by checking `_meta`.
// We do not want to mutate the provided element.
return false;
}

const classes = element.meta.get('classes');
if (classes === undefined) {
return false;
}

return classes.includes(cls);
});

/**
* @module predicates
* @private
Expand All @@ -14,6 +31,7 @@ const isString = element => element.element === 'string';
const isBoolean = element => element.element === 'boolean';
const isNull = element => element.element === 'null';
const isDataStructure = element => element.element === 'dataStructure';
const isWarningAnnotation = R.both(isAnnotation, hasClass('warning'));

// Member

Expand Down Expand Up @@ -69,6 +87,7 @@ module.exports = {
isBoolean,
isNull,
isDataStructure,
isWarningAnnotation,

hasKey: R.curry(hasKey),
hasValue: R.curry(hasValue),
Expand Down
250 changes: 5 additions & 245 deletions packages/openapi3-parser/test/integration/fixtures/petstore.json
Original file line number Diff line number Diff line change
Expand Up @@ -934,7 +934,7 @@
]
}
},
"content": "'Operation Object' contains unsupported key 'tags'"
"content": "'Operation Object' contains unsupported key 'tags' (2 occurances)"
},
{
"element": "annotation",
Expand Down Expand Up @@ -994,7 +994,7 @@
]
}
},
"content": "'Parameter Object' contains unsupported key 'schema'"
"content": "'Parameter Object' contains unsupported key 'schema' (2 occurances)"
},
{
"element": "annotation",
Expand Down Expand Up @@ -1054,7 +1054,7 @@
]
}
},
"content": "'Header Object' contains unsupported key 'description'"
"content": "'Header Object' contains unsupported key 'description' (2 occurances)"
},
{
"element": "annotation",
Expand Down Expand Up @@ -1114,187 +1114,7 @@
]
}
},
"content": "'Header Object' contains unsupported key 'schema'"
},
{
"element": "annotation",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "warning"
}
]
}
},
"attributes": {
"sourceMap": {
"element": "array",
"content": [
{
"element": "sourceMap",
"content": [
{
"element": "array",
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 45
},
"column": {
"element": "number",
"content": 7
}
},
"content": 1083
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 45
},
"column": {
"element": "number",
"content": 11
}
},
"content": 4
}
]
}
]
}
]
}
},
"content": "'Operation Object' contains unsupported key 'tags'"
},
{
"element": "annotation",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "warning"
}
]
}
},
"attributes": {
"sourceMap": {
"element": "array",
"content": [
{
"element": "sourceMap",
"content": [
{
"element": "array",
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 62
},
"column": {
"element": "number",
"content": 9
}
},
"content": 1504
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 62
},
"column": {
"element": "number",
"content": 15
}
},
"content": 6
}
]
}
]
}
]
}
},
"content": "'Parameter Object' contains unsupported key 'schema'"
},
{
"element": "annotation",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "warning"
}
]
}
},
"attributes": {
"sourceMap": {
"element": "array",
"content": [
{
"element": "sourceMap",
"content": [
{
"element": "array",
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 67
},
"column": {
"element": "number",
"content": 7
}
},
"content": 1620
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 67
},
"column": {
"element": "number",
"content": 11
}
},
"content": 4
}
]
}
]
}
]
}
},
"content": "'Operation Object' contains unsupported key 'tags'"
"content": "'Header Object' contains unsupported key 'schema' (2 occurances)"
},
{
"element": "annotation",
Expand Down Expand Up @@ -1354,67 +1174,7 @@
]
}
},
"content": "'Schema Object' contains unsupported key 'format'"
},
{
"element": "annotation",
"meta": {
"classes": {
"element": "array",
"content": [
{
"element": "string",
"content": "warning"
}
]
}
},
"attributes": {
"sourceMap": {
"element": "array",
"content": [
{
"element": "sourceMap",
"content": [
{
"element": "array",
"content": [
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 109
},
"column": {
"element": "number",
"content": 11
}
},
"content": 2506
},
{
"element": "number",
"attributes": {
"line": {
"element": "number",
"content": 109
},
"column": {
"element": "number",
"content": 17
}
},
"content": 6
}
]
}
]
}
]
}
},
"content": "'Schema Object' contains unsupported key 'format'"
"content": "'Schema Object' contains unsupported key 'format' (2 occurances)"
}
]
}

0 comments on commit 75d2b97

Please sign in to comment.