@redocly/openapi-core 1.0.0-beta.77 → 1.0.0-beta.81
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/__tests__/utils.ts +11 -1
- package/lib/bundle.d.ts +3 -1
- package/lib/bundle.js +30 -8
- package/lib/config/builtIn.js +18 -4
- package/lib/config/config.d.ts +2 -9
- package/lib/{rules → decorators}/common/info-description-override.d.ts +0 -0
- package/lib/{rules → decorators}/common/info-description-override.js +0 -0
- package/lib/{rules → decorators}/common/operation-description-override.d.ts +0 -0
- package/lib/{rules → decorators}/common/operation-description-override.js +0 -0
- package/lib/{rules → decorators}/common/registry-dependencies.d.ts +0 -0
- package/lib/{rules → decorators}/common/registry-dependencies.js +0 -0
- package/lib/decorators/common/remove-x-internal.d.ts +2 -0
- package/lib/decorators/common/remove-x-internal.js +58 -0
- package/lib/{rules → decorators}/common/tag-description-override.d.ts +0 -0
- package/lib/{rules → decorators}/common/tag-description-override.js +0 -0
- package/lib/decorators/oas2/index.d.ts +8 -0
- package/lib/decorators/oas2/index.js +15 -0
- package/lib/decorators/oas3/index.d.ts +8 -0
- package/lib/decorators/oas3/index.js +15 -0
- package/lib/js-yaml/index.js +1 -1
- package/lib/redocly/registry-api.js +4 -1
- package/lib/resolve.d.ts +1 -0
- package/lib/resolve.js +7 -3
- package/lib/rules/common/operation-security-defined.js +1 -4
- package/lib/rules/common/spec.js +18 -3
- package/lib/rules/oas2/index.d.ts +1 -7
- package/lib/rules/oas2/index.js +1 -11
- package/lib/rules/oas2/remove-unused-components.d.ts +2 -0
- package/lib/rules/oas2/remove-unused-components.js +73 -0
- package/lib/rules/oas3/index.d.ts +0 -7
- package/lib/rules/oas3/index.js +1 -11
- package/lib/rules/oas3/no-invalid-media-type-examples.js +12 -11
- package/lib/rules/oas3/remove-unused-components.d.ts +2 -0
- package/lib/rules/oas3/remove-unused-components.js +83 -0
- package/lib/types/index.d.ts +4 -0
- package/lib/types/oas2.js +42 -35
- package/lib/types/oas3.js +68 -139
- package/lib/types/oas3_1.js +52 -49
- package/lib/typings/swagger.d.ts +14 -0
- package/lib/utils.d.ts +2 -0
- package/lib/utils.js +10 -2
- package/lib/walk.js +5 -8
- package/package.json +1 -1
- package/src/__tests__/js-yaml.test.ts +29 -5
- package/src/__tests__/lint.test.ts +1 -3
- package/src/bundle.ts +37 -8
- package/src/config/builtIn.ts +20 -7
- package/src/config/config.ts +2 -3
- package/src/decorators/__tests__/remove-x-internal.test.ts +316 -0
- package/src/{rules → decorators}/common/info-description-override.ts +0 -0
- package/src/{rules → decorators}/common/operation-description-override.ts +0 -0
- package/src/{rules → decorators}/common/registry-dependencies.ts +0 -0
- package/src/decorators/common/remove-x-internal.ts +59 -0
- package/src/{rules → decorators}/common/tag-description-override.ts +0 -0
- package/src/decorators/oas2/index.ts +14 -0
- package/src/decorators/oas3/index.ts +14 -0
- package/src/js-yaml/index.ts +1 -2
- package/src/redocly/registry-api.ts +7 -1
- package/src/resolve.ts +6 -6
- package/src/rules/__tests__/no-unresolved-refs.test.ts +1 -4
- package/src/rules/common/__tests__/info-description.test.ts +1 -3
- package/src/rules/common/__tests__/info-license.test.ts +1 -2
- package/src/rules/common/__tests__/license-url.test.ts +1 -2
- package/src/rules/common/__tests__/no-ambiguous-paths.test.ts +1 -2
- package/src/rules/common/__tests__/no-enum-type-mismatch.test.ts +1 -2
- package/src/rules/common/__tests__/no-identical-paths.test.ts +1 -2
- package/src/rules/common/__tests__/no-path-trailing-slash.test.ts +1 -2
- package/src/rules/common/__tests__/operation-2xx-response.test.ts +1 -2
- package/src/rules/common/__tests__/operation-4xx-response.test.ts +1 -2
- package/src/rules/common/__tests__/operation-operationId-unique.test.ts +1 -2
- package/src/rules/common/__tests__/operation-operationId-url-safe.test.ts +1 -2
- package/src/rules/common/__tests__/operation-parameters-unique.test.ts +1 -2
- package/src/rules/common/__tests__/operation-security-defined.test.ts +1 -2
- package/src/rules/common/__tests__/operation-singular-tag.test.ts +1 -2
- package/src/rules/common/__tests__/path-http-verbs-order.test.ts +1 -2
- package/src/rules/common/__tests__/path-not-include-query.test.ts +1 -2
- package/src/rules/common/__tests__/path-params-defined.test.ts +1 -2
- package/src/rules/common/__tests__/paths-kebab-case.test.ts +1 -2
- package/src/rules/common/__tests__/tag-description.test.ts +1 -2
- package/src/rules/common/__tests__/tags-alphabetical.test.ts +1 -2
- package/src/rules/common/operation-security-defined.ts +4 -5
- package/src/rules/common/spec.ts +18 -0
- package/src/rules/oas2/__tests__/boolean-parameter-prefixes.test.ts +1 -2
- package/src/rules/oas2/__tests__/spec/referenceableScalars.test.ts +1 -2
- package/src/rules/oas2/index.ts +1 -11
- package/src/rules/oas2/remove-unused-components.ts +76 -0
- package/src/rules/oas3/__tests__/boolean-parameter-prefixes.test.ts +1 -2
- package/src/rules/oas3/__tests__/no-empty-enum-servers.com.test.ts +1 -2
- package/src/rules/oas3/__tests__/no-example-value-and-externalValue.test.ts +1 -2
- package/src/rules/oas3/__tests__/no-invalid-media-type-examples.test.ts +1 -2
- package/src/rules/oas3/__tests__/no-server-example.com.test.ts +1 -2
- package/src/rules/oas3/__tests__/no-server-trailing-slash.test.ts +1 -2
- package/src/rules/oas3/__tests__/no-unused-components.test.ts +1 -2
- package/src/rules/oas3/index.ts +0 -12
- package/src/rules/oas3/no-invalid-media-type-examples.ts +17 -21
- package/src/rules/oas3/remove-unused-components.ts +84 -0
- package/src/types/index.ts +4 -0
- package/src/types/oas2.ts +42 -43
- package/src/types/oas3.ts +68 -141
- package/src/types/oas3_1.ts +51 -50
- package/src/typings/swagger.ts +7 -0
- package/src/utils.ts +9 -1
- package/src/walk.ts +6 -13
- package/tsconfig.tsbuildinfo +1 -1
- package/lib/rules/builtin.d.ts +0 -20
- package/lib/rules/builtin.js +0 -17
- package/src/rules/__tests__/config.ts +0 -10
- package/src/rules/builtin.ts +0 -18
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { outdent } from 'outdent';
|
|
2
2
|
import { lintDocument } from '../../../lint';
|
|
3
|
-
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils';
|
|
4
|
-
import { makeConfig } from '../../__tests__/config';
|
|
3
|
+
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
|
|
5
4
|
import { BaseResolver } from '../../../resolve';
|
|
6
5
|
|
|
7
6
|
describe('Oas3 path-params-defined', () => {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { outdent } from 'outdent';
|
|
2
2
|
import { lintDocument } from '../../../lint';
|
|
3
|
-
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils';
|
|
4
|
-
import { makeConfig } from '../../__tests__/config';
|
|
3
|
+
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
|
|
5
4
|
import { BaseResolver } from '../../../resolve';
|
|
6
5
|
|
|
7
6
|
describe('Oas3 paths-kebab-case', () => {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { outdent } from 'outdent';
|
|
2
2
|
import { lintDocument } from '../../../lint';
|
|
3
|
-
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils';
|
|
4
|
-
import { makeConfig } from '../../__tests__/config';
|
|
3
|
+
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
|
|
5
4
|
import { BaseResolver } from '../../../resolve';
|
|
6
5
|
|
|
7
6
|
describe('Oas3 tag-description', () => {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { outdent } from 'outdent';
|
|
2
2
|
import { lintDocument } from '../../../lint';
|
|
3
|
-
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils';
|
|
4
|
-
import { makeConfig } from '../../__tests__/config';
|
|
3
|
+
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
|
|
5
4
|
import { BaseResolver } from '../../../resolve';
|
|
6
5
|
|
|
7
6
|
describe('Oas3 tags-alphabetical', () => {
|
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { Oas3Rule, Oas2Rule } from '../../visitors';
|
|
2
2
|
import { Location } from '../../ref-utils';
|
|
3
3
|
import { UserContext } from '../../walk';
|
|
4
|
+
import { Oas2SecurityScheme } from '../../typings/swagger';
|
|
5
|
+
import { Oas3SecurityScheme } from '../../typings/openapi';
|
|
4
6
|
|
|
5
7
|
export const OperationSecurityDefined: Oas3Rule | Oas2Rule = () => {
|
|
6
8
|
let referencedSchemes = new Map<
|
|
@@ -25,11 +27,8 @@ export const OperationSecurityDefined: Oas3Rule | Oas2Rule = () => {
|
|
|
25
27
|
}
|
|
26
28
|
},
|
|
27
29
|
},
|
|
28
|
-
SecurityScheme(_securityScheme:
|
|
29
|
-
referencedSchemes.set(key.toString(), {
|
|
30
|
-
defined: true,
|
|
31
|
-
from: [],
|
|
32
|
-
});
|
|
30
|
+
SecurityScheme(_securityScheme: Oas2SecurityScheme | Oas3SecurityScheme, { key }: UserContext) {
|
|
31
|
+
referencedSchemes.set(key.toString(), { defined: true, from: [] });
|
|
33
32
|
},
|
|
34
33
|
SecurityRequirement(requirements, { location }) {
|
|
35
34
|
for (const requirement of Object.keys(requirements)) {
|
package/src/rules/common/spec.ts
CHANGED
|
@@ -2,6 +2,7 @@ import type { Oas3Rule, Oas2Rule } from '../../visitors';
|
|
|
2
2
|
import { isNamedType } from '../../types';
|
|
3
3
|
import { oasTypeOf, matchesJsonSchemaType, getSuggest } from '../utils';
|
|
4
4
|
import { isRef } from '../../ref-utils';
|
|
5
|
+
import { isPlainObject } from '../../utils';
|
|
5
6
|
|
|
6
7
|
export const OasSpec: Oas3Rule | Oas2Rule = () => {
|
|
7
8
|
return {
|
|
@@ -26,6 +27,7 @@ export const OasSpec: Oas3Rule | Oas2Rule = () => {
|
|
|
26
27
|
|
|
27
28
|
const required =
|
|
28
29
|
typeof type.required === 'function' ? type.required(node, key) : type.required;
|
|
30
|
+
|
|
29
31
|
for (let propName of required || []) {
|
|
30
32
|
if (!(node as object).hasOwnProperty(propName)) {
|
|
31
33
|
report({
|
|
@@ -35,6 +37,22 @@ export const OasSpec: Oas3Rule | Oas2Rule = () => {
|
|
|
35
37
|
}
|
|
36
38
|
}
|
|
37
39
|
|
|
40
|
+
const allowed = type.allowed?.(node);
|
|
41
|
+
if (allowed && isPlainObject(node)) {
|
|
42
|
+
for (const propName in node) {
|
|
43
|
+
if (allowed.includes(propName) ||
|
|
44
|
+
(type.extensionsPrefix && propName.startsWith(type.extensionsPrefix)) ||
|
|
45
|
+
!Object.keys(type.properties).includes(propName)
|
|
46
|
+
) {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
report({
|
|
50
|
+
message: `The field \`${propName}\` is not allowed here.`,
|
|
51
|
+
location: location.child([propName]).key()
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
38
56
|
const requiredOneOf = type.requiredOneOf || null;
|
|
39
57
|
if (requiredOneOf) {
|
|
40
58
|
let hasProperty = false;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { outdent } from 'outdent';
|
|
2
|
-
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils';
|
|
3
|
-
import { makeConfig } from '../../__tests__/config';
|
|
2
|
+
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
|
|
4
3
|
import { lintDocument } from '../../../lint';
|
|
5
4
|
import { BaseResolver } from '../../../resolve';
|
|
6
5
|
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { outdent } from 'outdent';
|
|
2
|
-
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../../__tests__/utils';
|
|
2
|
+
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../../__tests__/utils';
|
|
3
3
|
import { lintDocument } from '../../../../lint';
|
|
4
4
|
import { BaseResolver } from '../../../../resolve';
|
|
5
|
-
import { makeConfig } from '../../../__tests__/config';
|
|
6
5
|
|
|
7
6
|
describe('Referenceable scalars', () => {
|
|
8
7
|
it('should not report $ref description', async () => {
|
package/src/rules/oas2/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Oas2Rule } from '../../visitors';
|
|
1
2
|
import { OasSpec } from '../common/spec';
|
|
2
3
|
import { NoInvalidSchemaExamples } from '../common/no-invalid-schema-examples';
|
|
3
4
|
import { NoInvalidParameterExamples } from '../common/no-invalid-parameter-examples';
|
|
@@ -26,8 +27,6 @@ import { OperationSingularTag } from '../common/operation-singular-tag';
|
|
|
26
27
|
import { OperationSecurityDefined } from '../common/operation-security-defined';
|
|
27
28
|
import { NoUnresolvedRefs } from '../no-unresolved-refs';
|
|
28
29
|
import { PathHttpVerbsOrder } from '../common/path-http-verbs-order';
|
|
29
|
-
import { Oas2Decorator, Oas2Rule } from '../../visitors';
|
|
30
|
-
import { RegistryDependencies } from '../common/registry-dependencies';
|
|
31
30
|
import { NoIdenticalPaths } from '../common/no-identical-paths';
|
|
32
31
|
import { OperationOperationId } from '../common/operation-operationId';
|
|
33
32
|
import { OperationSummary } from '../common/operation-summary';
|
|
@@ -37,9 +36,6 @@ import { PathExcludesPatterns } from '../common/path-excludes-patterns';
|
|
|
37
36
|
import { RequestMimeType } from './request-mime-type';
|
|
38
37
|
import { ResponseMimeType } from './response-mime-type';
|
|
39
38
|
import { PathSegmentPlural } from '../common/path-segment-plural';
|
|
40
|
-
import { OperationDescriptionOverride } from '../common/operation-description-override';
|
|
41
|
-
import { TagDescriptionOverride } from '../common/tag-description-override';
|
|
42
|
-
import { InfoDescriptionOverride } from '../common/info-description-override';
|
|
43
39
|
|
|
44
40
|
export const rules = {
|
|
45
41
|
spec: OasSpec as Oas2Rule,
|
|
@@ -83,9 +79,3 @@ export const rules = {
|
|
|
83
79
|
};
|
|
84
80
|
|
|
85
81
|
export const preprocessors = {};
|
|
86
|
-
export const decorators = {
|
|
87
|
-
'registry-dependencies': RegistryDependencies as Oas2Decorator,
|
|
88
|
-
'operation-description-override': OperationDescriptionOverride as Oas2Decorator,
|
|
89
|
-
'tag-description-override': TagDescriptionOverride as Oas2Decorator,
|
|
90
|
-
'info-description-override': InfoDescriptionOverride as Oas2Decorator,
|
|
91
|
-
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Oas2Rule } from '../../visitors';
|
|
2
|
+
import { Location } from '../../ref-utils';
|
|
3
|
+
import { Oas2Components } from '../../typings/swagger';
|
|
4
|
+
import { isEmptyObject } from '../../utils';
|
|
5
|
+
|
|
6
|
+
export const RemoveUnusedComponents: Oas2Rule = () => {
|
|
7
|
+
let components = new Map<string, { used: boolean; componentType?: keyof Oas2Components; name: string }>();
|
|
8
|
+
|
|
9
|
+
function registerComponent(location: Location, componentType: keyof Oas2Components, name: string): void {
|
|
10
|
+
components.set(location.absolutePointer, {
|
|
11
|
+
used: components.get(location.absolutePointer)?.used || false,
|
|
12
|
+
componentType,
|
|
13
|
+
name,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
ref: {
|
|
19
|
+
leave(ref, { type, resolve, key }) {
|
|
20
|
+
if (
|
|
21
|
+
['Schema', 'Parameter', 'Response', 'SecurityScheme'].includes(type.name)
|
|
22
|
+
) {
|
|
23
|
+
const resolvedRef = resolve(ref);
|
|
24
|
+
if (!resolvedRef.location) return;
|
|
25
|
+
components.set(resolvedRef.location.absolutePointer, {
|
|
26
|
+
used: true,
|
|
27
|
+
name: key.toString(),
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
DefinitionRoot: {
|
|
33
|
+
leave(root, ctx) {
|
|
34
|
+
const data = ctx.getVisitorData() as { removedCount: number };
|
|
35
|
+
data.removedCount = 0;
|
|
36
|
+
|
|
37
|
+
let rootComponents = new Set<keyof Oas2Components>();
|
|
38
|
+
components.forEach(usageInfo => {
|
|
39
|
+
const { used, name, componentType } = usageInfo;
|
|
40
|
+
if (!used && componentType) {
|
|
41
|
+
rootComponents.add(componentType);
|
|
42
|
+
delete root[componentType]![name];
|
|
43
|
+
data.removedCount++;
|
|
44
|
+
}
|
|
45
|
+
});
|
|
46
|
+
for (const component of rootComponents) {
|
|
47
|
+
if (isEmptyObject(root[component])) {
|
|
48
|
+
delete root[component];
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
NamedSchemas: {
|
|
54
|
+
Schema(schema, { location, key }) {
|
|
55
|
+
if (!schema.allOf) {
|
|
56
|
+
registerComponent(location, 'definitions', key.toString());
|
|
57
|
+
}
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
NamedParameters: {
|
|
61
|
+
Parameter(_parameter, { location, key }) {
|
|
62
|
+
registerComponent(location, 'parameters', key.toString());
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
NamedResponses: {
|
|
66
|
+
Response(_response, { location, key }) {
|
|
67
|
+
registerComponent(location, 'responses', key.toString());
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
NamedSecuritySchemes: {
|
|
71
|
+
SecurityScheme(_securityScheme, { location, key }) {
|
|
72
|
+
registerComponent(location, 'securityDefinitions', key.toString());
|
|
73
|
+
},
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { outdent } from 'outdent';
|
|
2
|
-
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils';
|
|
3
|
-
import { makeConfig } from '../../__tests__/config';
|
|
2
|
+
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
|
|
4
3
|
import { lintDocument } from '../../../lint';
|
|
5
4
|
import { BaseResolver } from '../../../resolve';
|
|
6
5
|
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { outdent } from 'outdent';
|
|
2
2
|
import { lintDocument } from '../../../lint';
|
|
3
|
-
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils';
|
|
3
|
+
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
|
|
4
4
|
import { BaseResolver } from '../../../resolve';
|
|
5
|
-
import { makeConfig } from '../../__tests__/config';
|
|
6
5
|
|
|
7
6
|
describe('Oas3 as3-no-servers-empty-enum', () => {
|
|
8
7
|
it('oas3-no-servers-empty-enum: should report on server object with empty enum and unknown enum value', async () => {
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { outdent } from 'outdent';
|
|
2
2
|
import { lintDocument } from '../../../lint';
|
|
3
|
-
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils';
|
|
3
|
+
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
|
|
4
4
|
import { BaseResolver } from '../../../resolve';
|
|
5
|
-
import { makeConfig } from '../../__tests__/config';
|
|
6
5
|
|
|
7
6
|
describe('Oas3 oas3-no-example-value-and-externalValue', () => {
|
|
8
7
|
it('oas3-no-example-value-and-externalValue: should report on example object with both value and external value', async () => {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { outdent } from 'outdent';
|
|
2
2
|
import { lintDocument } from '../../../lint';
|
|
3
|
-
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils';
|
|
4
|
-
import { makeConfig } from '../../__tests__/config';
|
|
3
|
+
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
|
|
5
4
|
import { BaseResolver } from '../../../resolve';
|
|
6
5
|
|
|
7
6
|
describe('no-invalid-media-type-examples', () => {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { outdent } from 'outdent';
|
|
2
2
|
import { lintDocument } from '../../../lint';
|
|
3
|
-
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils';
|
|
4
|
-
import { makeConfig } from '../../__tests__/config';
|
|
3
|
+
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
|
|
5
4
|
import { BaseResolver } from '../../../resolve';
|
|
6
5
|
|
|
7
6
|
describe('Oas3 oas3-no-server-example.com', () => {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { outdent } from 'outdent';
|
|
2
2
|
import { lintDocument } from '../../../lint';
|
|
3
|
-
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils';
|
|
4
|
-
import { makeConfig } from '../../__tests__/config';
|
|
3
|
+
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
|
|
5
4
|
import { BaseResolver } from '../../../resolve';
|
|
6
5
|
|
|
7
6
|
describe('Oas3 oas3-no-server-trailing-slash', () => {
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { outdent } from 'outdent';
|
|
2
2
|
import { lintDocument } from '../../../lint';
|
|
3
|
-
import { parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils';
|
|
4
|
-
import { makeConfig } from '../../__tests__/config';
|
|
3
|
+
import { parseYamlToDocument, replaceSourceWithRef, makeConfig } from '../../../../__tests__/utils';
|
|
5
4
|
import { BaseResolver } from '../../../resolve';
|
|
6
5
|
|
|
7
6
|
describe('Oas3 no-unused-components', () => {
|
package/src/rules/oas3/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Oas3RuleSet } from '../../oas-types';
|
|
2
|
-
import { Oas3Decorator } from '../../visitors';
|
|
3
2
|
import { OasSpec } from '../common/spec';
|
|
4
3
|
import { Operation2xxResponse } from '../common/operation-2xx-response';
|
|
5
4
|
import { Operation4xxResponse } from '../common/operation-4xx-response';
|
|
@@ -32,7 +31,6 @@ import { PathsKebabCase } from '../common/paths-kebab-case';
|
|
|
32
31
|
import { PathHttpVerbsOrder } from '../common/path-http-verbs-order';
|
|
33
32
|
import { NoEmptyServers } from './no-empty-servers';
|
|
34
33
|
import { ValidContentExamples } from './no-invalid-media-type-examples';
|
|
35
|
-
import { RegistryDependencies } from '../common/registry-dependencies';
|
|
36
34
|
import { NoIdenticalPaths } from '../common/no-identical-paths';
|
|
37
35
|
import { NoUndefinedServerVariable } from './no-undefined-server-variable';
|
|
38
36
|
import { OperationOperationId } from '../common/operation-operationId';
|
|
@@ -43,9 +41,6 @@ import { NoHttpVerbsInPaths } from '../common/no-http-verbs-in-paths';
|
|
|
43
41
|
import { RequestMimeType } from './request-mime-type';
|
|
44
42
|
import { ResponseMimeType } from './response-mime-type';
|
|
45
43
|
import { PathSegmentPlural } from '../common/path-segment-plural';
|
|
46
|
-
import { OperationDescriptionOverride } from '../common/operation-description-override';
|
|
47
|
-
import { TagDescriptionOverride } from '../common/tag-description-override';
|
|
48
|
-
import { InfoDescriptionOverride } from '../common/info-description-override';
|
|
49
44
|
import { PathExcludesPatterns } from '../common/path-excludes-patterns';
|
|
50
45
|
import { NoInvalidSchemaExamples } from '../common/no-invalid-schema-examples';
|
|
51
46
|
import { NoInvalidParameterExamples } from '../common/no-invalid-parameter-examples';
|
|
@@ -100,10 +95,3 @@ export const rules = {
|
|
|
100
95
|
} as Oas3RuleSet;
|
|
101
96
|
|
|
102
97
|
export const preprocessors = {};
|
|
103
|
-
|
|
104
|
-
export const decorators = {
|
|
105
|
-
'registry-dependencies': RegistryDependencies as Oas3Decorator,
|
|
106
|
-
'operation-description-override': OperationDescriptionOverride as Oas3Decorator,
|
|
107
|
-
'tag-description-override': TagDescriptionOverride as Oas3Decorator,
|
|
108
|
-
'info-description-override': InfoDescriptionOverride as Oas3Decorator,
|
|
109
|
-
};
|
|
@@ -13,31 +13,27 @@ export const ValidContentExamples: Oas3Rule = (opts) => {
|
|
|
13
13
|
const { location, resolve } = ctx;
|
|
14
14
|
if (!mediaType.schema) return;
|
|
15
15
|
if (mediaType.example) {
|
|
16
|
+
resolveAndValidateExample(mediaType.example, location.child('example'));
|
|
17
|
+
} else if (mediaType.examples) {
|
|
18
|
+
for (const exampleName of Object.keys(mediaType.examples)) {
|
|
19
|
+
resolveAndValidateExample(mediaType.examples[exampleName], location.child(['examples', exampleName, 'value']), true);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function resolveAndValidateExample(example: Oas3Example | any, location: Location, isMultiple?: boolean) {
|
|
24
|
+
if (isRef(example)) {
|
|
25
|
+
const resolved = resolve<Oas3Example>(example);
|
|
26
|
+
if (!resolved.location) return;
|
|
27
|
+
location = isMultiple ? resolved.location.child('value') : resolved.location;
|
|
28
|
+
example = resolved.node;
|
|
29
|
+
}
|
|
16
30
|
validateExample(
|
|
17
|
-
|
|
18
|
-
mediaType.schema
|
|
19
|
-
location
|
|
31
|
+
isMultiple ? example.value : example,
|
|
32
|
+
mediaType.schema!,
|
|
33
|
+
location,
|
|
20
34
|
ctx,
|
|
21
35
|
disallowAdditionalProperties,
|
|
22
36
|
);
|
|
23
|
-
} else if (mediaType.examples) {
|
|
24
|
-
for (const exampleName of Object.keys(mediaType.examples)) {
|
|
25
|
-
let example = mediaType.examples[exampleName];
|
|
26
|
-
let dataLoc: Location = location.child(['examples', exampleName, 'value']);
|
|
27
|
-
if (isRef(example)) {
|
|
28
|
-
const resolved = resolve<Oas3Example>(example);
|
|
29
|
-
if (!resolved.location) continue;
|
|
30
|
-
dataLoc = resolved.location.child('value');
|
|
31
|
-
example = resolved.node;
|
|
32
|
-
}
|
|
33
|
-
validateExample(
|
|
34
|
-
example.value,
|
|
35
|
-
mediaType.schema,
|
|
36
|
-
dataLoc,
|
|
37
|
-
ctx,
|
|
38
|
-
disallowAdditionalProperties,
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
37
|
}
|
|
42
38
|
},
|
|
43
39
|
},
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Oas3Rule } from '../../visitors';
|
|
2
|
+
import { Location } from '../../ref-utils';
|
|
3
|
+
import { Oas3Components } from '../../typings/openapi'
|
|
4
|
+
import { isEmptyObject } from '../../utils';
|
|
5
|
+
|
|
6
|
+
export const RemoveUnusedComponents: Oas3Rule = () => {
|
|
7
|
+
let components = new Map<string, { used: boolean; componentType?: keyof Oas3Components; name: string }>();
|
|
8
|
+
|
|
9
|
+
function registerComponent(location: Location, componentType: keyof Oas3Components, name: string): void {
|
|
10
|
+
components.set(location.absolutePointer, {
|
|
11
|
+
used: components.get(location.absolutePointer)?.used || false,
|
|
12
|
+
componentType,
|
|
13
|
+
name,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
ref: {
|
|
19
|
+
leave(ref, { type, resolve, key }) {
|
|
20
|
+
if (
|
|
21
|
+
['Schema', 'Header', 'Parameter', 'Response', 'Example', 'RequestBody'].includes(type.name)
|
|
22
|
+
) {
|
|
23
|
+
const resolvedRef = resolve(ref);
|
|
24
|
+
if (!resolvedRef.location) return;
|
|
25
|
+
components.set(resolvedRef.location.absolutePointer, {
|
|
26
|
+
used: true,
|
|
27
|
+
name: key.toString(),
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
DefinitionRoot: {
|
|
33
|
+
leave(root, ctx) {
|
|
34
|
+
const data = ctx.getVisitorData() as { removedCount: number };
|
|
35
|
+
data.removedCount = 0;
|
|
36
|
+
|
|
37
|
+
components.forEach(usageInfo => {
|
|
38
|
+
const { used, componentType, name } = usageInfo;
|
|
39
|
+
if (!used && componentType) {
|
|
40
|
+
let componentChild = root.components![componentType];
|
|
41
|
+
delete componentChild![name];
|
|
42
|
+
data.removedCount++;
|
|
43
|
+
if (isEmptyObject(componentChild)) {
|
|
44
|
+
delete root.components![componentType];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
if (isEmptyObject(root.components)) { delete root.components; }
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
NamedSchemas: {
|
|
52
|
+
Schema(schema, { location, key }) {
|
|
53
|
+
if (!schema.allOf) {
|
|
54
|
+
registerComponent(location, 'schemas', key.toString());
|
|
55
|
+
}
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
NamedParameters: {
|
|
59
|
+
Parameter(_parameter, { location, key }) {
|
|
60
|
+
registerComponent(location, 'parameters', key.toString());
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
NamedResponses: {
|
|
64
|
+
Response(_response, { location, key }) {
|
|
65
|
+
registerComponent(location, 'responses', key.toString());
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
NamedExamples: {
|
|
69
|
+
Example(_example, { location, key }) {
|
|
70
|
+
registerComponent(location, 'examples', key.toString());
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
NamedRequestBodies: {
|
|
74
|
+
RequestBody(_requestBody, { location, key }) {
|
|
75
|
+
registerComponent(location, 'requestBodies', key.toString());
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
NamedHeaders: {
|
|
79
|
+
Header(_header, { location, key }) {
|
|
80
|
+
registerComponent(location, 'headers', key.toString());
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
};
|
package/src/types/index.ts
CHANGED
|
@@ -24,6 +24,8 @@ export type NodeType = {
|
|
|
24
24
|
items?: string;
|
|
25
25
|
required?: string[] | ((value: any, key: string | number | undefined) => string[]);
|
|
26
26
|
requiredOneOf?: string[];
|
|
27
|
+
allowed?: ((value: any) => string[] | undefined);
|
|
28
|
+
extensionsPrefix?: string;
|
|
27
29
|
};
|
|
28
30
|
type PropType = string | NodeType | ScalarSchema | undefined | null;
|
|
29
31
|
type ResolveTypeFn = (value: any, key: string) => string | PropType;
|
|
@@ -35,6 +37,8 @@ export type NormalizedNodeType = {
|
|
|
35
37
|
items?: NormalizedNodeType;
|
|
36
38
|
required?: string[] | ((value: any, key: string | number | undefined) => string[]);
|
|
37
39
|
requiredOneOf?: string[];
|
|
40
|
+
allowed?: ((value: any) => string[] | undefined);
|
|
41
|
+
extensionsPrefix?: string;
|
|
38
42
|
};
|
|
39
43
|
|
|
40
44
|
type NormalizedPropType = NormalizedNodeType | NormalizedScalarSchema | undefined | null;
|