@redocly/openapi-core 1.28.5 → 1.29.0

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.
Files changed (48) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/lib/config/all.js +3 -0
  3. package/lib/config/minimal.js +3 -0
  4. package/lib/config/recommended-strict.js +3 -0
  5. package/lib/config/recommended.js +3 -0
  6. package/lib/rules/common/no-schema-type-mismatch.d.ts +2 -0
  7. package/lib/rules/common/no-schema-type-mismatch.js +22 -0
  8. package/lib/rules/oas2/index.js +2 -0
  9. package/lib/rules/oas3/index.js +2 -0
  10. package/lib/rules/oas3/no-invalid-media-type-examples.js +1 -1
  11. package/lib/types/overlay.d.ts +2 -0
  12. package/lib/types/overlay.js +39 -0
  13. package/lib/types/redocly-yaml.d.ts +2 -2
  14. package/lib/types/redocly-yaml.js +3 -1
  15. package/lib/typings/arazzo.d.ts +1 -1
  16. package/lib/typings/arazzo.js +1 -1
  17. package/lib/typings/overlay.d.ts +17 -0
  18. package/lib/typings/overlay.js +4 -0
  19. package/package.json +1 -1
  20. package/src/config/__tests__/__snapshots__/config-resolvers.test.ts.snap +6 -0
  21. package/src/config/all.ts +3 -0
  22. package/src/config/minimal.ts +3 -0
  23. package/src/config/recommended-strict.ts +3 -0
  24. package/src/config/recommended.ts +3 -0
  25. package/src/rules/arazzo/__tests__/criteria-unique.test.ts +1 -1
  26. package/src/rules/arazzo/__tests__/no-criteria-xpath.test.ts +1 -1
  27. package/src/rules/arazzo/__tests__/parameters-unique.test.ts +1 -1
  28. package/src/rules/arazzo/__tests__/requestBody-replacements-unique.test.ts +1 -1
  29. package/src/rules/arazzo/__tests__/source-description-type.test.ts +1 -1
  30. package/src/rules/arazzo/__tests__/sourceDescription-name-unique.test.ts +1 -1
  31. package/src/rules/arazzo/__tests__/sourceDescriptions-not-empty.test.ts +2 -2
  32. package/src/rules/arazzo/__tests__/spot-supported-versions.test.ts +1 -1
  33. package/src/rules/arazzo/__tests__/step-onFailure-unique.test.ts +1 -1
  34. package/src/rules/arazzo/__tests__/step-onSuccess-unique.test.ts +1 -1
  35. package/src/rules/arazzo/__tests__/stepId-unique.test.ts +1 -1
  36. package/src/rules/arazzo/__tests__/workflow-dependsOn.test.ts +2 -2
  37. package/src/rules/arazzo/__tests__/workflowId-unique.test.ts +1 -1
  38. package/src/rules/common/__tests__/no-schema-type-mismatch.test.ts +126 -0
  39. package/src/rules/common/no-schema-type-mismatch.ts +24 -0
  40. package/src/rules/oas2/index.ts +2 -0
  41. package/src/rules/oas3/__tests__/no-invalid-media-type-examples.test.ts +50 -0
  42. package/src/rules/oas3/index.ts +2 -0
  43. package/src/rules/oas3/no-invalid-media-type-examples.ts +1 -1
  44. package/src/types/overlay.ts +40 -0
  45. package/src/types/redocly-yaml.ts +4 -2
  46. package/src/typings/arazzo.ts +2 -2
  47. package/src/typings/overlay.ts +19 -0
  48. package/tsconfig.tsbuildinfo +1 -1
