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

Commit b51b62a

Browse files
committed
fix(oas3): add unsupported warnings for OpenAPI 3.1 schema
1 parent eaaa73c commit b51b62a

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

packages/openapi3-parser/lib/parser/oas/parseSchemaObject.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,38 @@ const parseReference = require('../parseReference');
1717

1818
const name = 'Schema Object';
1919
const unsupportedKeys = [
20-
// JSON Schema
21-
'multipleOf', 'maximum', 'exclusiveMaximum', 'minimum',
22-
'exclusiveMinimum', 'maxLength', 'minLength', 'pattern', 'maxItems',
23-
'minItems', 'uniqueItems', 'maxProperties', 'minProperties',
20+
// JSON Schema Core applicators
21+
'allOf', 'anyOf', 'not',
2422

25-
// JSON Schema + OAS 3 specific rules
26-
'allOf', 'anyOf', 'not', 'additionalProperties', 'format',
23+
// JSON Schema Validation (6.2 Numeric)
24+
'multipleOf', 'maximum', 'exclusiveMaximum', 'minimum', 'exclusiveMinimum',
25+
26+
// JSON Schema Validation (6.3 Strings)
27+
'maxLength', 'minLength', 'pattern', 'format',
28+
29+
// JSON Schema Validation (6.4 Array)
30+
'maxItems', 'minItems', 'uniqueItems',
31+
32+
// JSON Schema Validation (6.5 Objects)
33+
'maxProperties', 'minProperties',
2734

2835
// OAS 3 specific
2936
'discriminator', 'readOnly', 'writeOnly', 'xml', 'externalDocs', 'deprecated',
3037
];
38+
const unsupportedJSONSchemaDraft202012 = [
39+
// General applicators
40+
'if', 'then', 'else', 'dependentSchemas',
41+
42+
// Array
43+
'prefixItems', 'unevaluatedItems', 'contains', 'minContains', 'maxContains',
44+
45+
// Object
46+
'propertiesNames', 'unevaluatedProperties', 'dependentRequired',
47+
];
3148
const isUnsupportedKey = R.anyPass(R.map(hasKey, unsupportedKeys));
49+
const isUnsupportedKeyJSONSchemaDraft202012 = R.anyPass(
50+
R.map(hasKey, unsupportedJSONSchemaDraft202012)
51+
);
3252

3353

3454
function constructObjectStructure(namespace, schema) {
@@ -252,6 +272,13 @@ function parseSchema(context) {
252272
[hasKey('oneOf'), R.compose(parseOneOf, getValue)],
253273

254274
[isUnsupportedKey, createUnsupportedMemberWarning(namespace, name)],
275+
[
276+
R.both(
277+
isUnsupportedKeyJSONSchemaDraft202012,
278+
R.always(context.isOpenAPIVersionMoreThanOrEqual(3, 1))
279+
),
280+
createUnsupportedMemberWarning(namespace, name),
281+
],
255282

256283
// Return a warning for additional properties
257284
[R.T, createInvalidMemberWarning(namespace, name)],

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,4 +1017,43 @@ describe('Schema Object', () => {
10171017
expect(element.enumerations.get(1)).to.be.instanceof(namespace.elements.Array);
10181018
});
10191019
});
1020+
1021+
describe('warnings for unsupported properties', () => {
1022+
it('provides warning for unsupported property', () => {
1023+
const schema = new namespace.elements.Object({
1024+
deprecated: true,
1025+
});
1026+
1027+
const parseResult = parse(context, schema);
1028+
1029+
expect(parseResult).to.contain.warning(
1030+
"'Schema Object' contains unsupported key 'deprecated'"
1031+
);
1032+
});
1033+
1034+
it('provides invalid warning for unsupported OpenAPI 3.1 property on OpenAPI 3.0', () => {
1035+
const schema = new namespace.elements.Object({
1036+
if: true,
1037+
});
1038+
1039+
const parseResult = parse(context, schema);
1040+
1041+
expect(parseResult).to.contain.warning(
1042+
"'Schema Object' contains invalid key 'if'"
1043+
);
1044+
});
1045+
1046+
it('provides warning for unsupported OpenAPI 3.1 property', () => {
1047+
context.openapiVersion = { major: 3, minor: 1 };
1048+
const schema = new namespace.elements.Object({
1049+
if: true,
1050+
});
1051+
1052+
const parseResult = parse(context, schema);
1053+
1054+
expect(parseResult).to.contain.warning(
1055+
"'Schema Object' contains unsupported key 'if'"
1056+
);
1057+
});
1058+
});
10201059
});

0 commit comments

Comments
 (0)