Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bundle externalValue with relative reference in examples object #1747

Merged
merged 9 commits into from
Oct 3, 2024
Merged
6 changes: 6 additions & 0 deletions .changeset/little-pears-sniff.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 the bundle command did not resolve relative links in `externalValue`.
AlexVarchuk marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 4 additions & 0 deletions __tests__/bundle/bundle-external-value/external-value.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"foo": "bar",
"key": "value"
}
6 changes: 6 additions & 0 deletions __tests__/bundle/bundle-external-value/redocly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
apis:
main:
root: ./test.yaml

extends:
- recommended
37 changes: 37 additions & 0 deletions __tests__/bundle/bundle-external-value/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E bundle bundle-external-value 1`] = `
openapi: 3.1.0
info:
version: 1.0.0
title: Example.com
termsOfService: https://example.com/terms/
contact:
email: [email protected]
url: http://example.com/contact
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
description: OpenAPI description with external example
security: []
paths:
/:
post:
summary: Test request externalValue with relative reference in examples
requestBody:
content:
application/xml:
schema:
type: object
examples:
mergeRequest:
summary: Merge request example
value:
foo: bar
key: value
components: {}

bundling ./test.yaml...
📦 Created a bundle for ./test.yaml at stdout <test>ms.

`;
28 changes: 28 additions & 0 deletions __tests__/bundle/bundle-external-value/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
openapi: 3.1.0
security: []
info:
version: 1.0.0
title: Example.com
termsOfService: https://example.com/terms/
contact:
email: [email protected]
url: http://example.com/contact
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
description: OpenAPI description with external example
components: {}

paths:
/:
post:
summary: Test request externalValue with relative reference in examples
requestBody:
content:
application/xml:
schema:
type: object
examples:
mergeRequest:
summary: Merge request example
externalValue: './external-value.json'
4 changes: 4 additions & 0 deletions packages/core/src/ref-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export function isRef(node: any): node is OasRef {
return node && typeof node.$ref === 'string';
}

export function isRelativeReference(value: any): boolean {
AlexVarchuk marked this conversation as resolved.
Show resolved Hide resolved
return !value.startsWith('http');
}

export class Location {
constructor(public source: Source, public pointer: string) {}

Expand Down
18 changes: 17 additions & 1 deletion 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,
isRelativeReference,
} from './ref-utils';
import { isNamedType, SpecExtension } from './types';
import { readFileFromUrl, parseYaml, nextTick } from './utils';

Expand Down Expand Up @@ -289,6 +297,14 @@ export async function resolveDocument(opts: {
return;
}

// handle externalValue with relative reference in examples object
if (node.externalValue && isRelativeReference(node.externalValue)) {
AlexVarchuk marked this conversation as resolved.
Show resolved Hide resolved
node.value = {
$ref: node.externalValue,
};
delete node.externalValue;
}

for (const propName of Object.keys(node)) {
let propValue = node[propName];
let propType = type.properties[propName];
Expand Down
Loading