@@ -0,0 +1,126 @@
1
+ import { outdent } from 'outdent';
2
+ import { makeConfig, parseYamlToDocument, replaceSourceWithRef } from '../../../../__tests__/utils';
3
+ import { lintDocument } from '../../../lint';
4
+ import { BaseResolver } from '../../../resolve';
5
+
6
+ describe('no-schema-type-mismatch rule', () => {
7
+ it('should report a warning for object type with items field', async () => {
8
+ const yaml = outdent`
9
+ openapi: 3.0.0
10
+ info:
11
+ title: Test API
12
+ version: 1.0.0
13
+ paths:
14
+ /test:
15
+ get:
16
+ responses:
17
+ '200':
18
+ description: OK
19
+ content:
20
+ application/json:
21
+ schema:
22
+ type: object
23
+ items:
24
+ type: string
25
+ `;
26
+
27
+ const document = parseYamlToDocument(yaml, 'test.yaml');
28
+ const results = await lintDocument({
29
+ document,
30
+ externalRefResolver: new BaseResolver(),
31
+ config: await makeConfig({ rules: { 'no-schema-type-mismatch': 'warn' } }),
32
+ });
33
+
34
+ expect(replaceSourceWithRef(results)).toEqual([
35
+ {
36
+ location: [
37
+ {
38
+ pointer: '#/paths/~1test/get/responses/200/content/application~1json/schema/items',
39
+ reportOnKey: false,
40
+ source: 'test.yaml',
41
+ },
42
+ ],
43
+ message: "Schema type mismatch: 'object' type should not contain 'items' field.",
44
+ ruleId: 'no-schema-type-mismatch',
45
+ severity: 'warn',
46
+ suggest: [],
47
+ },
48
+ ]);
49
+ });
50
+
51
+ it('should report a warning for array type with properties field', async () => {
52
+ const yaml = outdent`
53
+ openapi: 3.0.0
54
+ info:
55
+ title: Test API
56
+ version: 1.0.0
57
+ paths:
58
+ /test:
59
+ get:
60
+ responses:
61
+ '200':
62
+ description: OK
63
+ content:
64
+ application/json:
65
+ schema:
66
+ type: array
67
+ properties:
68
+ name:
69
+ type: string
70
+ `;
71
+
72
+ const document = parseYamlToDocument(yaml, 'test.yaml');
73
+ const results = await lintDocument({
74
+ document,
75
+ externalRefResolver: new BaseResolver(),
76
+ config: await makeConfig({ rules: { 'no-schema-type-mismatch': 'warn' } }),
77
+ });
78
+
79
+ expect(replaceSourceWithRef(results)).toEqual([
80
+ {
81
+ location: [
82
+ {
83
+ pointer: '#/paths/~1test/get/responses/200/content/application~1json/schema/properties',
84
+ reportOnKey: false,
85
+ source: 'test.yaml',
86
+ },
87
+ ],
88
+ message: "Schema type mismatch: 'array' type should not contain 'properties' field.",
89
+ ruleId: 'no-schema-type-mismatch',
90
+ severity: 'warn',
91
+ suggest: [],
92
+ },
93
+ ]);
94
+ });
95
+
96
+ it('should not report a warning for valid schemas', async () => {
97
+ const yaml = outdent`
98
+ openapi: 3.0.0
99
+ info:
100
+ title: Test API
101
+ version: 1.0.0
102
+ paths:
103
+ /test:
104
+ get:
105
+ responses:
106
+ '200':
107
+ description: OK
108
+ content:
109
+ application/json:
110
+ schema:
111
+ type: object
112
+ properties:
113
+ name:
114
+ type: string
115
+ `;
116
+
117
+ const document = parseYamlToDocument(yaml, 'test.yaml');
118
+ const results = await lintDocument({
119
+ document,
120
+ externalRefResolver: new BaseResolver(),
121
+ config: await makeConfig({ rules: { 'no-schema-type-mismatch': 'warn' } }),
122
+ });
123
+
124
+ expect(replaceSourceWithRef(results)).toEqual([]);
125
+ });
126
+ });
@@ -0,0 +1,24 @@
1
+ import type { Oas3Schema } from '../../typings/openapi';
2
+ import type { Oas2Schema } from '../../typings/swagger';
3
+ import type { Oas2Rule, Oas3Rule } from '../../visitors';
4
+ import type { UserContext } from '../../walk';
5
+
6
+ export const NoSchemaTypeMismatch: Oas3Rule | Oas2Rule = () => {
7
+ return {
8
+ Schema(schema: Oas2Schema | Oas3Schema, { report, location }: UserContext) {
9
+ if (schema.type === 'object' && schema.items) {
10
+ report({
11
+ message: "Schema type mismatch: 'object' type should not contain 'items' field.",
12
+ location: location.child('items'),
13
+ });
14
+ }
15
+
16
+ if (schema.type === 'array' && schema.properties) {
17
+ report({
18
+ message: "Schema type mismatch: 'array' type should not contain 'properties' field.",
19
+ location: location.child('properties'),
20
+ });
21
+ }
22
+ },
23
+ };
24
+ };
@@ -42,6 +42,7 @@ import { ScalarPropertyMissingExample } from '../common/scalar-property-missing-
42
42
  import { RequiredStringPropertyMissingMinLength } from '../common/required-string-property-missing-min-length';
