From 373ad9d6686ebd5d4ac1cc21170142517f95bb3a Mon Sep 17 00:00:00 2001 From: Brad Jones Date: Mon, 22 Jul 2024 04:07:48 -0600 Subject: [PATCH] Treat `dependentSchemas` as a map not array. (#1641) --- .changeset/five-cougars-arrive.md | 7 +++ .../src/rules/common/__tests__/spec.test.ts | 47 +++++++++++++++++++ packages/core/src/types/oas3_1.ts | 2 +- 3 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 .changeset/five-cougars-arrive.md diff --git a/.changeset/five-cougars-arrive.md b/.changeset/five-cougars-arrive.md new file mode 100644 index 0000000000..b5967e7e7d --- /dev/null +++ b/.changeset/five-cougars-arrive.md @@ -0,0 +1,7 @@ +--- +"@redocly/openapi-core": patch +"@redocly/cli": patch +--- + +Fixed an issue in the OpenAPI `spec` rule where `dependentSchemas` was parsed as an array. +It is now correctly parsed as a map. diff --git a/packages/core/src/rules/common/__tests__/spec.test.ts b/packages/core/src/rules/common/__tests__/spec.test.ts index 4c45e7f041..ee35721d2d 100644 --- a/packages/core/src/rules/common/__tests__/spec.test.ts +++ b/packages/core/src/rules/common/__tests__/spec.test.ts @@ -607,4 +607,51 @@ describe('Oas3.1 spec', () => { ] `); }); + + it('should flag invalid dependentSchemas', async () => { + const document = parseYamlToDocument( + outdent` + openapi: 3.1.0 + info: + version: 1.0.0 + title: Example.com + description: info, + license: + name: Apache 2.0 + url: 'http://www.apache.org/licenses/LICENSE-2.0.html' + components: + schemas: + withInvalidDependentSchemas: + dependentSchemas: + - invalid1 + - invalid2 + `, + 'foobar.yaml' + ); + + const results = await lintDocument({ + externalRefResolver: new BaseResolver(), + document, + config: await makeConfig({ spec: 'error' }), + }); + + expect(replaceSourceWithRef(results)).toMatchInlineSnapshot(` + [ + { + "from": undefined, + "location": [ + { + "pointer": "#/components/schemas/withInvalidDependentSchemas/dependentSchemas", + "reportOnKey": false, + "source": "foobar.yaml", + }, + ], + "message": "Expected type \`SchemaMap\` (object) but got \`array\`", + "ruleId": "spec", + "severity": "error", + "suggest": [], + }, + ] + `); + }); }); diff --git a/packages/core/src/types/oas3_1.ts b/packages/core/src/types/oas3_1.ts index 4b10e2e707..3eb5000adb 100755 --- a/packages/core/src/types/oas3_1.ts +++ b/packages/core/src/types/oas3_1.ts @@ -133,7 +133,7 @@ const Schema: NodeType = { if: 'Schema', then: 'Schema', else: 'Schema', - dependentSchemas: listOf('Schema'), + dependentSchemas: mapOf('Schema'), dependentRequired: 'DependentRequired', prefixItems: listOf('Schema'), contains: 'Schema',