Skip to content

Commit

Permalink
fix: do not try resolving exteranlValues used as property names (#1778)
Browse files Browse the repository at this point in the history
* fix: do not try resolving exteranlValues used as property names

* fix code and add changeset

* Update .changeset/tricky-balloons-kick.md

Co-authored-by: Jacek Łękawa <[email protected]>

---------

Co-authored-by: Jacek Łękawa <[email protected]>
  • Loading branch information
tatomyr and JLekawa authored Oct 21, 2024
1 parent 6f870fd commit 3b7221e
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
6 changes: 6 additions & 0 deletions .changeset/tricky-balloons-kick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@redocly/openapi-core": patch
"@redocly/cli": patch
---

Fixed an issue where using `externalValue` as a property name was causing the API description validation process to fail.
24 changes: 24 additions & 0 deletions packages/core/src/__tests__/__snapshots__/bundle.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,30 @@ components:
`;

exports[`bundle should bundle schemas with properties named $ref and externalValues correctly 1`] = `
openapi: 3.1.0
info: {}
paths:
/example:
get:
summary: Get Example
description: This endpoint returns an example response.
responses:
'200':
description: Successful response
content:
application/json:
schema:
type: object
properties:
externalValue:
type: string
$ref:
type: string
components: {}
`;

exports[`bundle should dereferenced correctly when used with dereference 1`] = `
openapi: 3.0.0
paths:
Expand Down
9 changes: 9 additions & 0 deletions packages/core/src/__tests__/bundle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,15 @@ describe('bundle', () => {
expect(problems).toHaveLength(0);
expect(parsed).toEqual(origCopy);
});

it('should bundle schemas with properties named $ref and externalValues correctly', async () => {
const { bundle: res, problems } = await bundle({
config: new Config({} as ResolvedConfig),
ref: path.join(__dirname, 'fixtures/refs/openapi-with-special-names-in-props.yaml'),
});
expect(problems).toHaveLength(0);
expect(res.parsed).toMatchSnapshot();
});
});

describe('bundleFromString', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
openapi: 3.1.0
info: {}
paths:
/example:
get:
summary: Get Example
description: This endpoint returns an example response.
responses:
'200':
description: Successful response
content:
application/json:
schema:
type: object
properties:
externalValue:
type: string
$ref:
type: string
10 changes: 7 additions & 3 deletions packages/core/src/ref-utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isTruthy } from './utils';
import { isPlainObject, isTruthy } from './utils';

import type { Source } from './resolve';
import type { OasRef } from './typings/openapi';
Expand All @@ -8,8 +8,12 @@ export function joinPointer(base: string, key: string | number) {
return base[base.length - 1] === '/' ? base + key : base + '/' + key;
}

export function isRef(node: any): node is OasRef {
return node && typeof node.$ref === 'string';
export function isRef(node: unknown): node is OasRef {
return isPlainObject(node) && typeof node.$ref === 'string';
}

export function isExternalValue(node: unknown) {
return isPlainObject(node) && typeof node.externalValue === 'string';
}

export class Location {
Expand Down
12 changes: 10 additions & 2 deletions packages/core/src/resolve.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import * as fs from 'fs';
import * as path from 'path';
import { isRef, joinPointer, escapePointer, parseRef, isAbsoluteUrl, isAnchor } from './ref-utils';
import {
isRef,
joinPointer,
escapePointer,
parseRef,
isAbsoluteUrl,
isAnchor,
isExternalValue,
} from './ref-utils';
import { isNamedType, SpecExtension } from './types';
import { readFileFromUrl, parseYaml, nextTick } from './utils';

Expand Down Expand Up @@ -337,7 +345,7 @@ export async function resolveDocument(opts: {
}

// handle example.externalValue as reference
if (node.externalValue) {
if (isExternalValue(node)) {
const promise = followRef(
rootNodeDocument,
{ $ref: node.externalValue },
Expand Down

1 comment on commit 3b7221e

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report

St.
Category Percentage Covered / Total
🟡 Statements 78.58% 4993/6354
🟡 Branches 67.24% 2063/3068
🟡 Functions 73.01% 825/1130
🟡 Lines 78.87% 4711/5973

Test suite run success

810 tests passing in 121 suites.

Report generated by 🧪jest coverage report action from 3b7221e

Please sign in to comment.