43
43
  import { SpecStrictRefs } from '../common/spec-strict-refs';
44
44
  import { NoRequiredSchemaPropertiesUndefined } from '../common/no-required-schema-properties-undefined';
45
+ import { NoSchemaTypeMismatch } from '../common/no-schema-type-mismatch';
45
46
 
46
47
  import type { Oas2Rule } from '../../visitors';
47
48
  import type { Oas2RuleSet } from '../../oas-types';
@@ -95,6 +96,7 @@ export const rules: Oas2RuleSet<'built-in'> = {
95
96
  'required-string-property-missing-min-length': RequiredStringPropertyMissingMinLength as Oas2Rule,
96
97
  'spec-strict-refs': SpecStrictRefs as Oas2Rule,
97
98
  'no-required-schema-properties-undefined': NoRequiredSchemaPropertiesUndefined as Oas2Rule,
99
+ 'no-schema-type-mismatch': NoSchemaTypeMismatch as Oas2Rule,
98
100
  };
99
101
 
100
102
  export const preprocessors = {};
@@ -677,4 +677,54 @@ describe('no-invalid-media-type-examples', () => {
677
677
  ]
678
678
  `);
679
679
  });
680
+
681
+ it('should first report on unresolved ref rather than fail on validation', async () => {
682
+ const document = parseYamlToDocument(
683
+ outdent`
684
+ openapi: 3.1.0
685
+ paths:
686
+ /groups:
687
+ get:
688
+ responses:
689
+ '200':
690
+ content:
691
+ application/json:
692
+ schema:
693
+ type: string
694
+ examples:
695
+ example1:
696
+ $ref: '#/components/examples/NotExisting'
697
+ `,
698
+ 'foobar.yaml'
699
+ );
700
+
701
+ const results = await lintDocument({
702
+ externalRefResolver: new BaseResolver(),
703
+ document,
704
+ config: await makeConfig({
705
+ rules: {
706
+ 'no-invalid-media-type-examples': 'warn',
707
+ 'no-unresolved-refs': 'error',
708
+ },
709
+ }),
710
+ });
711
+
712
+ expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(`
713
+ [
714
+ {
715
+ "location": [
716
+ {
717
+ "pointer": "#/paths/~1groups/get/responses/200/content/application~1json/examples/example1",
718
+ "reportOnKey": false,
719
+ "source": "foobar.yaml",
720
+ },
721
+ ],
722
+ "message": "Can't resolve $ref",
723
+ "ruleId": "no-unresolved-refs",
724
+ "severity": "error",
725
+ "suggest": [],
726
+ },
727
+ ]
728
+ `);
729
+ });
680
730
  });
@@ -54,6 +54,7 @@ import { SpecStrictRefs } from '../common/spec-strict-refs';
54
54
  import { ComponentNameUnique } from './component-name-unique';
55
55
  import { ArrayParameterSerialization } from './array-parameter-serialization';
56
56
  import { NoRequiredSchemaPropertiesUndefined } from '../common/no-required-schema-properties-undefined';
57
+ import { NoSchemaTypeMismatch } from '../common/no-schema-type-mismatch';
57
58
 
58
59
  import type { Oas3RuleSet } from '../../oas-types';
59
60
  import type { Oas3Rule } from '../../visitors';
@@ -119,6 +120,7 @@ export const rules: Oas3RuleSet<'built-in'> = {
119
120
  'component-name-unique': ComponentNameUnique as Oas3Rule,
120
121
  'array-parameter-serialization': ArrayParameterSerialization,
121
122
  'no-required-schema-properties-undefined': NoRequiredSchemaPropertiesUndefined as Oas3Rule,
123
+ 'no-schema-type-mismatch': NoSchemaTypeMismatch as Oas3Rule,
122
124
  };
123
125
 
124
126
  export const preprocessors = {};
@@ -37,7 +37,7 @@ export const ValidContentExamples: Oas3Rule = (opts) => {
37
37
  location = isMultiple ? resolved.location.child('value') : resolved.location;
38
38
  example = resolved.node;
39
39
  }
40
- if (isMultiple && typeof example.value === 'undefined') {
40
+ if (isMultiple && typeof example?.value === 'undefined') {
41
41
  return;
42
42
  }
43
43
  validateExample(
@@ -0,0 +1,40 @@
1
+ import { type NodeType, listOf } from '.';
2
+
3
+ const Root: NodeType = {
4
+ properties: {
5
+ overlay: { type: 'string' },
6
+ info: 'Info',
7
+ extends: { type: 'string' },
8
+ actions: 'Actions',
9
+ },
10
+ required: ['overlay', 'info', 'actions'],
11
+ extensionsPrefix: 'x-',
12
+ };
13
+
14
+ const Info: NodeType = {
15
+ properties: {
16
+ title: { type: 'string' },
17
+ version: { type: 'string' },
18
+ },
19
+ required: ['title', 'version'],
20
+ extensionsPrefix: 'x-',
21
+ };
22
+
23
+ const Actions: NodeType = listOf('Action');
24
+ const Action: NodeType = {
25
+ properties: {
26
+ target: { type: 'string' },
27
+ description: { type: 'string' },
28
+ update: {}, // any
29
+ remove: { type: 'boolean' },
30
+ },
31
+ required: ['target'],
32
+ extensionsPrefix: 'x-',
33
+ };
34
+
35
+ export const Overlay1Types: Record<string, NodeType> = {
36
+ Root,
37
+ Info,
38
+ Actions,
39
+ Action,
40
+ };
@@ -1,8 +1,8 @@
1
1
  import { rootRedoclyConfigSchema } from '@redocly/config';
2
2
  import { listOf } from '.';
3
- import { omitObjectProps, pickObjectProps, isCustomRuleId } from '../utils';
4
- import { getNodeTypesFromJSONSchema } from './json-schema-adapter';
5
3
  import { SpecVersion, getTypes } from '../oas-types';
4
+ import { isCustomRuleId, omitObjectProps, pickObjectProps } from '../utils';
5
+ import { getNodeTypesFromJSONSchema } from './json-schema-adapter';
6
6
 
7
7
  import type { JSONSchema } from 'json-schema-to-ts';
8
8
  import type { NodeType } from '.';
@@ -48,6 +48,7 @@ const builtInOAS2Rules = [
48
48
  'spec-strict-refs',
49
49
  'no-unresolved-refs',
50
50
  'no-required-schema-properties-undefined',
51
+ 'no-schema-type-mismatch',
51
52
  'boolean-parameter-prefixes',
52
53
  'request-mime-type',
53
54
  'response-contains-property',
@@ -96,6 +97,7 @@ const builtInOAS3Rules = [
96
97
  'spec-strict-refs',
97
98
  'no-unresolved-refs',
98
99
  'no-required-schema-properties-undefined',
100
+ 'no-schema-type-mismatch',
99
101
  'boolean-parameter-prefixes',
100
102
  'component-name-unique',
101
103
  'no-empty-servers',
@@ -142,7 +142,7 @@ export interface Workflow {
142
142
  }
143
143
 
144
144
  export interface ArazzoDefinition {
145
- arazzo: '1.0.0';
145
+ arazzo: '1.0.1';
146
146
  info: InfoObject;
147
147
  sourceDescriptions: SourceDescription[];
148
148
  workflows: Workflow[];
@@ -169,4 +169,4 @@ export interface ArazzoDefinition {
169
169
 
170
170
  export const VERSION_PATTERN = /^1\.0\.\d+(-.+)?$/;
171
171
 
172
- export const ARAZZO_VERSIONS_SUPPORTED_BY_SPOT = ['1.0.0', '1.0.1'];
172
+ export const ARAZZO_VERSIONS_SUPPORTED_BY_SPOT = ['1.0.1'];
@@ -0,0 +1,19 @@
1
+ export interface InfoObject {
2
+ title: string;
3
+ version: string;
4
+ }
5
+
6
+ export interface ActionObject {
7
+ target: string;
8
+ description?: string;
9
+ update?: unknown;
10
+ remove?: boolean;
11
+ }
12
+ export interface Overlay1Definition {
13
+ overlay: '1.0.0';
14
+ info: InfoObject;
15
+ extends?: string;
16
+ actions: ActionObject[];
17
+ }
18
+
19
+ export const VERSION_PATTERN = /^1\.0\.\d+(-.+)?$/;